目錄
概述
RadioButton
是單選按鈕,點擊後選中,再次點擊不可取消選中。RadioButton
繼承自CompoundButton(複合按鈕)
。RadioButton組必須在RadioGroup
視圖(RadioGroup
繼承自LinearLayout
)中使用。RadioButton組中只有最後被點擊的RadioButton處於選中狀態,其餘RadioButton則處於未選中狀態。
RadioButton的創建
使用xml佈局創建
使用xml佈局創建RadioButton的核心在於findviewById()
和佈局文件
。注意RadioButton組必須在RadioGroup
中使用。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout...>
...
<RadioGroup
android:id="@+id/rg_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/rb_1"
android:layout_width="match_parent"
android:height="wrap_content"
android:text="Java"/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="match_parent"
android:height="wrap_content"
android:text="C++"/>
<RadioButton
android:id="@+id/rb_3"
android:layout_width="match_parent"
android:height="wrap_content"
android:text="C#"/>
</RadioGroup>
...
</LinearLayout>
MainActivity.java
import...
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//remove title bar and status bar in activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
RadioGroup rg_1=new (RadioGroup)findviewById(R.id.rg_1);
RadioButton rb_1=new (RadioButton)findviewById(R.id.rb_1);
RadioButton rb_2=new (RadioButton)findviewById(R.id.rb_2);
RadioButton rb_3=new (RadioButton)findviewById(R.id.rb_3);
...
}
}
使用Java代碼創建
使用Java代碼創建RadioButton的步驟與使用Java代碼創建Button的步驟基本一致(詳見Button的創建、監聽與繼承):
①使用構造函數RadioGroup(Context context)
創建RadioGroup
對象並設置必要屬性參數
RadioGroup rg_1=new RadioGroup(this);
LinearLayout.LayoutParams params_rg=new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rg_1.setLayoutParams(params_rg);
②將RadioGroup對象添加至目標ViewGroup
//find ViewGroup in xml file
LinearLayout layout_main=(LinearLayout)findviewById(R.id.layout_main);
layout_main.addView(rg_1);
③爲RadioGroup設置id、外觀等參數
rg_1.setId(R.id.rg_1);
rg_1.setBackgroundResource(R.drawable.drawablefile);
...
④使用構造函數RadioButton(Context context)
創建RadioButton對象並設置必要屬性參數
RadioButton rb_1=new RadioButton(this);
RadioButton rb_2=new RadioButton(this);
RadioButton rb_3=new RadioButton(this);
...
LinearLayout.LayoutParams params_rb=new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rb_1.setLayoutParams(params_rb);
rb_2.setLayoutParams(params_rb);
rb_3.setLayoutParams(params_rb);
...
⑤將RadioButton添加到之前創建的RadioGroup對象
rg_1.addView(rb_1);
rg_1.addView(rb_2);
rg_1.addView(rb_3);
...
⑥爲RadioButton設置id、外觀等參數
rb_1.setId(R.id.rb_1);
rb_1.setBackgroundResource(R.drawable.drawablefile);
rb_2.setId(R.id.rb_2);
rb_2.setBackgroundResource(R.drawable.drawablefile);
rb_3.setId(R.id.rb_3);
rb_3.setBackgroundResource(R.drawable.drawablefile);
...
⑦使用目標ViewGroup對象的removeView()
方法從目標ViewGroup移除RadioButton
rg_1.removeView(rb_1);
RadioButton配合ScrollView使用
當ViewGroup尺寸有限而內容View可能會超過ViewGroup尺寸時,可以使用ScrollView
裝盛View以實現滾動顯示。但是由於RadioButton
組必須在RadioGroup
中使用,再配合ScrollView
使用時需要注意有不同之處,正確的做法是:RadioButton
in RadioGroup
,RadioGroup
in ScrollView
;而並非:。RadioButton
in ScrollView
,ScrollView
in RadioGroup
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/rb_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Java"/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="C++"/>
<RadioButton
android:id="@+id/rb_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="C#"/>
</RadioGroup>
</ScrollView>
RadioButton的監聽
與RadioButton相關的監聽器爲OnCheckedChangeListener
,然而RadioButton不可註冊OnCheckedChangeListener
。RadioButton的監聽,需要由RadioButton所在的RadioGroup
註冊OnCheckedChangeListener
。
充當RadioGroup的OnCheckedChangeListener
的對象類必須實現RadioGroup.OnCheckedChangeListener
接口。
public class MyCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
...
}
RadioButton設置監聽器的方式有:
①當前Activity類
實現RadioGroup.OnCheckedChangeListener
接口
②內部類
實現RadioGroup.OnCheckedChangeListener
接口
③匿名內部類
實現RadioGroup.OnCheckedChangeListener
接口
④外部類
實現RadioGroup.OnCheckedChangeListener
接口
充當OnCheckedChangeListener
的對象類必須重寫onCheckedChanged()
方法:
public class MyCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
...
@Override
public void onCheckedChanged(RadioGroup group,int checkedId) {
if(group.getId()==R.id.rg_1) {
switch(checkedId) {
case R.id.rb_1:
...
break;
case R.id.rb_2:
...
break;
...
}
}
else if(group.getId()==R.id.rg_2) {...}
...
}
...
}
每點擊一次未選中的RadioButton按鈕就會觸發onCheckedChanged()
方法。使用group.getId()
方法判斷響應點擊的RadioGroup
;使用checkedId
判斷響應點擊的RadioGroup
中被選中的RadioButton
。
RadioButton的常用屬性
①android:checked
RadioButton的當前選中狀態。屬性值爲true/false
。爲true
當前狀態爲被選中;爲false
當前狀態爲未被選中。對應的設置方法爲setChecked(boolean checked)
。
②android:button
RadioButton左側按鈕的圖形或顏色。屬性值可以是RGB888
或ARGB
值,也可以是圖形圖片資源。對應的設置方法爲setButtonDrawable(R.drawable.drawablefile)
。
③android:background
RadioButton右側文字部分的背景。屬性值可以是RGB888
或ARGB
值,也可以是圖形圖片資源。對應的設置方法爲setBackground()
和setBackgroundColor()
。
④android:gravity
RadioButton文字的內部對齊方式。屬性值:top(頂對齊)
、bottom(底對齊)
、left(左對齊)
、right(右對齊)
、center(居中)
。對應的設置方法爲setGravity()
。
⑤android:scaleX/android:scaleY
RadioButton的X/Y方向的尺寸縮放。屬性值爲0~1
。對應的設置方法爲setScaleX(float scaleX)/setScaleY(float scaleY)
。
RadioButton的繼承
自定義RadioButton時會用到RadioButton的繼承。AndroidStudio要求使用AppCompatRadioButton
代替RadioButton用以自定義RadioButton類的繼承。對於開發者而言,自定義RadioButton中最有意義的方法是構造函數
和onTouchEvent()
。
以下以簡單實例展示RadioButton的繼承:
rb_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="200dp"
android:height="25dp"/>
<solid
android:color="#50f0f0f0"/>
<corners
android:radius="10dp"/>
<stroke
android:width="1dp"
android:color="#ff000000"/>
</shape>
rb_id
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="rg_1" type="id"/>
<item name="rb_1" type="id"/>
<item name="rb_2" type="id"/>
<item name="rb_3" type="id"/>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
...
android:id="@+id/layout_main"
...>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rb_shape"
android:text="hello"/>
...
</LinearLayout>
MyRadioButton.java
import...
import androidx.appcompat.widget.AppCompatRadioButton;
public class MyRadioButton extends AppCompatRadioButton {
//constructors
public MyRadioButton(Context context) {
super(context);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//set necessary params
this.setLayoutParams(params);
//set background
this.setBackgroundResource(R.drawable.rb_shape);
}
public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//set necessary params
this.setLayoutParams(params);
//set background
this.setBackgroundResource(R.drawable.rb_shape);
}
public MyRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//set necessary params
this.setLayoutParams(params);
//set background
this.setBackgroundResource(R.drawable.rb_shape);
}
}
MainActivity.java
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
private TextView tv_1=(TextView)findviewById(R.id.tv_1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//remove title bar and status bar in activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LinearLayout layout_main=new (LinearLayout)findviewById(R.id.layout_main);
RadioGroup rg_1=new RadioGroup(this);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rg_1.setLayoutParams(params);
layout_main.addView(rg_1);
rg_1.setId(R.id.rg_1);
MyRadioButton rb_1=new MyRadioButton(this);
MyRadioButton rb_2=new MyRadioButton(this);
MyRadioButton rb_3=new MyRadioButton(this);
rg_1.addView(rb_1);
rg_1.addView(rb_2);
rg_1.addView(rb_3);
rb_1.setId(R.id.rb_1);
rb_1.setText("Java");
rb_1.setOnCheckedChangeListener(this);
rb_2.setId(R.id.rb_2);
rb_2.setText("C++");
rb_2.setOnCheckedChangeListener(this);
rb_3.setId(R.id.rb_3);
rb_3.setText("Python");
rb_3.setOnCheckedChangeListener(this);
}
@Override
public void onCheckeChanged(RadioGroup group,int checkedId) {
if(group.getId==R.id.rg_1) {
switch(checkedId) {
case R.id.rb_1:
tv_1.setText("you choosed Java");
break;
case R.id.rb_2:
tv_1.setText("you choosed C++");
break;
case R.id.rb_3:
tv_1.setText("you choosed Python");
break;
}
}
}
}
歡迎指正