1、複選框CheckBox控件
public class CheckBox extend CompoundButton
複選框允許用戶選擇一個或多個選項。通常情況下,是從一個垂直列表中選擇多個選項。每個複選框是分開管理的,必須爲每個複選框註冊監聽事件。註冊click監聽事件的方式有兩種。
xml佈局文件如下:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="23dp"
android:text="看小說" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:text="打網遊" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:text="看電影" />
第一種:在使用該xml佈局文件的Activity類實現CompoundButton.OnCheckedChangeListener接口
package com.example.checkboxradiobutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
private CheckBox cb1,cb2,cb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = (CheckBox) this.findViewById(R.id.checkBox1);
cb2 = (CheckBox) this.findViewById(R.id.checkBox2);
cb3 = (CheckBox) this.findViewById(R.id.checkBox3);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* @param compoundButton 被單擊的複選框
* @param isChecked 複選框選中爲true,沒有選中爲false
*/
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.checkBox1:
if (isChecked) Toast.makeText(this, "選中看小說", Toast.LENGTH_SHORT).show();
else Toast.makeText(this, "取消看小說", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox2:
if (isChecked) Toast.makeText(this, "選中打網遊", Toast.LENGTH_SHORT).show();
else Toast.makeText(this, "取消打網遊", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox3:
if (isChecked) Toast.makeText(this, "選中看電影", Toast.LENGTH_SHORT).show();
else Toast.makeText(this, "取消看電影", Toast.LENGTH_SHORT).show();
break;
}
}
}
第二種:使用內部類的方式,對每一個複選框注入單擊事件
cb1 = (CheckBox) this.findViewById(R.id.checkBox1);
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
}
});
2、單選框RadioButton控件
單選按鈕允許用戶從一組中選擇一個選項。由於單選按鈕是相互排斥的,通過RadioGroup將多個RadioButton組合到一起,這樣可以確保只能選擇一個選項。
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox3"
android:layout_below="@+id/textView2" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="92dp"
android:layout_height="match_parent"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
事件處理:onRadioButtonClicked。