1. RadioButton(單選按鈕)
1.1 RadioButton XML佈局
RadioButton
即單選按鈕
,也就是一組單選按鈕,我們只能選擇其中的一個。RadioButton
要包含在RadioGroup
中。
看一下佈局效果(圖片有點小):
XML
代碼:
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="20sp" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="20sp" />
</RadioGroup>
1.2 RadioButton三種事件處理方法
1.2.0 事件處理大致步驟
1.2.1 demo需求
實現一個單選框,然後用不同的事件處理方法,把我們選中的某個單選按鈕的十六進制Id值
和文本內容
輸出在控制檯上。
1.2.2 onClickListener
onClickListener
是老朋友了,也是一個最基本的控件,我們只需要實現onClickListener
接口,重寫其中的onClick
方法,最後綁定到控件上就OK了。
先來看一下最後的輸出:
下面是代碼,註釋已經標好了:
public class MainActivity extends AppCompatActivity {
//0、聲明對象
private RadioGroup rg;
private RadioButton male, female;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
//1、findViewById獲取對象
rg = (RadioGroup) findViewById(R.id.rg);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
//3、爲控件綁定監聽事件
RadioOnClick roc = new RadioOnClick();
male.setOnClickListener(roc);
female.setOnClickListener(roc);
}
//2、實現OnClickListener接口,重寫onClick方法
class RadioOnClick implements View.OnClickListener {
@Override
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
String m = rb.getId() + "-------->" + rb.getText();
System.out.println(m);
}
}
}
1.2.3 RadioGroup.onCheckedChangeListener
用這個onCheckedChangeListener
只需要綁定到RadioGroup
組件上就好了,需要重寫onCheckedChanged
方法,這個方法包含下面兩個參數:
RadioGroup group
:這個參數就是傳來的RadioGroup
對象int CheckedId
:這個參數傳來的是被選中的某個按鈕的id
屬性,我們用它來判斷誰被選中了。
看一下最後的輸出結果:
代碼:
public class MainActivity extends AppCompatActivity {
//0、聲明對象
private RadioGroup rg;
private RadioButton male, female;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
//1、findViewById獲取對象
rg = (RadioGroup) findViewById(R.id.rg);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
//2、匿名內部類直接懟
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String m = "OnCheckedChangeListener:";
if (checkedId == R.id.male) m += male.getId() + "--->" + male.getText();
else m += female.getId() + "--->" + female.getText();
System.out.println(m);
}
});
}
}
因爲只設置了兩個單選按鈕,可以直接判斷。如果有的人懟了超級多
RadioButton
,可以直接使用循環去判斷,從0開始循環到group.getChildCount
個,判斷有沒有被選中的就可以了。
1.2.4 CompoundButton.OnCheckedChangeListeren
這個接口跟上面的那個接口重名,但是不屬於同一個包。用這個接口需要實現onCheckedChanged
方法,也和上面的那個方法一樣。但是傳遞的參數不一樣。兩個參數分別是:
CompoundButton buttonView
:傳遞過來的類型boolean isChecked
:是否被選中
看一下效果:
代碼:
public class MainActivity extends AppCompatActivity {
//0、聲明對象
private RadioGroup rg;
private RadioButton male, female;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
//1、findViewById獲取對象
rg = (RadioGroup) findViewById(R.id.rg);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
//3、綁定
RadioOnClick roc = new RadioOnClick();
male.setOnCheckedChangeListener(roc);
female.setOnCheckedChangeListener(roc);
}
//2、實現接口,重寫方法
class RadioOnClick implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String m = "";
if(buttonView.getId()==R.id.male) m+=buttonView.getText() + "---->";
else m+=buttonView.getText() + "----->";
if(isChecked) m+="被選中了!";
else m+="沒有被選中!";
System.out.println(m);
}
}
}
2. CheckBox(複選框)
2.1 CheckBox XML佈局
CheckBox
的佈局和上面的RadioButton
佈局很相似,直接寫就好了。
XML代碼:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃飯"
android:textSize="18sp" />
<CheckBox
android:id="@+id/sleep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡覺"
android:textSize="18sp" />
<CheckBox
android:id="@+id/game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="王者榮耀"
android:textSize="18sp" />
</LinearLayout>
效果展示:
2.2 CheckBox的事件處理
2.2.1 OnClick方法
和上述的RadioButton
幾乎一樣,這裏不再展示
2.2.2 CompoundButton.OnCheckedChangeListener
也和上述的一樣,這個方法同樣有兩個參數,因爲上面已經說過了,直接寫個案例,先來看看效果:
java
代碼:
public class MainActivity extends AppCompatActivity {
private CheckBox eat, game, sleep;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
eat = (CheckBox) findViewById(R.id.eat);
sleep = (CheckBox) findViewById(R.id.sleep);
game = (CheckBox) findViewById(R.id.game);
CheckChange cc = new CheckChange();
eat.setOnCheckedChangeListener(cc);
sleep.setOnCheckedChangeListener(cc);
game.setOnCheckedChangeListener(cc);
}
class CheckChange implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String m = "";
switch (buttonView.getId()) {
case R.id.eat:
m += buttonView.getText();
break;
case R.id.sleep:
m += buttonView.getText();
break;
case R.id.game:
m += buttonView.getText();
break;
}
System.out.println(m);
}
}
}
ok啦!
如果我們想設置某一個被選中那就直接用
setChecked(true)
就夠了!