我很难获得包含用户输入的字符串,并在单击按钮时在文本视图中显示它。例如,如果用户键入 10 并单击按钮,我希望textview显示 10 。我收到警告说 answerINT 从未使用过。
代码我遇到了
的问题 //Fetches user answer and converts it into an integer
EditText answer = (EditText)findViewById(R.id.editanswer); {
if (answer.getText().toString().length() > 0){
int answerInt = Integer.parseInt(answer.getText().toString());
//Display answer in textview on click
TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
displayanswer.setText(answer);
}
其中的coode
findViewById(R.id.btnok).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("onClick", "OK button was clicked");
//Fetches user answer and converts it into an integer
EditText answer = (EditText)findViewById(R.id.editanswer); {
if (answer.getText().toString().length() > 0){
int answerInt = Integer.parseInt(answer.getText().toString());
//Display answer in textview on click
TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
displayanswer.setText(answer);
}
//Displaying a toast if no answer is provided
else
{
Toast.makeText(getApplicationContext(), "No answer provided!",
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:2)
试试这个
displayanswer.setText(" "+answerInt);
OR
displayanswer.setText(String.valueOf(answerInt));
答案 1 :(得分:0)
试试这个:
displayanswer.setText(""+answerInt);
答案 2 :(得分:0)
您可以使用:
String.valueOf(yourint);
答案 3 :(得分:0)
你可以这样做:
if (answer.getText().toString().length() > 0){
//Display answer in textview on click
TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
displayanswer.setText(answer.getText().toString());
}