java - 无法在 ClickListener 上查看按​​钮内的 TextView

标签 java android textview

大家早上好,

我想找出为什么我的 TextView 不会显示在我的应用程序中。我在按钮的 onClick 方法中声明了它们,所以就像单击按钮后,它们就会弹出。它基本上是一个“输入”类型的按钮,如果用户输入正确的答案,则在 TextView 中显示正确的答案,否则显示错误的答案。

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

}

我必须制作单独的 TextView 来调用相同的 TextView ,因为由于括号的原因它无法识别它们。这个方法是我的 Game 类的一部分,它只显示一个表达式,并通过按屏幕上的 a 按钮来检查用户的答案是否正确。如果有人知道出了什么问题,请分享。谢谢

<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....

最佳答案

这是因为您正在编写 TextView 的 View 没有得到更新。尝试通过重绘来刷新 View 或传递一些 Intent 来查看书写部分是否有效

关于java - 无法在 ClickListener 上查看按​​钮内的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9579612/

相关文章:

Java/android : Help understanding this construct, 它是方法还是匿名类?

java - 如何在Java中找到所有本地绑定(bind)的IP地址?

Android - RelativeLayout 带图文,设计问题

android - TextView 中的富文本

android - 如何获取TextView的X Y坐标和像素大小?

java - 匿名类中 "this"的值与 lambda 表达式

java - 基本的Java控制结构问题。

android - 如何使用alljoyn传输一次选择的多个文件

android - 如何在 Flutter 中添加 Webview?

android - android如何跟踪分配?