2015-12-08 59 views
-1

好日子编程勇士,请帮我解决这个简单的问题,我是新来的android。如何将TextView值转换为Integer。请看我附上的代码,谢谢。如何将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,“Your score is:”+ total,Toast.LENGTH_LONG).show();给我0分。

+0

看我的答案[这里](http://stackoverflow.com/a/34481681/2649104)。和你的问题一样。 –

回答

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

嗨,先生,thnx为快速重播,但似乎我没有得到你的观点,你可以重新编码我发布的源代码?我真的不知道在哪里插入你给的代码。 thnx^_^ –

+0

@HerbertWillkomm完成。你需要学习java基础-.-' –

+0

这是我学习先生的一部分。^_^ –

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

+0

这段代码的先生在吐司消息上给了我“Your score is:null”。 –

+0

抱歉,我以前没有回复过你。我不知道你是否已经解决了你的问题。如果不是,但显然不存在将总分添加到总分的功能。并且您已将变量设置为0,因此它显示为空。 – Anasbzr