首页 > 解决方案 > 如何修复java代码以将结果显示为名称和分数

问题描述

我已经为 4 个问题提供了 xml 和 java 代码。我在其中工作,每个问题都有 4 个单选按钮,在 javam 中,如果用户选择正确的答案,它必须在最后添加分数。结果必须显示名称和分数,但我似乎无法从那里继续

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFF00"
    android:orientation="vertical">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:inputType="textPersonName"
        android:text="@string/name"
        android:importantForAutofill="no"
        android:hint="@string/name"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="130dp"
        android:layout_marginLeft="130dp"
        android:layout_marginTop="22dp"
        android:text="@string/Question1"
        android:textAllCaps="true"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="20dp"
        android:text="@string/firstQuestion"
        android:textSize="16sp" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/GQ11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ11" />

        <RadioButton
            android:id="@+id/GQ12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ12" />

        <RadioButton
            android:id="@+id/GQ13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ13" />

        <RadioButton
            android:id="@+id/GQ14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ14" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="130dp"
        android:layout_marginLeft="130dp"
        android:layout_marginTop="22dp"
        android:text="@string/Questuin2"
        android:textAllCaps="true"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="20dp"
        android:text="@string/secondQuestion"
        android:textSize="16sp" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/GQ21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ21" />

        <RadioButton
            android:id="@+id/GQ22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ22" />

        <RadioButton
            android:id="@+id/GQ23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ23" />

        <RadioButton
            android:id="@+id/GQ24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ24" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="130dp"
        android:layout_marginLeft="130dp"
        android:layout_marginTop="22dp"
        android:text="@string/Question3"
        android:textAllCaps="true"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="20dp"
        android:text="@string/thirdQuestion"
        android:textSize="16sp" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/GQ31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ31" />

        <RadioButton
            android:id="@+id/GQ32"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ32" />

        <RadioButton
            android:id="@+id/GQ33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ33" />

        <RadioButton
            android:id="@+id/GQ34"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ34" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="130dp"
        android:layout_marginLeft="130dp"
        android:layout_marginTop="22dp"
        android:text="@string/Question4"
        android:textAllCaps="true"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="20dp"
        android:text="@string/fourthQuestion"
        android:textSize="16sp" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/GQ41"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ41" />

        <RadioButton
            android:id="@+id/GQ42"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ42" />

        <RadioButton
            android:id="@+id/GQ43"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ43" />

        <RadioButton
            android:id="@+id/GQ44"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="@string/GQ44" />
    </RadioGroup>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/end"
        android:textSize="16sp"
        android:padding="15dp"
        android:layout_marginTop="22dp"
        android:id="@+id/submit"
        android:onClick="result"/>


</LinearLayout>

Java 类:

public class MainActivity extends AppCompatActivity {
    int score = 0;

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

    }

    public int answersQ1(View view) {
        RadioButton aq1 = (RadioButton) findViewById(R.id.GQ11);
        RadioButton aq2 = (RadioButton) findViewById(R.id.GQ12);
        RadioButton aq3 = (RadioButton) findViewById(R.id.GQ13);
        RadioButton aq4 = (RadioButton) findViewById(R.id.GQ14);
        if (aq1.isChecked()) {
            score = score + 1;
        } else if (aq2.isChecked() && (aq3.isChecked() && (aq4.isChecked()))) {
            score = score + 0;
        }
        return score;

    }

    public int answersQ2(View view) {
        RadioButton ab1 = (RadioButton) findViewById(R.id.GQ21);
        RadioButton ab2 = (RadioButton) findViewById(R.id.GQ22);
        RadioButton ab3 = (RadioButton) findViewById(R.id.GQ23);
        RadioButton ab4 = (RadioButton) findViewById(R.id.GQ24);
        if (ab2.isChecked()) {
            score = score + 1;
        } else if (ab1.isChecked() && (ab3.isChecked() && (ab4.isChecked()))) {
            score = score + 0;
        }
        return score;
    }

    public int answersQ3(View view) {
        RadioButton ac1 = (RadioButton) findViewById(R.id.GQ31);
        RadioButton ac2 = (RadioButton) findViewById(R.id.GQ32);
        RadioButton ac3 = (RadioButton) findViewById(R.id.GQ33);
        RadioButton ac4 = (RadioButton) findViewById(R.id.GQ34);
        if (ac2.isChecked()) {
            score = score + 1;
        } else if (ac1.isChecked() && (ac3.isChecked() && (ac4.isChecked()))) {
            score = score + 0;
        }
        return score;
    }

    public int answersQ4(View view) {
        RadioButton ad1 = (RadioButton) findViewById(R.id.GQ41);
        RadioButton ad2 = (RadioButton) findViewById(R.id.GQ42);
        RadioButton ad3 = (RadioButton) findViewById(R.id.GQ43);
        RadioButton ad4 = (RadioButton) findViewById(R.id.GQ44);
        if (ad1.isChecked()) {
            score = score + 1;
        } else if (ad2.isChecked() && (ad3.isChecked() && (ad4.isChecked()))) {
            score = score + 0;
        }
        return score;

    }

    public void result (View view){
     // userName
        EditText nameField = (EditText) findViewById(R.id.name);
        Editable nameEditable = nameField.getText();
        String name = nameEditable.toString();
        Toast.makeText(getApplicationContext(), name , Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), score, Toast.LENGTH_SHORT).show();
    }


}

当我按下按钮提交应用程序崩溃。

标签: javaandroidmethods

解决方案


您的应用程序崩溃是因为您没有 onClickListener。您需要一个来处理按钮单击。它应该看起来像这样:

Button submitButton = (Button) findViewById(R.id.submit);
clickButton.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO
            }
        });

在 onClick 方法中,您要检查答案以及单击提交按钮后您希望发生的任何事情。此外,由于您的 LinearLayout 很长,我建议您添加一个 ScrollView。


推荐阅读