How to convert TextView value to Integer

旧巷老猫 提交于 2021-02-05 12:23:09

问题


Good day programming warriors, please help me to this simple problem of mine, i'm new to android. How can I How to convert TextView value to Integer. please see my attached codes, thank you.

public class appleTrivia extends AppCompatActivity {

     public int total = 0;
     public int score = 40;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apple_trivia);


        TextView scoreLabel = (TextView) findViewById(R.id.labelScore);
        scoreLabel.setText("Score: " + score);
        scoreLabel.setText("Score: " + getIntent().getExtras().getInt("points", 0));

        try {
            total = Integer.parseInt("score: " + scoreLabel);
        }
        catch (NumberFormatException nfe )
        {
        }
    }
        public void onClickProceed (View view) {
        Intent intent = new Intent(this, cherry.class);
        startActivity(intent);
        Toast.makeText(appleTrivia.this, "Your score is:" + total, Toast.LENGTH_LONG).show();
    }
}

I need to convert scoreLabel value to integer so I can add it to another score, thank you. PS. Toast.makeText(appleTrivia.this, "Your score is:" + total, Toast.LENGTH_LONG).show(); give's me 0 score.


回答1:


First, you need to remove the Score : text using regex maybe :

String str = scoreLabel.getText().toString();      
str = str.replaceAll("[^-?0-9]+", "");

Then parse it to integer :

int total = Integer.parseInt(str);

EDIT

public class appleTrivia extends AppCompatActivity {

     public int total = 0;
     public int score = 40;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apple_trivia);


        TextView scoreLabel = (TextView) findViewById(R.id.labelScore);
        scoreLabel.setText("Score: " + score);
        scoreLabel.setText("Score: " + getIntent().getExtras().getInt("points", 0));

        try {
            String str = scoreLabel.getText().toString();      
            str = str.replaceAll("[^-?0-9]+", "");
            total = Integer.parseInt(str);
        }
        catch (NumberFormatException nfe )
        {
        }
    }
        public void onClickProceed (View view) {
        Intent intent = new Intent(this, cherry.class);
        startActivity(intent);
        Toast.makeText(appleTrivia.this, "Your score is:" + total, Toast.LENGTH_LONG).show();
    }
}



回答2:


Try to set the Value in total and just display your result with a different string, if you want to add the int to another one you can use total and display it again.

public class appleTrivia extends AppCompatActivity {

     public int total = 0;
     public int score = 40;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apple_trivia);


        TextView scoreLabel = (TextView) findViewById(R.id.labelScore);
        scoreLabel.setText("Score: " + score);
        scoreLabel.setText("Score: " + getIntent().getExtras().getInt("points", 0));

        try {
        total = Integer.parseInt(scoreLabel.getText().ToString());
        String totalScore = "Score:" + Total; 
        }
        catch (NumberFormatException nfe )
        {
        }
    }
        public void onClickProceed (View view) {
        Intent intent = new Intent(this, cherry.class);
        startActivity(intent);
        Toast.makeText(appleTrivia.this, "Your score is:" + totalScore, Toast.LENGTH_LONG).show();
    }
}


来源:https://stackoverflow.com/questions/34147374/how-to-convert-textview-value-to-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!