Android 不知道在MainActivity.java中放置复选框代码的位置
我正在开发一个测验应用程序,遇到了一个基本问题,我想实现一个复选框,如果两个答案都正确,分数应该得到更新,我不知道java中的代码应该放在哪里 基本上,这就是应用程序应该如何工作,总共5个问题,每个都有2分,3个MCQ,其中一个是正确的,第4个问题两个答案都是正确的,所以我们添加了复选框,每个复选框都有一个标记,然后是editText字段,这样用户可以添加自己的评论,在用户回答完所有问题后,然后并且只有那时,点击提交按钮后,分数应显示给用户Android 不知道在MainActivity.java中放置复选框代码的位置,android,checkbox,android-checkbox,main-activity,Android,Checkbox,Android Checkbox,Main Activity,我正在开发一个测验应用程序,遇到了一个基本问题,我想实现一个复选框,如果两个答案都正确,分数应该得到更新,我不知道java中的代码应该放在哪里 基本上,这就是应用程序应该如何工作,总共5个问题,每个都有2分,3个MCQ,其中一个是正确的,第4个问题两个答案都是正确的,所以我们添加了复选框,每个复选框都有一个标记,然后是editText字段,这样用户可以添加自己的评论,在用户回答完所有问题后,然后并且只有那时,点击提交按钮后,分数应显示给用户 多谢各位 我想知道如何简单地使用复选框来更新分数 M
多谢各位 我想知道如何简单地使用复选框来更新分数 MainActivity.java文件在此
package com.example.android.quizatlast;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int Totalscore = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toasts , onclick , and distroying buttons for next time
final Button buttononeone = (Button) findViewById(R.id.Button1_1);
final Button buttononetwo = (Button) findViewById(R.id.Button1_2);
buttononeone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int score = 2;
Totalscore = Totalscore + score;
Score(Totalscore);
buttononeone.setEnabled(false);
buttononetwo.setEnabled(false);
}
});
buttononetwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence text = "Ahh,Wait for the score";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
buttononetwo.setEnabled(false);
buttononeone.setEnabled(false);
}
});
final Button buttontwoone = (Button) findViewById(R.id.Button2_1);
final Button buttontwotwo = (Button) findViewById(R.id.Button2_2);
buttontwoone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence text = "Dehli,City you should must visit";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
buttontwoone.setEnabled(false);
buttontwotwo.setEnabled(false);
}
});
buttontwotwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int score = 2;
Totalscore = Totalscore + score;
Score(Totalscore);
Context context = getApplicationContext();
CharSequence text = "Ahh,mumbai is a beautiful place";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
buttontwotwo.setEnabled(false);
buttontwoone.setEnabled(false);
}
});
final Button buttonthreeone = (Button) findViewById(R.id.Button3_1);
final Button buttonthreetwo = (Button) findViewById(R.id.Button3_2);
buttonthreeone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int score = 2;
Totalscore = Totalscore + score;
Score(Totalscore);
Context context = getApplicationContext();
CharSequence text = "Agra , wonders are waiting there for you";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
buttonthreeone.setEnabled(false);
buttonthreetwo.setEnabled(false);
}
});
//PROBLEM HERE
CheckBox checkBoxfourone = (CheckBox) findViewById(R.id.Checkbox4_1);
boolean check4_1 = checkBoxfourone.isChecked();
CheckBox checkBoxfourtwo = (CheckBox) findViewById(R.id.Checkbox4_2);
boolean check4_2 = checkBoxfourtwo.isChecked();
Score(Totalscore);
}
private int checkscore(boolean check4_1,boolean check4_2) {
int score = Totalscore;
if (check4_1) {
Totalscore = Totalscore + 1;
}
if (check4_2) {
Totalscore = Totalscore + 1;
}
return Totalscore;
}
//TILL HERE
public void Score(int score) {
TextView scoreView = (TextView) findViewById(R.id.score);
scoreView.setText(String.valueOf(score));
}public void reset(View view){
Score(Totalscore = 0);
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
tools:context="com.example.android.justjava.MainActivity">
<LinearLayout
android:id="@+id/topping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/Name_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textCapCharacters" />
<EditText
android:id="@+id/Email_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textCapCharacters" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Toppings"
android:textSize="16sp"
android:textAllCaps="true"/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whipped Cream"
android:paddingLeft="24dp"
android:textSize="16sp"
android:layout_marginBottom="8dp"/>
<CheckBox
android:id="@+id/chocolate_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chocolate"
android:paddingLeft="24dp"
android:textSize="16sp"
android:layout_marginBottom="8dp"/>
</LinearLayout>
<TextView
android:id="@+id/text_view"
android:layout_below="@id/topping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="quantity!"
android:textAllCaps="true"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/linear"
android:layout_below="@id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
> <Button
android:onClick="decrement" android:id="@+id/decrement"
android:layout_below="@id/quantity_text_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:layout_marginBottom="8dp"/>
<TextView
android:layout_marginLeft="8dp"
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_below="@id/increment"
android:text="0"
android:textColor="#000"
android:layout_marginRight="8dp"
android:textSize="24sp" />
<Button
android:onClick="increment"
android:id="@+id/increment"
android:layout_below="@id/text_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:layout_marginBottom="8dp"/>
</LinearLayout>
<TextView
android:id="@+id/order_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/linear"
android:layout_marginBottom="8dp"
android:text="order summary"
android:textAllCaps="true"
android:textSize="16sp" />
<TextView
android:id="@+id/order_summary_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/order_summary"
android:layout_marginBottom="8dp"
android:textSize="24sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/order_summary_text_view"
android:onClick="submitOrder"
android:text="order" />
</LinearLayout>
</ScrollView>
如果您只想在单击“提交”按钮后计算分数,则只需将您的
isChecked()
代码移动到您的submitor
方法中,这样,只有在用户单击“提交”按钮后,我们才会检查是否选中了复选框
如果您想实时更新分数,请遵循Rahul Chaudhary的答案,并在选中两个复选框后禁用这两个复选框。如果您只想在单击提交按钮后计算分数,则只需移动您的
isChecked()
将代码编码到您的提交者
方法中,以便只有在用户单击提交按钮后,我们才会检查是否选中复选框
如果您想实时更新分数,请遵循Rahul Chaudhary的答案,并在选中两个复选框后禁用这两个复选框。当您按下提交按钮时,从复选框和 使用验证
checkValidation(){
if(checkbox1.equal(correct anwser1) && checkbox2.equal(correct anwser2)
)
{
return true
}else{
return false
}}
并在提交时签入
submit.setOnclick(new.....
onclick(){
if( checkValidation()){
Toast(answer is right)
}else{
Toast(answer is wrong)
}
}
)当您按下提交按钮时,从复选框和 使用验证
checkValidation(){
if(checkbox1.equal(correct anwser1) && checkbox2.equal(correct anwser2)
)
{
return true
}else{
return false
}}
并在提交时签入
submit.setOnclick(new.....
onclick(){
if( checkValidation()){
Toast(answer is right)
}else{
Toast(answer is wrong)
}
}
)是否要检查是否只有在单击“提交”按钮时才选中复选框?是的,但是否要简化这些内容,我处于非常基本的级别,我们是否可以定义复选框变量,然后在oncreate中使用findviewbyid将复选框连接到代码,然后创建一个简单的点击方法,在其中更新分数?在问题3“提交按钮”之前,应用程序运行良好,目前成绩在提交按钮下方实时更新,目前还可以,我只需要复选框的帮助和编辑文本以从用户获取输入检查我的答案。是否要检查是否仅在单击提交按钮时才选中复选框?是,但是,是否有任何方法可以简化这些内容,我处于非常基本的级别,我们可以定义复选框变量,然后在oncreate中使用findviewbyid将复选框连接到代码,然后创建一个简单的单击方法来更新分数吗?应用程序在第3个问题之前运行良好,关于提交按钮,目前分数在提交按钮下实时更新,现在还可以,我只需要复选框的帮助和编辑文本以从用户获取输入检查我下面的答案。