目錄
Android 開發第七講 RadioButton (單選按鈕)
一丶重構代碼
之前我們響應按鈕事件都是直接通過匿名內部類的方式. new一個對象來實現OnClick方法.
現在因為按鈕較多.所以新建內部類,實現接口為 View.OnClickListener 並且實現里面的OnClick方法
代碼如下:
package com.ibinary.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.view.View;
import android.widget.Button;
// Alt + Shift + Entery 引入此包
public class MainActivity extends AppCompatActivity {
// 聲明Button 一會使用 如果找不到則引入這個包 Alt+Shift+Enter
private Button m_BtnText;
private Button m_BtnEditView;
private Button m_ButtonView;
private Button m_RadioButtonView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_BtnText = (Button) findViewById(R.id.Btn_Text);
m_BtnEditView = (Button) findViewById(R.id.EdtView1);
m_ButtonView = (Button) findViewById(R.id.m_BtnView);
m_RadioButtonView = (Button) findViewById(R.id.RadioButton_Id);
SetListener();
}
private void SetListener()
{
MyOnClick myOnClick = new MyOnClick();
m_BtnText.setOnClickListener(myOnClick);
m_BtnEditView.setOnClickListener(myOnClick);
m_ButtonView.setOnClickListener(myOnClick);
m_RadioButtonView.setOnClickListener(myOnClick);
}
// 實現接口,在街口里面判斷
private class MyOnClick implements View.OnClickListener{
@Override
public void onClick(View view) {
Intent intent = null;
switch (view.getId()){
case R.id.Btn_Text:
//跳轉到TextView中
intent = new Intent(MainActivity.this,TextViewActive.class);
break;
//跳轉到Button頁面
case R.id.m_BtnView:
intent = new Intent(MainActivity.this,MyButton.class);
break;
//跳轉到ExtView頁面
case R.id.EdtView1:
intent = new Intent(MainActivity.this,EdtActive.class);
break;
//跳轉到RadioButton頁面
case R.id.RadioButton_Id:
intent = new Intent(MainActivity.this,RadioActivity.class);
break;
}
if (intent == null) {
return;
}
startActivity(intent);
}
}
}
二丶RadioButton屬性與xml編寫
2.1 RadioButton屬性
RadioButton是繼承自TextView 所以一些屬性是可以用的.
單獨定義一個RadioButton不會有效果的.原因是.兩個RadioButton以上. 都屬於一個分組.
當這個分組中定義了兩個 RadioButton的時候.那么你點擊RadioButton1 那么RadioButton2就是未選中狀態.看下如下xml描述
常用屬性
android:checked = "true" 默認選中,使用這個屬性那么 其他的RadioButton必須設置ID
android:button="@null" 去掉按鈕屬性,不使用小園框,自定義一個
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<!-- 使用RadioButton必須放到一個分組里面-->
<RadioGroup
android:id="@+id/rg_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- RadioButton放到里面-->
<RadioButton
android:id="@+id/rd_1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="男"
android:textSize="20sp">
</RadioButton>
<RadioButton
android:id="@+id/rd_2"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="女"
android:textSize="20sp">
</RadioButton>
</RadioGroup>
</RelativeLayout>
界面則為下,選擇男,則女就是未選中,否則就是相反
因為他們在一個組里面.所以只能單選
2.2 RadioButton實現自定義
實現自定義還是使用 android:background屬性,來制定一個選擇狀態的xml. 來實現自定義的選中和未選中
但是前提要 設置 android:button="@null"才可以.
狀態選擇器XML如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="MissingDefaultResource">
<!-- 狀態選擇器 android:state_checked = true代表選中-->
<item android:state_checked="true">
<!-- 如果是選中,那么我們使用shape畫一個-->
<shape>
<!-- 設置實心顏色,並且設置圓角-->
<solid android:color="#ff0000"></solid>
<corners android:radius="10dp"></corners>
</shape>
</item>
<!-- 否則設置為綠色-->
<item android:state_checked="false">
<!-- 如果是未選中,那么我們使用shape畫一個-->
<shape>
<!-- 設置描邊形式,並且設置圓角-->
<stroke android:width="1dp" android:color="#0ca30c"></stroke>
<corners android:radius="10dp"></corners>
</shape>
</item>
</selector>
xml如下布局如下
<!-- 自定義-->
<RadioGroup
android:id="@+id/rg_2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:layout_below="@id/rg_1"
android:layout_marginTop="1dp"
>
<!-- RadioButton放到里面-->
<RadioButton
android:id="@+id/rd_3"
android:layout_width="100dp"
android:layout_height="40dp"
android:button="@null"
android:background="@drawable/selector_radiobutton"
android:text="男1"
android:textSize="20sp">
</RadioButton>
<RadioButton
android:id="@+id/rd_4"
android:layout_width="100dp"
android:layout_height="40dp"
android:button="@null"
android:background="@drawable/selector_radiobutton"
android:text="女1"
android:textSize="20sp">
</RadioButton>
</RadioGroup>
請注意selector_radiobutton 文件名一定小寫.
實現效果如下
自定義了一個實現效果
三丶RadioButton的監聽事件
既然是單選那么單選之后肯定會有監聽事件
package com.ibinary.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioActivity extends AppCompatActivity {
private RadioGroup m_rg1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
m_rg1 = (RadioGroup)findViewById(R.id.rg_1);
//響應Checked Listener
m_rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
//兩個參數,一個是組,一個是選中的ID. 可以通過組里面的finviewById找到當前是哪個Id
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton RaButton = radioGroup.findViewById(i);
Toast.makeText(RadioActivity.this, RaButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
只需要設置事件,實現 RadioGroup.OnCheckedChangeListener() 即可.