Android開發(fā)之獲取單選與復(fù)選框的值操作示例
本文實(shí)例講述了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">
<!--定義兩個(gè)單選按鈕-->
<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="喜歡的顏色"/>
<!--定義一個(gè)垂直線性布局-->
<LinearLayout
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--定義三個(gè)復(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ù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android分享純圖片到QQ空間實(shí)現(xiàn)方式
- Android權(quán)限如何禁止以及友好提示用戶開通必要權(quán)限詳解
- Android開發(fā)中TextView各種常見使用方法小結(jié)
- 史上最全Android build.gradle配置詳解(小結(jié))
- Android開發(fā)實(shí)現(xiàn)的圓角按鈕、文字陰影按鈕效果示例
- Android開發(fā)實(shí)現(xiàn)Switch控件修改樣式功能示例【附源碼下載】
- Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
- Android開發(fā)之ListView的簡(jiǎn)單用法及定制ListView界面操作示例
- Android文字基線Baseline算法的使用講解
- Android中Retrofit的簡(jiǎn)要介紹
相關(guān)文章
Android中的Handler與多線程應(yīng)用實(shí)例
這篇文章主要介紹了Android中的Handler與多線程應(yīng)用實(shí)例,本文首先解釋一下handler是用來干嘛的,然后通過例子介紹其在多線程中的應(yīng)用,需要的朋友可以參考下2015-03-03
android 檢查網(wǎng)絡(luò)連接狀態(tài)實(shí)現(xiàn)步驟
android 如何檢查網(wǎng)絡(luò)連接狀態(tài),是android開發(fā)中一個(gè)常見的問題,本文將介紹如何實(shí)現(xiàn),需要的朋友可以參考下2012-12-12
Android里實(shí)現(xiàn)退出主程序的提示代碼
當(dāng)用戶選擇"確定",就退出當(dāng)前的對(duì)話框。其中,有個(gè)很重要的函數(shù),Activity.finish(),通過調(diào)用這個(gè)函數(shù),退出當(dāng)前運(yùn)行的整個(gè)Android程序2013-06-06
利用Jetpack Compose繪制可愛的天氣動(dòng)畫
Jetpack Compose是用于構(gòu)建原生Android UI的現(xiàn)代工具包。Jetpack Compose使用更少的代碼,強(qiáng)大的工具和直觀的Kotlin API,簡(jiǎn)化并加速了Android上的UI開發(fā)。本文將利用Jetpack Compose繪制可愛的天氣動(dòng)畫,感興趣的可以了解一下2022-01-01
Android退出應(yīng)用最優(yōu)雅的方式(改進(jìn)版)
這篇文章主要介紹了Android退出應(yīng)用最優(yōu)雅的方式,改進(jìn)版,感興趣的小伙伴們可以參考一下2016-01-01
Android使用ftp方式實(shí)現(xiàn)文件上傳和下載功能
這篇文章主要介紹了Android使用ftp方式實(shí)現(xiàn)文件上傳和下載功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android中EditText setText方法的踩坑實(shí)戰(zhàn)
這篇文章主要給大家分享了一些關(guān)于Android中EditText setText方法的踩坑記錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

