Android自定义view之筛选按钮

来源:互联网 发布:剑灵召唤师女捏脸数据 编辑:程序博客网 时间:2024/05/16 11:43

前言:
现在市面上有很多需要判定当前的选项被选中然后在前面显示一个勾表示选中的效果,这种效果的实现方式有很多种,比如用一个button和一张图片来组合,下面是实现步骤。。先来看看简单的原型图:
这里写图片描述

步骤一:
自定义一个类,然后去继承一个Layout,这个layout可以是RelativeLayout,也可以是LinearLayout,也可以是ViewGroup,然后创建XML文件代码:
LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/llyt_item_filter_bg”
android:layout_width=”match_parent”
android:layout_height=”@dimen/shopping_list_flilter_input_height”
android:clickable=”true”
android:gravity=”center”
android:orientation=”horizontal”
android:paddingLeft=”@dimen/common_margin10dp”
android:paddingRight=”@dimen/common_margin10dp”
android:weightSum=”1”>

<ImageView    android:id="@+id/iv_selected"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@mipmap/hook_icon" /><ToggleButton    android:id="@+id/tb_item"    android:layout_width="0dp"    android:layout_height="match_parent"    android:layout_weight="1"    android:background="@null"    android:button="@null"    android:clickable="false"    android:ellipsize="end"    android:maxLines="1"    android:textOff="@string/shopping_list_flilter_sales"    android:textOn="@string/shopping_list_flilter_sales"    android:textSize="@dimen/shopping_list_filter_item_font_size" />

LinearLayout>
步骤二:重写继承layout的三个构造方法,在第三个构造发方法中去获取xml文件里面的控件,然后定义我们需要的方法。
方法一:选中与未选中的判定:
private boolean checked;

public boolean isChecked() {
return checked;
}

public void setChecked(boolean checked) {    this.checked = checked;    if (checked) {        tbItem.setChecked(true);        llytItemFilterBg.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_filter_item_red_bg));        tbItem.setTextColor(getResources().getColorStateList(R.color.color_title_bg));        ivSelected.setVisibility(VISIBLE);    } else {        tbItem.setChecked(false);        ivSelected.setVisibility(GONE);        llytItemFilterBg.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_filter_item_default_bg));        tbItem.setTextColor(getResources().getColorStateList(R.color.color_default_font_color));    }}

方法二:设置和获取文字

public String getText() {    return tbItem.getText() + "";}public void setText(String text) {    tbItem.setText(text);    tbItem.setTextOff(text);    tbItem.setTextOn(text);}

方法三:重写onFinishInflate方法

@Override
protected void onFinishInflate() {
super.onFinishInflate();
setChecked(false);
}
OK,到这里一个完整的筛选控件就完成了,谢谢!

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 保险柜没有电了打不开怎么办 小保险箱没电了怎么办 bim墙的颜色反了怎么办 眼睛大但是无神怎么办 吃了凉的胃难受怎么办 吃凉东西胃疼怎么办 游戏只有一个分辨率选项怎么办 玩游戏心态易崩怎么办 打游戏心态炸了怎么办 赛鸽比赛回来拉稀怎么办 鸽子拉竹节水便怎么办 新买的鸽子拉稀怎么办 信鸽羽毛上长了虫子怎么办 羊肉煮熟了太硬怎么办 切菜不小心切到手指怎么办 打荷盘子端错了怎么办 学厨师不会翻锅怎么办? 在饭店上班不给工资怎么办 炒菜的左手拿不起锅怎么办?? 厨师想尽快上手炒菜怎么办? 空调吹的肩膀痛怎么办 学厨师翻锅不会怎么办 肋软骨隆鼻太硬怎么办 百度云下载速度慢怎么办 买的种子没出怎么办 铃木小r烧整流器怎么办 厨师在厨房太热怎么办 被家长投诉体罚孩子怎么办 空腹彩超喝水了怎么办 鱼把厕.所堵了怎么办 螃蟹爬进厕所里怎么办 抄作业被老师发现怎么办 鱼把厕所堵了怎么办 刚憋尿上完厕所之后腹部很疼怎么办 小孩被老师投诉了家长怎么办 家长投诉被老师知道了怎么办 孩子上课不敢回答问题怎么办 孩子犯了错家长怎么办 错了不该错的题怎么办 不该错的题错了怎么办 小学生不爱写课堂作业怎么办