【问题标题】:How reset the button keep the score in android重置按钮如何在android中保持分数
【发布时间】:2016-07-13 13:43:00
【问题描述】:

我尝试创建一个简单的测验应用程序,我需要一个重置按钮。所以,当用户点击重置按钮时,分数应该为 0。但是我的代码出了点问题,无法弄清楚。 感谢您的帮助!

MainActivity.java

public class MainActivity extends AppCompatActivity {

    String Name;
    int score = 0;
    int resetScore = 0;
    Button submitButton;

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

        submitButton = (Button) findViewById(R.id.submitAnswer);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitButton.setText("Your score is:" + score);
            }
        });
    }

    private void displayResult(int score) {
        TextView result = (TextView)findViewById(R.id.score);
        result.setText(String.valueOf(score));
    }

    public void resetScore(View view){
        resetScore= 0;
        displayResetScore(resetScore);
    }

    private void displayResetScore(int resetScore) {
        TextView ResetButton = (TextView) findViewById(R.id.ResetButton);
        ResetButton.setText(String.valueOf(resetScore));
    }
}

activity_main.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/nameField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:inputType="text"
            android:hint="Name"
            android:textColor="#EF6C00"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/welcomeMessage"
            style="@style/WelcomeScreenText"
            android:fontFamily="sans-serif-light"
            android:text="Welcome to Hungry For History!\n         Let's get started!"/>

        <TextView
            android:id="@+id/firstQuestion"
            style="@style/QuestionsStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/welcomeMessage"
            android:paddingLeft="5dp"
            android:text="Who was born in Ancient City Stagira, Greece?"/>

        <CheckBox
            android:id="@+id/firstLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/firstQuestion"
            android:text="Aristotle"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/firstRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/firstQuestion"
            android:layout_toRightOf="@+id/firstLeftCheckBox"
            android:text="Pythagoras"
            android:onClick="onCheckboxClicked"/>
        <TextView
            android:id="@+id/secondQuestion"
            style="@style/QuestionsStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/firstLeftCheckBox"
            android:paddingLeft="5dp"
            android:text="Who said in his last speech:With malice toward none;...let us strive on to finish the work we are in;to bind up the nation's wounds;into care for him who shall have borne the battle and for his widow and his orphans?"/>

        <CheckBox
            android:id="@+id/secondLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/secondQuestion"
            android:text="William McKinley"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/secondRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/secondQuestion"
            android:layout_toRightOf="@+id/secondLeftCheckBox"
            android:text="Abraham Lincoln"
            android:onClick="onCheckboxClicked"/>


        <TextView
            android:id="@+id/thirdQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/secondLeftCheckBox"
            android:paddingLeft="5dp"
            android:text="Where the An Lushan Rebellion took place?"/>

        <CheckBox
            android:id="@+id/thirdLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/thirdQuestion"
            android:text="China"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/thirdRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/thirdQuestion"
            android:layout_toRightOf="@+id/thirdLeftCheckBox"
            android:text="Thailand"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/fourthQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/thirdLeftCheckBox"
            android:text="Who was the most famous exemplar of absolute monarchy in France?"/>

        <CheckBox
            android:id="@+id/fourthLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fourthQuestion"
            android:text="Louis XIV"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/fourthRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fourthQuestion"
            android:layout_toRightOf="@+id/fourthLeftCheckBox"
            android:text="Michael I"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/fifthQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/fourthLeftCheckBox"
            android:text="When Alexander The Great lived?"/>

        <CheckBox
            android:id="@+id/fifthLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fifthQuestion"
            android:text="330-323 BC"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/fifthRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fifthQuestion"
            android:layout_toRightOf="@+id/fifthLeftCheckBox"
            android:text="336-323 BC"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/sixthQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/fifthLeftCheckBox"
            android:text="Where Albert Einstein studied?"/>

        <CheckBox
            android:id="@+id/sixthLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/sixthQuestion"
            android:text="University of Zurich"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/sixthRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/sixthQuestion"
            android:layout_toRightOf="@+id/sixthLeftCheckBox"
            android:text="University of Germany"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/seventhQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/sixthLeftCheckBox"
            android:text="What was the main interest of Democritus?"/>

        <RadioGroup
            android:id="@+id/radioButtons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/seventhQuestion"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/astronomyRadioButton"
                style="@style/CheckBoxStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mathematics-Astronomy"
                android:onClick="onRadioButtonClicked"/>

            <RadioButton
                android:id="@+id/philosophyRadioButton"
                style="@style/CheckBoxStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Philosophy-Psychology"
                android:onClick="onRadioButtonClicked"/>
        </RadioGroup>

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>


        <Button
            android:id="@+id/submitAnswer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioButtons"
            android:layout_marginBottom="3dp"
            android:background="@color/backgroundColor"
            android:text="Submit"
            android:textColor="@color/textColor"
            android:onClick="OnClickSubmit"/>

        <Button
            android:id="@+id/ResetButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/submitAnswer"
            android:layout_marginBottom="3dp"
            android:onClick="OnClickReset"
            android:background="@color/backgroundResetColor"
            android:textColor="@color/textColor"
            android:text="Reset"
            android:textAllCaps="true"/>

    </RelativeLayout>

</ScrollView>

【问题讨论】:

  • android:onClick="OnClickReset" in xml 应该是android:onClick="resetScore" 或者你的Java函数应该被称为OnClickReset()。他们只需要同名即可。不知道你从哪里得到这个语法。
  • resetScore 始终为 0,创建一个永远不会改变其值的变量是没有意义的。
  • 您好 codeMagic,我按照您的建议做了,但单击重置按钮时应用程序崩溃。我将发布我的日志以查看:
  • 嗨 Juan,我不是很了解你,如果我只是删除 resetScore 的值没有任何变化,点击 resetButton 时应用程序再次崩溃,我应该怎么做才能使重置按钮起作用?

标签: java android


【解决方案1】:

如果您在游戏过程中添加分数时使用'score'变量,则按下按钮时必须将score设置为0,而不是'resetScore'。

public void resetScore(View view){
    score = 0;
    displayResetScore(score);
}

 private void displayResetScore(int resetScore) {
    ResetButton.setText(String.valueOf(score));
}

阅读您的错误日志,您也可以尝试像这样声明 textview:

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

    submitButton = (Button) findViewById(R.id.submitAnswer);
    TextView ResetButton = (TextView) findViewById(R.id.ResetButton);

    submitButton.setOnClickListener(new View.OnClickListener() {
    ResetButton.setOnClickListener(new View.OnClickListener() {

【讨论】:

    【解决方案2】:

    在您的 MainActivity.onCreate() 方法中,您为 submitButton 添加了 OnClicklistener。对 resetButton 执行相同的操作。请将该按钮重命名为小写“resetButton”。

    【讨论】:

    【解决方案3】:

    最后,resetButton 起作用了。这是我来自 MainActivity.java 的代码

    public class MainActivity extends AppCompatActivity {
    
        String Name;
        int score = 0;
        Button submitButton;
        Button resetButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
             EditText nameField = (EditText)findViewById(R.id.nameField);
             String name = nameField.getText().toString();
    
            submitButton = (Button) findViewById(R.id.submitButton);
            submitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    submitButton.setText("Your score is:" + score);
                    //submitButton.setText(String.valueOf(score));
                }
            });
            resetButton = (Button) findViewById(R.id.resetButton);
            resetButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    submitButton.setText((String.valueOf(0)));
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2011-06-07
      相关资源
      最近更新 更多