1、checkbox
CheckBox簡介:
CheckBox和Button一樣,是與用戶點擊有關的控件,也是一種古老的控件,它的優點在於,不用用戶去填寫具體的信息,只需輕輕點擊,缺點在於只有“是”和“否”兩種情況,但我們往往利用它的這個特性,來獲取用戶的一些信息。
2舉例分析
《1》
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView//一個關於愛好的選擇,往往是多選
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選擇愛好"/>
<CheckBox//用checkbox給出兩個愛好
android:id="@+id/checkbox_LOL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOL"/>
<CheckBox
android:id="@+id/checkbox_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學習"/>
</LinearLayout>
《2》
checkbox放在密碼後面,作爲可視按鈕
<EditText
android:id="@+id/checkbox_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
android:password="true"/>
<CheckBox
android:id="@+id/checkbox_showpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼可見"
android:layout_gravity="right"//調整位置到右邊
android:layout_marginRight="30dp"/>//距離右邊界30dp
《3》綜合運用
搭建如下界面:
要求:
1、性別默認爲男性
2、愛好選擇可以多個
3、密碼開始時加密,點擊密碼可見後顯示密碼
4、點擊確定按鈕,在控制檯上打印出性別和愛好
代碼如下:
//這個是搭建界面,整體爲線性佈局,從上到下依次爲:LinearLayout (用於選擇性別)》》LinearLayout (用於選擇愛好)》》button(提交數據)》》EditText(輸入密碼)》》Checkbox(密碼可見)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選擇性別"
android:textSize="30dp"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radiogroup"
android:checkedButton="@id/radio"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其它"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選擇愛好"/>
<CheckBox
android:id="@+id/checkbox_LOL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOL"/>
<CheckBox
android:id="@+id/checkbox_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學習"/>
</LinearLayout>
<Button
android:id="@+id/button_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="確定"/>
<EditText
android:id="@+id/checkbox_password"//輸入密碼id
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
android:password="true"/>
<CheckBox
android:id="@+id/checkbox_showpassword"//顯示密碼id
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼可見"
android:layout_gravity="right"
android:layout_marginRight="30dp"/>
</LinearLayout>
在activity.java中的部分代碼:
setContentView(R.layout.radiobutton);
mCheckboxLOL = (CheckBox) findViewById(R.id.checkbox_LOL);//初始化
mCheckboxstudy = (CheckBox) findViewById(R.id.checkbox_study);
mPassword = (EditText) findViewById(R.id.checkbox_password);
mCheckboxShowPassword = (CheckBox) findViewById(R.id.checkbox_showpassword);
mCheckboxShowPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {//注意這裏是對checkbox建立的選擇事件
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mPassword.setTransformationMethod(null);//當選擇了密碼可見時,顯示密碼,注意這裏的id是輸入密碼的id
} else {
mPassword.setTransformationMethod(new PasswordTransformationMethod());
}
}
});
mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
mButtonSelect = (Button) findViewById(R.id.button_select);//對提交數據按鈕建立點擊事件
mButtonSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int checkedId = mRadioGroup.getCheckedRadioButtonId();
mRadioButton = (RadioButton) findViewById(checkedId);
Log.d("sex", "您的性別是:" + mRadioButton.getText());
//下面是打印出愛好,用ArrayList
ArrayList<String> hobby = new ArrayList<String>();
if (mCheckboxLOL.isChecked()) {
hobby.add("LOL");
}
if (mCheckboxstudy.isChecked()) {
hobby.add("學習");
}
for (String ho : hobby) {
Log.d("hobby", "我的愛好" + ho);
}
}
});