如何将TextView值转换为Integer

时间:2015-12-08 03:17:22

标签: javascript android android-intent

美好的一天编程战士,请帮助我解决我的这个简单问题,我是android的新手。我该如何将TextView值转换为Integer。请参阅我附带的代码,谢谢。

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();
    }
}

我需要将scoreLabel值转换为整数,以便将其添加到另一个得分,谢谢。 PS。 Toast.makeText(appleTrivia.this,"你的分数是:" + total,Toast.LENGTH_LONG).show();给我0分。

2 个答案:

答案 0 :(得分:1)

首先,您需要使用正则表达式删除Score :文本:

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

然后将其解析为整数:

int total = Integer.parseInt(str);

修改

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();
    }
}

答案 1 :(得分:0)

尝试设置总值,只显示不同字符串的结果,如果要将int添加到另一个,可以使用total并再次显示。

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();
    }
}