Android笔记之自定义的RadioGroup、RadioButton,以及View实例状态的保存与恢复
效果图
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <com.bu_ish.custom_radio_button.RadioGroup
android:id="@+id/rgTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:checkedButton="@id/urbApproved"> <com.bu_ish.custom_radio_button.UnderlineRadioButton
android:id="@+id/urbAll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:text="全部"
app:textColorChecked="@color/colorPrimary"
app:textColorUnchecked="#55000000"
app:textMarginBottom="3dp"
app:textMarginTop="3dp"
app:underlineColorChecked="@color/colorPrimary"
app:underlineColorUnchecked="@android:color/transparent"
app:underlineHeight="3dp" /> <com.bu_ish.custom_radio_button.UnderlineRadioButton
android:id="@+id/urbApproved"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:text="已通过"
app:textColorChecked="@color/colorPrimary"
app:textColorUnchecked="#55000000"
app:textMarginBottom="3dp"
app:textMarginTop="3dp"
app:underlineColorChecked="@color/colorPrimary"
app:underlineColorUnchecked="@android:color/transparent"
app:underlineHeight="3dp" /> <com.bu_ish.custom_radio_button.UnderlineRadioButton
android:id="@+id/urbVerifying"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:text="审核中"
app:textColorChecked="@color/colorPrimary"
app:textColorUnchecked="#55000000"
app:textMarginBottom="3dp"
app:textMarginTop="3dp"
app:underlineColorChecked="@color/colorPrimary"
app:underlineColorUnchecked="@android:color/transparent"
app:underlineHeight="3dp" />
</com.bu_ish.custom_radio_button.RadioGroup> <TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="已通过" />
</LinearLayout>
MainActivity.java
package com.bu_ish.custom_radio_button; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
private RadioGroup rgTab;
private TextView tvContent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgTab = findViewById(R.id.rgTab);
tvContent = findViewById(R.id.tvContent);
rgTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(int checkedId) {
switch (checkedId) {
case R.id.urbAll:
tvContent.setText("全部");
break;
case R.id.urbApproved:
tvContent.setText("已通过");
break;
case R.id.urbVerifying:
tvContent.setText("审核中");
}
}
});
}
}
RadioGroup.java
package com.bu_ish.custom_radio_button; import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout; public class RadioGroup extends LinearLayout {
private int checkedId;
private OnCheckedChangeListener onCheckedChangeListener;
private static final String KEY_SUPER_PARCELABLE = "SuperParcelableKey";
private static final String KEY_CHECKED_ID = "CheckedIdKey"; public RadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RadioGroup);
checkedId = typedArray.getResourceId(R.styleable.RadioGroup_checkedButton, NO_ID);
typedArray.recycle();
} @Override
protected void onFinishInflate() {
super.onFinishInflate();
if (checkedId != NO_ID) {
setCheckedStateForView(checkedId, true);
}
} @Override
protected Parcelable onSaveInstanceState() {
Parcelable superParcelable = super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putParcelable(KEY_SUPER_PARCELABLE, superParcelable);
bundle.putInt(KEY_CHECKED_ID, checkedId);
return bundle;
} @Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
super.onRestoreInstanceState(bundle.getParcelable(KEY_SUPER_PARCELABLE));
int id = bundle.getInt(KEY_CHECKED_ID);
check(id);
} @Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
UnderlineRadioButton button = (UnderlineRadioButton) child;
if (button.isChecked()) {
int buttonId = button.getId();
if (buttonId != checkedId) {
setCheckedId(buttonId);
if (checkedId != NO_ID) {
setCheckedStateForView(checkedId, false);
}
}
}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
if (id != checkedId) {
check(id);
}
}
});
super.addView(child, index, params);
} public void check(int id) {
if (id != checkedId) {
setCheckedStateForView(checkedId, false);
setCheckedStateForView(id, true);
setCheckedId(id);
}
} public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
onCheckedChangeListener = listener;
} private void setCheckedStateForView(int viewId, boolean checked) {
UnderlineRadioButton button = findViewById(viewId);
button.setChecked(checked);
} private void setCheckedId(int id) {
checkedId = id;
if (onCheckedChangeListener != null) {
onCheckedChangeListener.onCheckedChanged(id);
}
} public interface OnCheckedChangeListener {
void onCheckedChanged(int checkedId);
}
}
UnderlineRadioButton.java
package com.bu_ish.custom_radio_button; import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView; public class UnderlineRadioButton extends FrameLayout {
private TextView tvText;
private View underline;
private int textColorChecked, textColorUnchecked;
private int underlineColorChecked, underlineColorUnchecked;
private boolean checked; public UnderlineRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnderlineRadioButton);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
LayoutParams llLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llLayoutParams.gravity = Gravity.CENTER;
tvText = new TextView(context);
String text = typedArray.getString(R.styleable.UnderlineRadioButton_text);
tvText.setText(text);
LinearLayout.LayoutParams tvTextLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tvTextLayoutParams.gravity = Gravity.CENTER;
tvTextLayoutParams.topMargin = typedArray.getDimensionPixelSize(R.styleable.UnderlineRadioButton_textMarginTop, 0);
tvTextLayoutParams.bottomMargin = typedArray.getDimensionPixelSize(R.styleable.UnderlineRadioButton_textMarginBottom, 0);
ll.addView(tvText, tvTextLayoutParams);
underline = new View(context);
int underlineHeight = typedArray.getDimensionPixelSize(R.styleable.UnderlineRadioButton_underlineHeight, 0);
LinearLayout.LayoutParams underlineLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, underlineHeight);
ll.addView(underline, underlineLayoutParams);
addView(ll, llLayoutParams);
boolean checked = typedArray.getBoolean(R.styleable.UnderlineRadioButton_checked, false);
textColorChecked = typedArray.getColor(R.styleable.UnderlineRadioButton_textColorChecked, 0);
textColorUnchecked = typedArray.getColor(R.styleable.UnderlineRadioButton_textColorUnchecked, 0);
underlineColorChecked = typedArray.getColor(R.styleable.UnderlineRadioButton_underlineColorChecked, 0);
underlineColorUnchecked = typedArray.getColor(R.styleable.UnderlineRadioButton_underlineColorUnchecked, 0);
setChecked(checked);
typedArray.recycle();
} public boolean isChecked() {
return checked;
} public void setChecked(boolean checked) {
this.checked = checked;
if (checked) {
tvText.setTextColor(textColorChecked);
underline.setBackgroundColor(underlineColorChecked);
} else {
tvText.setTextColor(textColorUnchecked);
underline.setBackgroundColor(underlineColorUnchecked);
}
}
}
P.S.
onSaveInstanceState用于保存实例状态
onRestoreInstanceState用于恢复实例状态
onFinishInflate,完成填充的回调
完整Demo链接:https://pan.baidu.com/s/1E_kQoVaatxFMdstfzw8oZw,提取码:rg9c
Android笔记之自定义的RadioGroup、RadioButton,以及View实例状态的保存与恢复的更多相关文章
- Android笔记之自定义对话框
效果如下图 对话框布局 dialog_uninstallation_confirmation.xml <?xml version="1.0" encoding="u ...
- Android笔记之自定义PopupWindow
效果图 popup_window_addition.xml <?xml version="1.0" encoding="utf-8"?> <L ...
- Android之Activity状态的保存和恢复
系统在某些情况下会调用onSaveInstanceState()和onRestoreInstanceState() 这两个方法来保存和恢复Activity的状态. 一句话:Activity在" ...
- Android开发中Activity状态的保存与恢复
当置于后台的Activity因内存紧张被系统自动回收的时候,再次启动它的话他会重新调用onCretae()从而丢失了之前置于后台前的状态. 这时候就要重写Activity的两个方法来保存和恢复状态,具 ...
- 保存和恢复 Android Fragment 的状态
经过几年在 Android 应用开发中应用 Fragment 的努力之后,我必须要说尽管Fragment的概念非常优秀,但是它也同时带来了一堆问题.当我们处理实例的状态保存时就需要特别一件一件地修护好 ...
- React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)
React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...
- 【Android自定义控件】支持多层嵌套RadioButton的RadioGroup
前言 非常喜欢用RadioButton+RadioGroup做Tabs,能自动处理选中等效果,但是自带的RadioGroup不支持嵌套RadioButton(从源码可看出仅仅是判断子控件是不是Radi ...
- android单选按钮选择,RadioGroup,radioButton
android单选按钮选择,RadioGroup,radioButton 14. 四 / android基础 / 没有评论 单选布局绑定 如何识别选择
- Android RadioGroup/RadioButton
RadioGroup RadioButton的集合,提供多选一的机制 属性: android:orientation="horizontal/vertical&quo ...
随机推荐
- 代码验证C#执行”文件打开关闭操作“耗时
2017-04-19 部门经理习惯用C#做数据清洗,遇到个需要验证的问题,在一个万次左右循环内对文件执行打开关闭操作,比在循环前打开文件.循环后关闭文件耗时多多少. using System; usi ...
- 转:android实时语音问题分析
转:http://ticktick.blog.51cto.com/823160/1746136 PigeonCall:一款Android VoIP网络电话App架构分析 2016-02-29 20:1 ...
- 使用Kinect2作为Oculus游戏应用的输入设备
注: 文章写于2015年8月, 眼下VR游戏Demo已经完结, 所以把上一次预研的一些经验分享出来, 希望对大家有所帮助 背景 初接触Oculus时, 从网上下载了一大堆的Demo来体验, 可是, 操 ...
- iOS开发--Mac下server搭建
前言 对于Mac电脑的认识.我一直停留在装B神器的意识上.就在前两天我彻底改变了庸俗的看法,当时忙着写毕业设计.苦于iOS开发没有server, 数据都是从网上抓取或本地plist文件,感觉不够高大上 ...
- Laravel之事件
一.事件 事件无处不在,比如用户登录.购买商品.搜索.查看文章,等等,都是事件,有了事件,就有事件监听器,事件监听器监听到事件发生后会执行一些操作,Laravel使用观察者模式来实现这种监听机制.本节 ...
- odoo分析会计
odoo财务会计凭证录入时,支持 在凭证行 输入 分析账户和 分析标签 如果凭证行设置了 分析账户或者分析标签, 则在会计凭证过账的时候, 在分析会计功能建立 分析会计分录 其中, 如果设置了分析账户 ...
- chown将指定文件的拥有者改为指定的用户或组(转)
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- 使用Batik绘制SVG图并保存为png图像格式
SVG(Scalable Vector Graph)--可缩放矢量图形. 可缩放矢量图形是基于可扩展标记语言(标准通用标记语言的子集),用于描写叙述二维矢量图形的一种图形格式.它由万维网联盟制定.是一 ...
- 【BIEE】08_修改浏览器标题栏显示内容
打开分析,我们可以看到标题栏中显示的BIEE默认的,现在想要把这个修改为自定义的 打开文件路径: D:\obiee\Oracle_BI1\bifoundation\web\msgdb\l_zh-CN\ ...
- 网络爬虫与搜索引擎优化(SEO)
一.网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引. ...