gpt4 book ai didi

android - 如何在 TextView 中显示已解析的整数字符串

转载 作者:行者123 更新时间:2023-11-29 21:09:00 25 4
gpt4 key购买 nike

我在获取包含用户输入的字符串并在单击按钮时将其显示在 TextView 中时遇到困难。例如,如果用户键入 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);

}

上下文中的代码

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


}

最佳答案

试试这个

 displayanswer.setText(" "+answerInt);

displayanswer.setText(String.valueOf(answerInt));

关于android - 如何在 TextView 中显示已解析的整数字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23560562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com