我在Android中的TextView没有更新

用户名

尽管我已经在该网站上检查了相关问题的解决方案,但仍无法解决我的问题。我正在尝试构建一个测验应用程序,该应用程序采用正确的答案,将分数加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()在click侦听器内部进行检查,而不是在其外部存储布尔值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

android TextView没有换行

在TextView的内容没有显示(Android Studio中)

TextView没有显示在nestedscrollview android中的RecyclerView上方

为什么我的 Android 应用程序中按钮上的文字没有更新?

为什么我的窗口没有从我的线程中更新?

我的 android studio 没有从 3.5 更新到 3.6 版本

我的android主屏幕小部件出现了,但没有更新

在Android Studio中更新textView

在android中更新应用程序后,我的资产文件夹中的图像没有被替换

为什么我的代码没有在Entity Framework中更新?

为什么我的样式没有在React Native中更新?

我没有提交,并且状态在Vuex中已更新

我在网格中的数据没有更新

为什么我的绑定没有在 XAML 中更新

为什么我的字典没有在python中更新

在bokeh中更新我的滑块时没有任何变化

为什么 JSON 更新没有显示在我的网页中?

Vue.js 中的 Eventbus 没有更新我的组件

为什么我的 Python 程序中的时间没有更新?

我的MediatorLiveData没有更新

我正在尝试在 android 的 sql lite 数据库中更新玩家的分数,但没有发生

TextView没有换行特殊字符Android

Android TextView没有显示正确的字符

TextView没有显示在Android设备上?

为什么我的用户名没有出现在 TextView 中?

ListActivity中的Android-ListView没有更新

为什么在 Android Studio 中单击没有 android:onClick 的 TextView 时会触发 OnClick 事件?

Android行没有更新

我从截击响应中更新textview的方法是否是“ Android方式”的正确方法?