简体   繁体   中英

Can't view my TextView inside buttons onClickListener

Good morning everyone,

I wanted to find out why my textview's won't show up in my app. I declared them inside a button's onClick method, so its like once the buttons been clicked, pop them up. Its basically for an "enter" type of button, where if the user enters the correct answer, display correct in the textview, otherwise wrong.

public void Easy12(){
    Random rand = new Random();
    int a = (int) rand.nextInt(100)+1;
    int b = (int) rand.nextInt(100)+1;
    final TextView tv = (TextView) findViewById(R.id.question_label);
    String aString = Integer.toString(a);
    String bString = Integer.toString(b);
    String display = aString + " + " + bString + " =";
    tv.setText(display);
    final int c = a + b;

    final Button buttonhash = (Button) findViewById(R.id.keypad_hash);
    buttonhash.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            final EditText edittext = (EditText) findViewById(R.id.USERentry);
            if(edittext.getText().equals(c)){
                final TextView answerLabel = (TextView) findViewById(R.id.rightwrong_label);
                answerLabel.setText(R.string.answer_correct);
                answerLabel.setTextColor(R.color.correct_color);
            }
            else
            {
                final TextView answerLabel = (TextView) findViewById(R.id.rightwrong_label);
                answerLabel.setText(R.string.answer_wrong);
                answerLabel.setTextColor(R.color.wrong_color);
            }
        }
    });

}

I had to make separate textviews that call the same textview because it wouldn't recognize them because of the brackets. This method is part of my Game class, which just displays an expression and checks to see whether the users answer is correct or not by pressing the a button on the screen. If anybody knows whats wrong please share. Thank you

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:orientation="vertical" >
<TextView android:id="@+id/question_label"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
<TextView android:id="@+id/rightwrong_label"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
<EditText
android:id="@+id/USERentry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:textColor="@color/user_txt_color"/>
<TableLayout....

This is because your View is not getting updated on which you are writing your textViews. Try refreshing your view by redrawing it or pass some intent to see if the writing part is working or not

OR else , if ur interested use "TOAST" method ,

Toast.makeText(LoginpageActivity.this, "Write your text here to display", Toast.LENGTH_LONG).show();

Here ," LoginpageActivity" s the current class name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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