Android開發(fā)之獲取單選與復(fù)選框的值操作示例
本文實例講述了Android開發(fā)之獲取單選與復(fù)選框的值操作。分享給大家供大家參考,具體如下:
效果圖:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性別"/> <!--定義一組單選按鈕--> <RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <!--定義兩個單選按鈕--> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/male" android:text="男" android:checked="false"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/female" android:text="女" android:checked="false"/> </RadioGroup> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜歡的顏色"/> <!--定義一個垂直線性布局--> <LinearLayout android:layout_gravity="center_horizontal" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--定義三個復(fù)選框--> <CheckBox android:id="@+id/color_red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="紅色"/> <CheckBox android:id="@+id/color_blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="藍(lán)色"/> <CheckBox android:id="@+id/color_green" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="綠色"/> </LinearLayout> </TableRow> <TextView android:id="@+id/show_sex" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示復(fù)選框內(nèi)容" android:textSize="20pt"/> <TextView android:id="@+id/show_color" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableLayout>
Java代碼:
public class Home extends AppCompatActivity { RadioGroup radioGroup01 ; TextView textView01 ; TextView textView02 ; Button button01 ; CheckBox checkBox01 ; CheckBox checkBox02 ; CheckBox checkBox03 ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//顯示manLayout //連接組建 radioGroup01 = (RadioGroup) findViewById(R.id.rg); textView01 = (TextView) findViewById(R.id.show_sex); textView02 = (TextView) findViewById(R.id.show_color); checkBox01 = (CheckBox) findViewById(R.id.color_red); checkBox02 = (CheckBox) findViewById(R.id.color_blue); checkBox03 = (CheckBox) findViewById(R.id.color_green); button01 = (Button) findViewById(R.id.show); //添加監(jiān)聽事件 radioGroup01.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //根據(jù)用戶勾選信息改變tip字符串的值 String tip = checkedId == R.id.male ? "您的性別為男" : "您的性別為n女" ; //修改show組件文本 textView01.setText(tip); } }); //輸出按鈕監(jiān)聽事件 button01.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView02.setText("喜歡的顏色: \n"); //篩選復(fù)選框信息 StringBuffer stringBuffer01 = new StringBuffer(); stringBuffer01.append(textView02.getText().toString()); if (checkBox01.isChecked()) { stringBuffer01.append("紅色\n"); } if (checkBox02.isChecked()) { stringBuffer01.append("藍(lán)色\n"); } if (checkBox03.isChecked()) { stringBuffer01.append("綠色"); } textView02.setText(stringBuffer01.toString()); } }); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
- android分享純圖片到QQ空間實現(xiàn)方式
- Android權(quán)限如何禁止以及友好提示用戶開通必要權(quán)限詳解
- Android開發(fā)中TextView各種常見使用方法小結(jié)
- 史上最全Android build.gradle配置詳解(小結(jié))
- Android開發(fā)實現(xiàn)的圓角按鈕、文字陰影按鈕效果示例
- Android開發(fā)實現(xiàn)Switch控件修改樣式功能示例【附源碼下載】
- Android開發(fā)實現(xiàn)的計時器功能示例
- Android開發(fā)之ListView的簡單用法及定制ListView界面操作示例
- Android文字基線Baseline算法的使用講解
- Android中Retrofit的簡要介紹
相關(guān)文章
解決Android加殼過程中mprotect調(diào)用失敗的原因分析
本文探討的主要內(nèi)容是mprotect調(diào)用失敗的根本原因,以及在加殼實現(xiàn)中的解決方案,通過本文的闡述,一方面能夠幫助遇到同類問題的小伙伴解決心中的疑惑,另一方面能夠給大家提供可落地的實現(xiàn)方案,需要的朋友可以參考下2022-01-01android 檢查網(wǎng)絡(luò)連接狀態(tài)實現(xiàn)步驟
android 如何檢查網(wǎng)絡(luò)連接狀態(tài),是android開發(fā)中一個常見的問題,本文將介紹如何實現(xiàn),需要的朋友可以參考下2012-12-12基于Fedora14下自帶jdk1.6版本 安裝jdk1.7不識別的解決方法
本篇文章是對Fedora14下自帶jdk1.6版本,安裝jdk1.7不識別的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android 選擇相冊照片并返回功能的實現(xiàn)代碼
這篇文章主要介紹了Android 從相冊中選擇照片并返回功能,需要的朋友可以參考下2018-03-03Jetpack Compose圖片組件使用實例詳細(xì)講解
在Compose中,圖片組件主要有兩種,分別是顯示圖標(biāo)的Icon組件和顯示圖片的Image組件,當(dāng)我們顯示一系列的小圖標(biāo)的時候,我們可以使用Icon組件,當(dāng)顯示圖片時,我們就用專用的Image組件2023-04-04