简体   繁体   English

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

[英]How to display a parsed integer string in a textview

I'm having difficulties getting a string containing the user's input and displaying it in a textview when the button is clicked. 我很难获得包含用户输入的字符串,并在单击按钮时将其显示在textview中。 For example, if the user types 10 and click the button, I want the textview to display 10 . 例如,如果用户键入10并单击按钮,我希望textview显示10 I get a warning saying that answerINT is never used. 我收到警告,说从未使用过answerINT

The code I'm having difficulties with 我遇到困难的代码

                //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);

                }

The coode in it's context 上下文中的代码

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


                    }

Try this 尝试这个

 displayanswer.setText(" "+answerInt);

OR 要么

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

尝试这个:

displayanswer.setText(""+answerInt);

您可以使用此:

String.valueOf(yourint);

You can simply do it this way: 您可以这样简单地进行操作:

if (answer.getText().toString().length() > 0){
                //Display answer in textview on click
                TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
                displayanswer.setText(answer.getText().toString());

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM