繁体   English   中英

我在Android中的TextView没有更新

[英]My TextView in Android doesn't update

尽管我已经在该网站上检查了相关问题的解决方案,但仍无法解决问题。 我正在尝试构建一个测验应用程序,该应用程序采用正确的答案,将分数加1并更新分数TextView上的分数。 我尝试通过android:onClick调用score方法,还尝试了setOnClickListener方法,但是它们似乎都不起作用。

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to the Canada Quiz"
        android:textSize="16sp"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_marginBottom="16dp"
        android:layout_gravity="center"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter name"
        android:layout_marginBottom="8dp"
        />
    <TextView
        android:id="@+id/scoreText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginBottom="16dp"
        android:textColor="#000000"
        android:textStyle="bold"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1.  What is the capital of Canada?"
        android:textSize="20dp"
        android:textColor="#000000"
        android:textStyle="bold"
        />
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/tokyo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tokyo"
            android:layout_marginLeft="24dp"
            android:textStyle="bold"
            android:textSize="16dp"/>
        <RadioButton
            android:id="@+id/newYork"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New York"
            android:layout_marginLeft="24dp"
            android:textStyle="bold"
            android:textSize="16dp"/>
        <RadioButton
            android:id="@+id/ottawa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ottawa"
            android:layout_marginLeft="24dp"
            android:textStyle="bold"
            android:textSize="16dp"/>
        <RadioButton
            android:id="@+id/hongKong"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hong Kong"
            android:layout_marginLeft="24dp"
            android:textStyle="bold"
            android:onClick="calculateScore"
            android:textSize="16dp"/>

    </RadioGroup>
    <Button
        android:id="@+id/scoreButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Calculate score"
        android:layout_marginTop="16dp"
        />
</LinearLayout>
</ScrollView>

这是我的Java:

public class MainActivity extends AppCompatActivity {
    int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        RadioButton rb = (RadioButton)findViewById(R.id.ottawa);
        Button calculate = (Button) findViewById(R.id.scoreButton);
        final TextView scoreShow = (TextView) findViewById(R.id.scoreText);

        final boolean rbChecked = rb.isChecked();

        calculate.setOnClickListener(new View.OnClickListener(){
            public void onClick (View v){
                if(rbChecked){
                    score += 1;
                    scoreShow.setText("Your score is: " + score + "/10");
                }
            }
        });
    }

//    public void calculateScore(){
//        RadioButton rb = (RadioButton)findViewById(R.id.ottawa);
//        //Button calculate = (Button) findViewById(R.id.scoreButton);
//        TextView scoreShow = (TextView) findViewById(R.id.scoreText);
//
//        boolean rbChecked = rb.isChecked();
//        if(rbChecked){
//            score += 1;
//            scoreShow.setText("Your score is: " + score + "/10");
//        }
//    }
}

老实说,它看起来确实可行,但是没有用。

您只在视图加载时存储检查状态的值,并且永远不会更新它。

始终在点击侦听器中检查rb.isChecked() ,而不是在其外部存储布尔值。

它应该是:

if(rbChecked.isChecked()){
      score += 1;
      scoreShow.setText("Your score is: " + score + "/10");
}

您需要在用户单击“计算”按钮时检查单选按钮的状态。...您的代码应像这样...

calculate.setOnClickListener(new View.OnClickListener(){
        public void onClick (View v){
            rbChecked = rb.isChecked();
            if(rbChecked){
                score += 1;
                scoreShow.setText("Your score is: " + score + "/10");
            }
        }
    });

暂无
暂无

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

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