【问题标题】:How to convert TextView value to Integer如何将 TextView 值转换为整数
【发布时间】:2015-12-08 03:17:22
【问题描述】:

编程勇士们好,请帮我解决这个简单的问题,我是 android 新手。如何将 TextView 值转换为整数。请看我附上的代码,谢谢。

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分。

【问题讨论】:

  • 看我的回答here。和你的问题一样。

标签: javascript android android-intent


【解决方案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();
    }
}

【讨论】:

  • 您好,先生,感谢您快速回放,但似乎我没有明白您的意思,您可以重新编码我发布的源代码吗?我真的不知道在哪里插入您提供的代码。谢谢^_^
  • 顺便说一句,先生,您给我的代码有效,但我意识到答案是字符串值而不是整数值。
  • 我需要它变成整数,这样我就可以添加我所有的分数。
【解决方案2】:

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

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

【讨论】:

  • 先生,这段代码在 toast 消息上给了我“你的分数是:null”。
  • 对不起,我没有早点回复你。我不知道你是否解决了你的问题。如果不是,但显然没有将分数添加到总分的功能。并且您已将变量设置为 0,因此它显示为 null。
猜你喜欢
  • 2016-04-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
相关资源
最近更新 更多