日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓CheckBox实现单选

發布時間:2024/10/5 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓CheckBox实现单选 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開發工具:Android studio
jdk:1.8
安卓谷歌12

1.activity_main.xml:

首先設置一個LinearLayout,里面添加子控件然后和另一個用來顯示選擇結果的,最后來個button。
效果預覽圖:代碼:

<LinearLayoutandroid:id="@+id/layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="=>選擇你的性別:"android:textColor="@color/purple_200" /><CheckBoxandroid:id="@+id/man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="" /><CheckBoxandroid:id="@+id/woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="" /><TextViewandroid:id="@+id/msg"android:layout_width="match_parent"android:layout_height="match_parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="選擇" /></LinearLayout>

2.MainActivity.java

先獲取每一個控件

CheckBox man, woman; Button button; TextView msg ;

監聽選擇狀態的方法:

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監聽獲取到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);//監聽事件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位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的安卓CheckBox实现单选的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。