我想從editText獲取整數,並按下按鈕,在textView中顯示輸入。問題是結果始終爲零。預先感謝您的幫助。Android:將Int轉換爲String和viceversa(在偵聽器中使用try和catch)
EditText editText = (EditText) findViewById(R.id.editText);
TextView textView = (TextView) findViewById(R.id.textView);
Button button = (Button) findViewById(R.id.button);
try{
String string = editText.getText().toString();
int num = Integer.parseInt(string);}
catch(NumberFormatException numb){
Toast.makeText(this, "Problem", Toast.LENGTH_SHORT).show();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = String.valueOf(num);
textView.setText(number);
}
});
可能的重複[如何將字符串轉換爲Java中的int?](http://stackoverflow.com/questions/5585779/how-to-convert-a-string-to-an-int-in- java) – HaroldSer