[英]Android checkbox check for null to avoid app crash
我是 android 工作室和 java 的新手。 我制作了一个简单的页面,其中包含复选框和一个按钮,用于在 toast 消息中打印出选中的复选框。 问题是如果我在没有选择任何内容的情况下单击提交按钮,应用程序将停止工作并崩溃。 我想要实现的是,如果我没有 select 任何东西并且我点击提交按钮它应该向我发送一条祝酒消息“请 select 一个盒子”而不会使应用程序崩溃。
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>
Java代码:
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();
}
});
}
}
我希望我必须检查提交按钮上的 null 值。
实现这一目标的代码是什么?
任何帮助将不胜感激。
谢谢!
根据您的问题,如果您在按钮单击事件中添加一些条件检查,则此代码将起作用。 检查更新的代码,您的 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 条件
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;
公共 class 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();
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.