RadioGroup和RadioButton
首先看RadioGroup,它是继承了LinearLayout,属于ViewGroup
作为RadioButton的容器,RadioGroup的属性比较简单,和LinearLayout基本差不多,区别是有 RadioGroup.OnCheckedChangeListener接口,XML可以默认单选的ID
默认情况下应在此单选组中选中的子单选按钮的 ID。 |
看一下公告的方法
公共方法 | |
void | addView(View child, int index, ViewGroup.LayoutParams params) 添加具有指定布局参数的子视图。 |
void | autofill(AutofillValue value) 自动填充此视图的内容value。 |
void | check(int id) 将选择设置为其标识符在参数中传递的单选按钮。 |
void | 清除选择。 |
generateLayoutParams(AttributeSet attrs) 根据提供的属性集返回一组新的布局参数。 | |
返回此对象的类名以用于可访问性目的。 | |
int | 描述此视图的自动填充类型,因此 AutofillService可以AutofillValue 在自动填充视图时创建正确的类型。 |
获取View的当前自动填充值。 | |
int | 返回该组中所选单选按钮的标识符。 |
void | onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) 使用AccessibilityNodeInfo有关此视图的信息初始化。 |
void | setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener) 在此组中检查的单选按钮更改时,请注册要调用的回调。 |
void | setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener) 注册一个回调,以便在将子项添加到此视图或从中删除时调用。 |
接下来看RadioButton,它是继承了CompoundButton,属于Button
XML主要属性,其他可以参考基础控件TextView
Drawable 用于按钮图形(例如,复选框和单选按钮)。 | |
应用于按钮图形的色调。 | |
用于应用按钮图形色调的混合模式。 |
看一下公共方法
公共方法 | |
返回此对象的类名以用于可访问性目的。 | |
void | onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) 使用AccessibilityNodeInfo有关此视图的信息初始化。 |
void | toggle() 将视图的选中状态更改为与其当前状态相反的状态 如果单选按钮已被选中,则此方法不会切换单选按钮。 |
下面是实际应用的样式
效果:
<RadioGroup
android:id="@+id/rg_fence"
android:layout_width="match_parent"
android:layout_height="@dimen/common_100dp"
android:layout_marginTop="@dimen/common_10dp"
android:background="@color/white"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/fl_name">
<RadioButton
android:id="@+id/rb_no_fly_area"
android:layout_width="match_parent"
android:layout_height="@dimen/common_50dp"
android:background="@drawable/selector_normal_press_bg"
android:button="@null"
android:drawableStart="@drawable/shape_fence_red_circle"
android:drawableEnd="@drawable/select_fence_check"
android:drawablePadding="@dimen/common_15dp"
android:paddingStart="@dimen/common_10dp"
android:paddingEnd="@dimen/common_10dp"
android:text="@string/no_fly_area"
android:textColor="@color/C6"
android:textSize="@dimen/sp_15" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/common_0_5dp"
android:layout_marginStart="@dimen/common_12dp"
android:background="@color/gray_c6c6c8" />
<RadioButton
android:id="@+id/rb_electronic_fence"
android:layout_width="match_parent"
android:layout_height="@dimen/common_50dp"
android:background="@drawable/selector_normal_press_bg"
android:button="@null"
android:checked="true"
android:drawableStart="@drawable/shape_fence_green_circle"
android:drawableEnd="@drawable/select_fence_check"
android:drawablePadding="@dimen/common_15dp"
android:paddingStart="@dimen/common_10dp"
android:paddingEnd="@dimen/common_10dp"
android:text="@string/electronic_fence"
android:textColor="@color/C6"
android:textSize="@dimen/sp_15" />
</RadioGroup>
RadioGroup可以嵌套其他组件,例如下图
<RadioGroup
android:id="@+id/rg_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/common_10dp"
android:background="@color/white"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/ll_seek">
<RadioButton
android:id="@+id/rb_time_limit"
android:layout_width="match_parent"
android:layout_height="@dimen/common_50dp"
android:background="@drawable/selector_normal_press_bg"
android:button="@null"
android:drawableEnd="@drawable/select_fence_check"
android:drawablePadding="@dimen/common_15dp"
android:paddingStart="@dimen/common_10dp"
android:paddingEnd="@dimen/common_10dp"
android:text="@string/fence_period"
android:textColor="@color/C6"
android:textSize="@dimen/sp_15" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl_time_limit"
android:layout_width="match_parent"
android:layout_height="@dimen/common_100dp"
android:visibility="gone">
<TextView
android:id="@+id/tv_start"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="@string/start"
android:textColor="@color/C6"
android:textSize="@dimen/sp_15"
app:layout_constraintBottom_toTopOf="@id/line"
app:layout_constraintStart_toStartOf="@id/line"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_start_date"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="@dimen/common_15dp"
android:background="@drawable/selector_normal_press_bg"
android:drawableStart="@mipmap/fence_time_add"
android:drawablePadding="@dimen/common_6dp"
android:gravity="center_vertical"
android:text="@string/date"
android:textColor="@color/blue_007aff"
android:textSize="@dimen/sp_15"
app:layout_constraintBottom_toTopOf="@id/line"
app:layout_constraintStart_toEndOf="@id/tv_start"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_start_time"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="@dimen/common_15dp"
android:background="@drawable/selector_normal_press_bg"
android:drawableStart="@mipmap/fence_time_add"
android:drawablePadding="@dimen/common_6dp"
android:gravity="center_vertical"
android:text="@string/time"
android:textColor="@color/blue_007aff"
android:textSize="@dimen/sp_15"
app:layout_constraintBottom_toTopOf="@id/line"
app:layout_constraintEnd_toEndOf="@id/line"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="@dimen/common_0_5dp"
android:layout_marginStart="@dimen/common_24dp"
android:background="@color/gray_c6c6c8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_end"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="@string/end"
android:textColor="@color/C6"
android:textSize="@dimen/sp_15"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@id/line"
app:layout_constraintTop_toBottomOf="@id/line" />
<TextView
android:id="@+id/tv_end_date"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="@dimen/common_15dp"
android:background="@drawable/selector_normal_press_bg"
android:drawableStart="@mipmap/fence_time_add"
android:drawablePadding="@dimen/common_6dp"
android:gravity="center_vertical"
android:text="@string/date"
android:textColor="@color/blue_007aff"
android:textSize="@dimen/sp_15"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/tv_start"
app:layout_constraintTop_toBottomOf="@id/line" />
<TextView
android:id="@+id/tv_end_time"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="@dimen/common_15dp"
android:background="@drawable/selector_normal_press_bg"
android:drawableStart="@mipmap/fence_time_add"
android:drawablePadding="@dimen/common_6dp"
android:gravity="center_vertical"
android:text="@string/time"
android:textColor="@color/blue_007aff"
android:textSize="@dimen/sp_15"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/line"
app:layout_constraintTop_toBottomOf="@id/line" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/common_0_5dp"
android:background="@color/gray_c6c6c8"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="@dimen/common_0_5dp"
android:layout_marginStart="@dimen/common_12dp"
android:background="@color/gray_c6c6c8" />
<RadioButton
android:id="@+id/rb_permanent"
android:layout_width="match_parent"
android:layout_height="@dimen/common_50dp"
android:background="@drawable/selector_normal_press_bg"
android:button="@null"
android:checked="true"
android:drawableEnd="@drawable/select_fence_check"
android:drawablePadding="@dimen/common_15dp"
android:paddingStart="@dimen/common_10dp"
android:paddingEnd="@dimen/common_10dp"
android:text="@string/fence_valid"
android:textColor="@color/C6"
android:textSize="@dimen/sp_15" />
</RadioGroup>
附上图片资源
shape_fence_red_circle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/red_D80000_30" />
<stroke
android:width="@dimen/common_0_5dp"
android:color="@color/red_D80000" />
<size
android:width="@dimen/common_10dp"
android:height="@dimen/common_10dp" />
</shape>
shape_fence_green_circle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/green_3CE171_30" />
<stroke
android:width="@dimen/common_0_5dp"
android:color="@color/green_3CE171" />
<size
android:width="@dimen/common_10dp"
android:height="@dimen/common_10dp" />
</shape>
select_fence_check
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/icon_select_check" android:state_checked="true" />
<item>
<shape android:shape="rectangle">
<solid android:color="@color/transparent" />
<size android:width="@dimen/common_15dp" android:height="@dimen/common_15dp" />
</shape>
</item>
</selector>
接下来看点击事件
@BindView(R.id.rg_fence)
RadioGroup rg_fence;
@BindView(R.id.rg_time)
RadioGroup rg_time;
rg_fence.setOnCheckedChangeListener((group, checkedId) -> {
if (checkedId == R.id.rb_no_fly_area) {
} else if (checkedId == R.id.rb_electronic_fence) {
}
});
rg_time.setOnCheckedChangeListener((group, checkedId) -> {
if (checkedId == R.id.rb_time_limit) {
} else if (checkedId == R.id.rb_permanent) {
}
});