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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android界面编程之利用单选框和复选框实现对学历和爱好进行选择

發布時間:2023/12/14 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android界面编程之利用单选框和复选框实现对学历和爱好进行选择 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android界面編程之利用單選框和復選框實現對學歷和愛好進行選擇

首先我們要了解一下單選框和復選框:

單選框(Radio Button):當用戶選擇某單選按鈕時,同一組中的其他單選按鈕不能同時選定,通常進行單選的按鈕放到一個RadioGroup中

復選框(Check Box):可以選擇任意數目,沒有必要放到一個組中

定位被選中的按鈕
單選框哪個按鈕被選中時,需要在組中進行選擇,復選框則不需要,所以單選框要對組進行監聽,而復選框可以對個體進行監聽,也可以創建一個數組用來循環掃描是否選中。

我使用數組來循環掃面,就不可以創建全局的變量了,不過寫起來簡單些,復制粘貼,出于代碼的規范性最好選擇不要創建數組,可以創建幾個全局變量,然后自定義一個掃描的方法,本文就不一一掩飾了

下面看代碼:


布局:采用Tablelayout布局

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"><TableRow android:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="學歷"android:textSize="20sp"/><RadioGroup android:id="@+id/radioGroup1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:orientation="horizontal"><RadioButton android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:id="@+id/small"android:text="小學"/><RadioButton android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/midle"android:text="中學"/><RadioButton android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/large"android:text="大學"/><RadioButton android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/secises"android:text="研究生"/></RadioGroup></TableRow><TableRow android:id="@+id/tableRow2"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="愛好:" /><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><CheckBox android:id="@+id/run"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跑步" /><CheckBox android:id="@+id/swim"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="游泳" /><CheckBox android:id="@+id/play"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="打球" /><CheckBox android:id="@+id/read"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="讀書" /></LinearLayout></TableRow><TextView android:id="@+id/info"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </TableLayout>

MainActivity

public class MainActivity extends AppCompatActivity {String stu;String hob;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tablelayout);final TextView info =(TextView)findViewById(R.id.info);RadioGroup radioGroup =(RadioGroup)findViewById(R.id.radioGroup1);final CheckBox checkBox[] = new CheckBox[4];checkBox[0] = (CheckBox)findViewById(R.id.swim);checkBox[1] = (CheckBox)findViewById(R.id.run);checkBox[2] = (CheckBox)findViewById(R.id.read);checkBox[3] = (CheckBox)findViewById(R.id.play);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {if (i == R.id.small)stu="您的學歷是小學";else if (i ==R.id.midle)stu="您的學歷是中學";else if (i ==R.id.large)stu="您的學歷是大學";else if (i==R.id.secises)stu="您的學歷是研究生";info.setText(print());}});checkBox[0].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的愛好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});checkBox[1].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的愛好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});checkBox[2].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的愛好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});checkBox[3].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的愛好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});}public String print(){String str = null;if(stu == null && hob == null){str = "請選擇您的學歷和愛好" ;}else if (stu != null && hob == null){str = stu ;}else if (stu == null && hob != null){str = hob ;}else {str = stu + "\n" + hob;}return str;}}

實現效果: 每次更改單選框或復選框都會更新提示

總結

以上是生活随笔為你收集整理的Android界面编程之利用单选框和复选框实现对学历和爱好进行选择的全部內容,希望文章能夠幫你解決所遇到的問題。

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