【问题标题】:Dont know where to place CheckBox Code in MainActivity.java不知道在 MainActivity.java 中放置 CheckBox 代码的位置
【发布时间】:2018-04-26 16:50:27
【问题描述】:

我正在开发一个测验应用程序并遇到了一个基本问题,我想实现一个复选框,如果两个答案都正确,分数应该会更新,我不知道将代码放在 java 中的哪个位置。

基本上这个app应该是这样工作的,一共5题,每题2分,3题MCQ,其中一个是正确的,第4题两个答案都是正确的,所以我们添加checkbox,每个checkbox加1分,然后editText字段,以便用户可以添加自己的 cmets,在用户回答完所有问题后,然后,只有在单击提交按钮后,才会将分数呈现给用户!
谢谢

我想知道如何简单地使用复选框来更新分数

此处为 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>

【问题讨论】:

  • 您是否要检查是否仅在单击提交按钮时才选中复选框?
  • 是的,但是有没有简化的东西,我处于非常基础的水平,我们可以定义复选框变量,然后在oncreate中使用findviewbyid将复选框连接到代码然后创建一个我们更新分数的简单点击方法?应用程序运行良好,直到问题 3,关于提交按钮,目前分数已在提交按钮下方实时更新,现在很好,我只需要复选框的帮助并编辑文本以获取用户的输入
  • 在下面查看我的答案。

标签: android checkbox android-checkbox main-activity


【解决方案1】:

如果您只想在单击提交按钮后计算分数,那么只需将您的 isChecked() 代码移动到您的 submitOrder 方法中,这样只有在用户单击提交按钮后,我们才会检查复选框是否被选中.

如果您想实时更新分数,请按照 Rahul Chaudhary 的回答,一旦选中两个复选框,就禁用两者。

【讨论】:

    【解决方案2】:

    当您按下提交按钮时,将复选框中的所有选中值和 使用验证

    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)
       }
        }
    

    )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      相关资源
      最近更新 更多