gpt4 book ai didi

java - Android 测验应用程序 - RadioGroup 和 CheckBox

转载 作者:行者123 更新时间:2023-12-01 09:28:50 25 4
gpt4 key购买 nike

我为测验应用程序编写了 XML。

将会有 (10) 个问题,一些 RadioGroup 和一些 CheckBox,但问题类型不会按任何顺序排列。所以第一个是单选组,第二个复选框,第三个、第四个和第五个单选组,第六个和第七个复选框,等等。

RadioGroup 显然只有一个正确答案,而 CheckBox 有两到三个正确答案。

我想创建一个“检查测验”按钮,该按钮在滚动测验的顶部显示一条消息,给出正确的数字,并在每个错误答案的前面显示一个红色的大红色“X”。

我不知道从哪里开始获取 MainActivity java 文件中的 Java 代码。

<小时/>
MainActivity.java


package com.example.android.quiztest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.TextView;

/**
* package com.example.android.quiztest;
* This app displays a radio button and checkbox quiz, and then grades the quiz, * displaying the score and identifying the incorrect answers.
**/

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.android.quiztest.R.layout.activity_main);
}
}
<小时/>

activity_main.xml

<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.example.android.quiztest.MainActivity"
android:fillViewport="false">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dimension_1"
android:orientation="vertical">


<EditText
android:id="@+id/name_text_view_STUDENT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/text_person_name"
android:inputType="textPersonName" />

<TextView
android:id="@+id/label_text_view_DIRECTIONS"
style="@style/HeaderTextStyle"
android:textSize="20sp"
android:layout_marginTop="@dimen/dimension_1"
android:text="@string/text_directions" />


<TextView
android:id="@+id/label_text_view_1"
style="@style/HeaderTextStyle"
android:layout_marginTop="@dimen/dimension_1"
android:text="@string/text_1" />

<RadioGroup
android:id="@+id/radioGroup_1"
style="@style/HeaderTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<RadioButton
android:id="@+id/radioButton_1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_1a"
android:textSize="@dimen/text_size_1" />

<RadioButton
android:id="@+id/radioButton_1b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_1b"
android:textSize="@dimen/text_size_1" />

<RadioButton
android:id="@+id/radioButton_1c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_1c"
android:textSize="@dimen/text_size_1" />

</RadioGroup>

<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="25dp"
android:background="#c0c0c0" />

<TextView
android:id="@+id/label_text_view_2"
style="@style/HeaderTextStyle"
android:layout_marginTop="@dimen/dimension_1"
android:text="@string/text_2" />



<CheckBox
android:id="@+id/checkBox_2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_2a"
android:textSize="@dimen/text_size_1" />

<CheckBox
android:id="@+id/checkBox_2b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_2b"
android:textSize="@dimen/text_size_1" />

<CheckBox
android:id="@+id/checkBox_2c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_2c"
android:textSize="@dimen/text_size_1" />



</LinearLayout>


</ScrollView>
<小时/>
strings.xml

<resources>

<string name="text_button_1">check answers</string>

<string name="app_name">Quiz Test</string>
<string name="text_person_name">Type student name here.</string>
<string name="text_directions">Select ALL correct answers:</string>

<string name="text_1">1) probability distribution</string>
<string name="text_1a">1a) A smooth curve indicating the frequency distribution for a discontinuous random variable.</string>
<string name="text_1b">1b) A discontinuous dot diagram showing the frequency distribution for a random variable.</string>
<string name="text_1c">1c) A smooth curve indicating the frequency distribution for a continuous random variable.</string>


<string name="text_2">2) normal distribution</string>
<string name="text_2a">2a) A smooth double-peak bell-shaped curve symmetrical about the mean.</string>
<string name="text_2b">2b) A smooth single-peak curve </string>
<string name="text_2c">3c) A bell-shaped curve symmetrical about the mean. </string>

</resources>

最佳答案

我更新了 XML,以提供在错误答案前面标记红色 X 的功能:

