生活随笔
收集整理的這篇文章主要介紹了
Android之CheckBox复选框控件使用inelayout.xml Xml代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
linelayout.xml
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"?>?? ????<Button?android:layout_width="fill_parent"?android:layout_height="wrap_content"?? ????????android:id="@+id/buton"?android:text="@string/button">?? ????</Button>?? </LinearLayout>?? checkbox.xml
Xml代碼??
<?xml?version="1.0"?encoding="utf-8"?>?? <CheckBox?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:layout_width="fill_parent"?? ????android:layout_height="wrap_content"?>?? </CheckBox>?? ?MainActivity.java
Java代碼??
import?java.util.ArrayList;?? import?java.util.List;?? ?? import?android.app.Activity;?? import?android.app.AlertDialog;?? 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;?? ?? public?class?MainActivity?extends?Activity??implements?OnClickListener{?? ?? ????private?List?<CheckBox>?checkboxs?=?new?ArrayList<CheckBox>?();??? ?????? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ?????????? ????????String[]?chekcBoxTexts?=?new?String[]{"旅游","讀書","看書","爬山","踢足球","打籃球","打乒乓球"};?? ?????????? ????????LinearLayout?linerLayout?=?(LinearLayout)?getLayoutInflater().inflate(R.layout.linelayout,?null)?;?? ????????for(int?i=0;i<?chekcBoxTexts.length;i++){?? ????????????String?item?=?chekcBoxTexts[i];?? ?????????????? ????????????CheckBox?checkbox?=?(CheckBox)?getLayoutInflater().inflate(R.layout.checkbox,?null);?? ????????????checkbox.setText(item);?? ????????????checkboxs.add(checkbox);?? ?????????????? ????????????linerLayout.addView(checkbox,?i);?? ????????}?? ????????setContentView(linerLayout);?? ?????????? ????????Button?button?=?(Button)?this.findViewById(R.id.buton);?? ????????button.setOnClickListener(this)?;?? ????}?? ?? ????@Override?? ????public?void?onClick(View?v)?{?? ????????StringBuffer?s?=?new?StringBuffer();?? ????????for(CheckBox?box?:?checkboxs){?? ????????????if(box.isChecked()){?? ????????????????if(s.length()>0){s.append("\n");}?? ????????????????s.append(box.getText())?;?? ????????????}?? ????????}?? ????????if(s.length()==0){?? ????????????s.append("您還沒有選擇呢");?? ????????}?? ?????????? ????????new?AlertDialog.Builder(this).setMessage(s.toString()).setPositiveButton("關(guān)閉",?null).show()?;?? ?????????? ????}?? } ?
總結(jié)
以上是生活随笔為你收集整理的Android之CheckBox复选框控件使用inelayout.xml Xml代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。