我正在开发一个测验应用并遇到一个基本问题,我想实现一个复选框,如果两个答案都正确,分数应该更新,我不知道将代码放在java中的哪个位置。< / p>
基本上这是应用程序应该如何工作,总问题5,每个带2个标记,3个MCQ,其中一个是正确的,第4个问题,两个答案都是正确的,所以我们添加复选框,每个复选框带一个标记,然后是editText字段,以便用户可以在用户回答完所有问题后添加自己的评论,然后只有在点击提交按钮后,才会向用户显示分数!
谢谢
我想知道如何使用复选框更新分数
此处的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);
}
}
activity_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>
答案 0 :(得分:2)
如果您只想在点击提交按钮后计算得分,只需将isChecked()
代码移到您的submitOrder
方法中,这样只有在用户点击提交按钮后,我们才会检查是否有复选框是否被选中。
如果您想实时更新分数,请按照Rahul Chaudhary的回答,选中两个复选框后禁用两者。
答案 1 :(得分:1)
将这段代码放在复选框初始化
下面checkBoxfourone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
} );
和第二个复选框相同或实现CompoundButton.OnCheckedChangeListener,你的MainActivity看起来像这样
class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//你的旧代码在这里
checkBoxfourtwo.setOnCheckedChangeListener(this);
checkBoxfourone.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
// to do here
}
}
}
答案 2 :(得分:0)
当您按下提交按钮时,从复选框中选中所有选中的值 使用验证
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)
}
}
)