安卓CheckBox实现单选
生活随笔
收集整理的這篇文章主要介紹了
安卓CheckBox实现单选
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
開發(fā)工具:Android studio
jdk:1.8
安卓谷歌12
1.activity_main.xml:
首先設(shè)置一個LinearLayout,里面添加子控件然后和另一個用來顯示選擇結(jié)果的,最后來個button。
效果預(yù)覽圖:代碼:
2.MainActivity.java
先獲取每一個控件
CheckBox man, woman; Button button; TextView msg ;監(jiān)聽選擇狀態(tài)的方法:
private void initView() {man = (CheckBox) findViewById(R.id.man);woman = (CheckBox) findViewById(R.id.woman);man.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {man.setChecked(true);woman.setChecked(false);} else {man.setChecked(false);}}});woman.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {woman.setChecked(true);man.setChecked(false);} else {woman.setChecked(false);}}});onCreate:button監(jiān)聽獲取到checkbox
initView();final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);button = (Button) findViewById(R.id.button);msg = (TextView)findViewById(R.id.msg);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {new AlertDialog.Builder(MainActivity.this).setTitle("提示信息").setMessage("確定選擇?").setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {StringBuilder sex = new StringBuilder();sex.append("性別為:");if (man.isChecked()) {sex.append(man.getText().toString() + " ");} else if (woman.isChecked()) {sex.append(woman.getText().toString() + " ");} else {msg.setText("未選擇性別。\n");}msg.setText(sex);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this,"取消選擇",Toast.LENGTH_LONG).show();}}).create().show();}});3.MainActivity.java全代碼
package edu.zut.mysqltest;import androidx.appcompat.app.AppCompatActivity;import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends AppCompatActivity {CheckBox man, woman;Button button;TextView msg ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);button = (Button) findViewById(R.id.button);msg = (TextView)findViewById(R.id.msg);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {new AlertDialog.Builder(MainActivity.this).setTitle("提示信息").setMessage("確定選擇?").setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {StringBuilder sex = new StringBuilder();sex.append("性別為:");if (man.isChecked()) {sex.append(man.getText().toString() + " ");} else if (woman.isChecked()) {sex.append(woman.getText().toString() + " ");} else {msg.setText("未選擇性別。\n");}msg.setText(sex);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this,"取消選擇",Toast.LENGTH_LONG).show();}}).create().show();}});}private void initView() {man = (CheckBox) findViewById(R.id.man);woman = (CheckBox) findViewById(R.id.woman);//監(jiān)聽事件man.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {man.setChecked(true);woman.setChecked(false);} else {man.setChecked(false);}}});woman.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {woman.setChecked(true);man.setChecked(false);} else {woman.setChecked(false);}}});} }最后效果圖:gif:
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的安卓CheckBox实现单选的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Miniconda管理多版本python
- 下一篇: SpringBoot+AOP实现多数据源