這篇具有很好參考價值的文章主要介紹了安卓控件 - 單選按鈕和復選框。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。
一、介紹
安卓應用中,常常需要用戶從若干選項中進行選擇,有時要求只能選擇一個,那么就要使用單選按鈕(RadioButton),有時要求用戶可以選擇多個,那么就要使用復選框(CheckBox)
二、常用屬性
1、單選按鈕
屬性 |
含義 |
orientation |
vertical (或 horizontal),決定單選按鈕是垂直排列還是水平排列 |
layout_width |
寬度(單位:dp) |
layout_height |
高度(單位:dp) |
屬性 |
含義 |
setOnCheckedChangeListener |
監(jiān)聽單選按鈕選中狀態(tài)的變化 |
setOnClickListener |
監(jiān)聽單選按鈕組是否被單擊了 |
isChecked() |
true 或false,顯示單選按鈕的選中狀態(tài) |
setChecked() |
參數(shù)是true或false,用來設置單選按鈕的選中狀態(tài) |
屬性 |
含義 |
setOnCheckedChangeListener |
監(jiān)聽單選按鈕選中狀態(tài)的變化 |
setOnClickListener |
監(jiān)聽單選按鈕是否被單擊了 |
2、復選框
屬性 |
含義 |
isChecked() |
true 或false,顯示復選框的選中狀態(tài) |
setChecked() |
參數(shù)是true或false,用來設置復選框的選中狀態(tài) |
屬性 |
含義 |
setOnCheckedChangeListener |
監(jiān)聽復選框選中狀態(tài)的變化 |
setOnClickListener |
-+監(jiān)聽復選框是否被單擊了 |
三、案例
1 、示例

2、步驟
(1)新建項目
- 基于
Empty Activity
創(chuàng)建安卓應用SetBasicInformation


(2)制備背景素材
- 拷貝到
drawable
目錄下
(3)字符串資源文件 - strings.xml

<resources>
<string name="app_name">設置基本信息</string>
<string name="set_information">設置基本信息</string>
<string name="name">姓名:</string>
<string name="input_name">請輸入姓名</string>
<string name="gender">性別:</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="hobby">愛好:</string>
<string name="music">音樂</string>
<string name="read">閱讀</string>
<string name="food">美食</string>
<string name="ok">確定</string>
<string name="clear">清除</string>
<string name="exit">退出</string>
</resources>
(4)主布局資源文件 - activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="30dp">
<TextView
android:id="@+id/tv_setInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:text="@string/set_information"
android:textColor="#0000ff"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_name"
android:textSize="20sp"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
android:textColor="#000000"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/male"
android:textSize="20sp" />
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/female"
android:textSize="20sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hobby"
android:textColor="#000000"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/music"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_food"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/food"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<Button
android:id="@+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doOK"
android:layout_marginRight="10dp"
android:text="@string/ok"
android:textSize="20sp" />
<Button
android:id="@+id/btn_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doClear"
android:layout_marginRight="10dp"
android:text="@string/clear"
android:textSize="20sp" />
<Button
android:id="@+id/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doExit"
android:text="@string/exit"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="20sp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:background="#dddddd"/>
</LinearLayout>
(5)主界面類MainActivity
文章來源:http://www.zghlxwxcb.cn/news/detail-736927.html
- 定義常量與變量
- 通過資源標識符獲取控件實例
- 給按鈕組注冊單擊事件監(jiān)聽器
- 確定按鈕單擊事件處理方法
- 清除按鈕單擊事件處理方法
- 推出按鈕單擊事件處理方法
package net.hxl.set_basic_information;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private EditText etName;
private RadioGroup rgGender;
private RadioButton rbMale;
private RadioButton rbFemale;
private CheckBox cbMusic;
private CheckBox cbRead;
private CheckBox cbFood;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.et_name);
rgGender = findViewById(R.id.rg_gender);
rbMale = findViewById(R.id.rb_male);
rbFemale = findViewById(R.id.rb_female);
cbMusic = findViewById(R.id.cb_music);
cbRead = findViewById(R.id.cb_read);
cbFood = findViewById(R.id.cb_food);
tvResult = findViewById(R.id.tv_result);
rgGender.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (rgGender.getCheckedRadioButtonId()){
case R.id.rb_male:// 選中男性單選按鈕
Toast.makeText(MainActivity.this, "你選擇", Toast.LENGTH_SHORT).show();
break;
case R.id.rb_female:// 選中女性單選按鈕
Toast.makeText(MainActivity.this, "你選擇", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
public void doOK(View view){
String name = etName.getText().toString().trim();
String gender = "";
switch (rgGender.getCheckedRadioButtonId()){
case R.id.rb_male:// 選中男性單選按鈕
gender = rbMale.getText().toString();
break;
case R.id.rb_female:// 選中女性單選按鈕
gender = rbFemale.getText().toString();
break;
}
StringBuilder builder = new StringBuilder();
if(cbMusic.isChecked()){
builder.append(cbMusic.getText().toString() + " ");
}
if(cbRead.isChecked()){
builder.append(cbRead.getText().toString() + " ");
}
if(cbFood.isChecked()){
builder.append(cbFood.getText().toString() + " ");
}
String hobbies = builder.toString().trim();
String result = "姓名:" + name + "\n"
+ "性別:" + gender + "\n"
+ "愛好:" + hobbies;
tvResult.setText(result);
}
public void doClear(View view){
etName.setText("");
rbMale.setChecked(true);
cbMusic.setChecked(false);
cbRead.setChecked(false);
cbFood.setChecked(false);
tvResult.setText("");
}
public void doExit(View view){
finish();
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-736927.html
到了這里,關于安卓控件 - 單選按鈕和復選框的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!
本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!