文件: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"
android:fillViewport="false"
tools:context=".MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dimension_1"
android:orientation="vertical">


<EditText
android:id="@+id/name_textView_STUDENT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/text_person_name"
android:inputType="textPersonName" />

<TextView
android:id="@+id/textView_directions"
style="@style/HeaderTextStyle"
android:layout_marginTop="@dimen/dimension_1"
android:text="@string/text_directions"
android:textSize="20sp" />


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dimension_1"
android:orientation="horizontal">

<TextView
android:id="@+id/incorrect_question_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dimension_1"
android:gravity="center"
android:text="X"
android:textColor="#ff0000"
android:textSize="30dp"
android:visibility="gone" />

<TextView
android:id="@+id/textView_1"
style="@style/HeaderTextStyle"
android:layout_marginTop="@dimen/dimension_1"
android:text="@string/text_1" />
</LinearLayout>


<RadioGroup
android:id="@+id/radioGroup_1"
style="@style/HeaderTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<RadioButton
android:id="@+id/radioButton_1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:textSize="@dimen/text_size_1"
android:text="@string/text_1a"
android:onClick="onClick_1a"/>


<RadioButton
android:id="@+id/radioButton_1b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_1b"
android:textSize="@dimen/text_size_1"
android:onClick="onClick_2a"/>/>

<RadioButton
android:id="@+id/radioButton_1c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimension_1"
android:paddingLeft="@dimen/dimension_4"
android:paddingRight="@dimen/dimension_5"
android:text="@string/text_1c"
android:textSize="@dimen/text_size_1"
android:onClick="onClick_3a"/>

</RadioGroup>
<小时/>

并为单选组和复选框开发了 Java 分级逻辑:

文件:MainActivity.java

package com.example.android.quiztest2;

/**
* *Below added my unique package name "com.example.android.justjava4"
*/

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;


/**
* package com.example.android.justjava4;
* This app displays an order form to order coffee, and displays the information.
* quantity is a global variable
**/
public class MainActivity extends AppCompatActivity {
private RadioButton radioButton_1a, radioButton_1b, radioButton_1c, radioButton_4a, radioButton_4b, radioButton_4c,
radioButton_5a, radioButton_5b, radioButton_5c, radioButton_6a, RadioButton_6b, radioButton_6c,
radioButton_7a, radioButton_7b, radioButton_7c, radioButton_9a, radioButton_9b, radioButton_9c;

private CheckBox checkBox_2a, checkBox_2b, checkBox_2c, checkBox_3a, checkBox_3b, checkBox_3c, checkBox_8a, checkBox_8b, checkBox_8c;

int grade = 0;

public void Score(int grade) {
if (radioButton_1c.isChecked()) grade++;

if (radioButton_4c.isChecked()) grade++;

if (radioButton_5c.isChecked()) grade++;

if (radioButton_6c.isChecked()) grade++;

if (radioButton_7c.isChecked()) grade++;

if (radioButton_9c.isChecked()) grade++;

if (checkBox_2a.isChecked() && !checkBox_2b.isChecked() && checkBox_2c.isChecked()) grade++;

if (checkBox_3a.isChecked() && checkBox_3b.isChecked() && checkBox_3c.isChecked()) grade++;

if (checkBox_8a.isChecked() && !checkBox_8b.isChecked() && checkBox_8c.isChecked()) grade++;

}


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


final Context currentContext = this;
Button button_grade_quiz = (Button)
findViewById(R.id.button_grade_quiz);
button_grade_quiz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {



boolean answerCorrect1 = false;

TextView incorrectQuestionOne = (TextView) findViewById(R.id.incorrect_question_1);

if (!answerCorrect1) {

incorrectQuestionOne.setVisibility(View.VISIBLE);
} else {

incorrectQuestionOne.setVisibility(View.GONE);

}

}
});
}
}
<小时/>

不确定如何将评分功能连接到其他 java 方法。

关于java - Android 测验应用程序 - RadioGroup 和 CheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39625072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com