Android中CheckBox復(fù)選框控件使用方法詳解
CheckBox復(fù)選框控件使用方法,具體內(nèi)容如下
一、簡(jiǎn)介
1、
2、類結(jié)構(gòu)圖
二、CheckBox復(fù)選框控件使用方法
這里是使用java代碼在LinearLayout里面添加控件
1、新建LinearLayout布局
2、建立CheckBox的XML的Layout文件
3、通過(guò)View.inflate()方法創(chuàng)建CheckBox
CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null);
4、通過(guò)LinearLayout的addView方法添加CheckBox
ll_checkBoxList.addView(checkBox);
5、通過(guò)List<CheckBox>完成輸出功能
for(CheckBox checkBox:checkBoxList)
三、代碼實(shí)例
1、效果圖:
2、代碼
fry.Activity01
package fry; import java.util.ArrayList; import java.util.List; import com.example.CheckBoxDemo1.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.Toast; public class Activity01 extends Activity implements OnClickListener{ private List<CheckBox> checkBoxList=new ArrayList<CheckBox>(); private LinearLayout ll_checkBoxList; private Button btn_ok; // CheckBox復(fù)選框控件使用方法 // 這里是使用java代碼在LinearLayout里面添加控件 // 1、新建LinearLayout布局 // 2、建立CheckBox的XML的Layout文件 // 3、通過(guò)View.inflate()方法創(chuàng)建CheckBox // 4、通過(guò)LinearLayout的addView方法添加CheckBox // 5、通過(guò)List<CheckBox>完成輸出功能 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); ll_checkBoxList=(LinearLayout) findViewById(R.id.ll_CheckBoxList); btn_ok=(Button) findViewById(R.id.btn_ok); String[] strArr={"你是學(xué)生嗎?","你是否喜歡android","您喜歡旅游嗎?","打算出國(guó)嗎?"}; for(String str:strArr){ CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null); checkBox.setText(str); ll_checkBoxList.addView(checkBox); checkBoxList.add(checkBox); } btn_ok.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub String str=""; for(CheckBox checkBox:checkBoxList){ if(checkBox.isChecked()){ str+=checkBox.getText().toString()+"\n"; } } Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } }
/CheckBoxDemo1/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/ll_CheckBoxList" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="確定" /> </LinearLayout>
/CheckBoxDemo1/res/layout/checkbox.xml
<?xml version="1.0" encoding="utf-8"?> <CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </CheckBox>
四、收獲
1、 View.inflate(this, R.layout.checkbox, null)方法里面的checkbox的XML
<?xml version="1.0" encoding="utf-8"?> <CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </CheckBox>
2、用代碼在LinearLayout中添加CheckBox方法
1)通過(guò)View.inflate()方法創(chuàng)建CheckBox
CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null);
2)通過(guò)LinearLayout的addView方法添加CheckBox
ll_checkBoxList.addView(checkBox);
3、List<CheckBox>的創(chuàng)建
private List<CheckBox> checkBoxList=new ArrayList<CheckBox>();
4、for(CheckBox checkBox:checkBoxList)
遍歷
5、list類結(jié)構(gòu)圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android RadioButton單選框的使用方法
- Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解
- Android中創(chuàng)建對(duì)話框(確定取消對(duì)話框、單選對(duì)話框、多選對(duì)話框)實(shí)例代碼
- Android自定義單選多選下拉列表的實(shí)例代碼
- Android單選按鈕對(duì)話框用法實(shí)例分析
- android實(shí)現(xiàn)單選按鈕功能
- Android ListView實(shí)現(xiàn)單選及多選等功能示例
- Android之復(fù)選框?qū)υ捒蛴梅▽?shí)例分析
- Android復(fù)選框?qū)υ捒蛴梅▽?shí)例簡(jiǎn)析
- Android開發(fā)之獲取單選與復(fù)選框的值操作示例
相關(guān)文章
Flutter實(shí)現(xiàn)自定義搜索框AppBar的示例代碼
開發(fā)中,頁(yè)面頭部為搜索樣式的設(shè)計(jì)非常常見,為了可以像系統(tǒng)AppBar那樣使用,本文將利用Flutter自定義一個(gè)搜索框,感興趣的可以了解一下2022-04-04使用Flutter實(shí)現(xiàn)一個(gè)走馬燈布局的示例代碼
這篇文章主要介紹了使用 Flutter 實(shí)現(xiàn)一個(gè)走馬燈布局的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11詳解Kotlin Android開發(fā)中的環(huán)境配置
這篇文章主要介紹了詳解Kotlin Android開發(fā)中的環(huán)境配置的相關(guān)資料,需要的朋友可以參考下2017-06-06Ionic2創(chuàng)建App啟動(dòng)頁(yè)左右滑動(dòng)歡迎界面
使用Ionic2創(chuàng)建應(yīng)用非常簡(jiǎn)單,只需在V1的命令后跟上--v2即可.這篇文章主要介紹了Ionic2創(chuàng)建App啟動(dòng)頁(yè)左右滑動(dòng)歡迎界面的相關(guān)資料,需要的朋友可以參考下2016-10-10Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
這篇文章主要介紹了Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Android App中自定義View視圖的實(shí)例教程
這篇文章主要介紹了Android App中自定義View視圖的實(shí)例教程,詳細(xì)講解了如何在創(chuàng)建View中定義各種鎖需要的樣式屬性,需要的朋友可以參考下2016-04-04Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法,實(shí)例分析了Android實(shí)現(xiàn)拋物線運(yùn)動(dòng)的算法原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android編程實(shí)現(xiàn)獲取標(biāo)題欄、狀態(tài)欄的高度、屏幕大小及模擬Home鍵的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取標(biāo)題欄、狀態(tài)欄的高度、屏幕大小及模擬Home鍵的方法,涉及Android獲取手機(jī)常見信息的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10