Android 复选框检查空以避免应用崩溃



我是android工作室和java的新手。我制作了一个简单的页面,其中包含复选框,以及一个按钮,用于在 Toast 消息中打印出选中的复选框。 问题是如果我单击提交按钮而没有选择任何内容,应用程序将停止工作并崩溃。 我想要实现的是,如果我没有选择任何东西并且我单击提交按钮,它应该向我发送一条 Toast 消息"请选择一个框"而不会使应用程序崩溃。

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:text="Favorite Courses: "
    android:textSize="20sp"
    android:layout_marginTop="30dp"
    android:layout_below="@+id/txDate"
    android:id="@+id/fvCourses"/>
<CheckBox
    android:id="@+id/cbCs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/fvCourses"
    android:layout_alignBottom="@+id/fvCourses"
    android:text="Computer Science" />
<CheckBox
    android:id="@+id/cbAcc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/fvCourses"
    android:layout_below="@+id/cbCs"
    android:text="Accounting" />
<CheckBox
    android:id="@+id/cbGraphic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/fvCourses"
    android:layout_below="@+id/cbAcc"
    android:text="Graphic Designer" />
<Button
    android:id="@+id/btnapply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="330dp"
    android:text="Confirm" />
    </RelativeLayout>

爪哇代码:

package test.cb.app;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
     TextView textView;
    CheckBox cbCs,cbAcc,cbGraphic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cbCs = findViewById(R.id.cbCs);
        cbAcc = findViewById(R.id.cbAcc);
        cbGraphic = findViewById(R.id.cbGraphic);
        Button btnapply = findViewById(R.id.btnapply);
        btnapply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuffer result = new StringBuffer();
                result.append("Computer Science check : ").append(cbCs.isChecked());
                result.append("nAccounting check : ").append(cbAcc.isChecked());
                result.append("nGraphic check :").append(cbGraphic.isChecked());
                Toast.makeText(MainActivity.this, result.toString(),
                        Toast.LENGTH_LONG).show();
            }
        });
        }
            }

我希望我必须在提交按钮上检查空值。

实现这一目标的代码是什么?

任何帮助都将得到极大的赞赏。

谢谢!

根据您的问题,如果您在按钮单击事件中添加一些条件检查,则此代码将起作用。 检查代码是否已更新,您的XML文件中也有一些不需要的ID。

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;
public class CheckActivity extends AppCompatActivity {
    TextView textView;
    CheckBox cbCs,cbAcc,cbGraphic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checks);
        cbCs = findViewById(R.id.cbCs);
        cbAcc = findViewById(R.id.cbAcc);
        cbGraphic = findViewById(R.id.cbGraphic);
        Button btnapply = findViewById(R.id.btnapply);
        btnapply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if(!cbCs.isChecked() &&!cbAcc.isChecked() && !cbGraphic.isChecked() ){
                   Toast.makeText(CheckActivity.this, "Select Any",
                           Toast.LENGTH_LONG).show();
               }
                else{
                    StringBuffer result = new StringBuffer();
                result.append("Computer Science check : ").append(cbCs.isChecked());
                result.append("nAccounting check : ").append(cbAcc.isChecked());
                result.append("nGraphic check :").append(cbGraphic.isChecked());
                Toast.makeText(CheckActivity.this, result.toString(),
                        Toast.LENGTH_LONG).show();
            }}
        });
    }
}

此外,您还可以对 XML 页面进行以下更改。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Favorite Courses: "
        android:textSize="20sp"
        android:layout_marginTop="30dp"
        android:id="@+id/fvCourses"/>
    <CheckBox
        android:id="@+id/cbCs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/fvCourses"
        android:layout_alignBottom="@+id/fvCourses"
        android:text="Computer Science" />
    <CheckBox
        android:id="@+id/cbAcc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/fvCourses"
        android:layout_below="@+id/cbCs"
        android:text="Accounting" />
    <CheckBox
        android:id="@+id/cbGraphic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/fvCourses"
        android:layout_below="@+id/cbAcc"
        android:text="Graphic Designer" />
    <Button
        android:id="@+id/btnapply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="330dp"
        android:text="Confirm" />
</RelativeLayout>

只是放入 if else 条件

包装 test.cb.app;

    import androidx.appcompat.app.AppCompatActivity;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.Button;
    import android.widget.CheckBox;

公共类 MainActivity 扩展 AppCompatActivity {

TextView textView;
CheckBox cbCs,cbAcc,cbGraphic;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cbCs = findViewById(R.id.cbCs);
    cbAcc = findViewById(R.id.cbAcc);
    cbGraphic = findViewById(R.id.cbGraphic);
    Button btnapply = findViewById(R.id.btnapply);
    btnapply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            StringBuffer result = new StringBuffer();
            result.append("Computer Science check : ").append(cbCs.isChecked());
            result.append("nAccounting check : ").append(cbAcc.isChecked());
            result.append("nGraphic check :").append(cbGraphic.isChecked());
            if(result.equals("")) {
                Toast.makeText(MainActivity.this, "no checkbox is selected",
                        Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(MainActivity.this, result.toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

最新更新