Android自定义view之筛选按钮

前言:
现在市面上有很多需要判定当前的选项被选中然后在前面显示一个勾表示选中的效果,这种效果的实现方式有很多种,比如用一个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,到这里一个完整的筛选控件就完成了,谢谢!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeArray 是 Android 中的一个特殊的资源类型,用于在 XML 中声明自定义 View 的属性。使用 TypeArray 可以方便地在 XML 布局中指定 View 的属性,而不需要在 Java 代码中进行硬编码。 使用 TypeArray 的步骤如下: 1. 在 res/values/attrs.xml 文件中定义自定义 View 的属性。 ```xml <resources> <declare-styleable name="MyCustomView"> <attr name="customAttr1" format="integer" /> <attr name="customAttr2" format="string" /> <attr name="customAttr3" format="boolean" /> </declare-styleable> </resources> ``` 2. 在自定义 View 的构造函数中获取 TypedArray 对象,并从中获取属性值。 ```java public class MyCustomView extends View { private int customAttr1; private String customAttr2; private boolean customAttr3; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); customAttr1 = a.getInt(R.styleable.MyCustomView_customAttr1, 0); customAttr2 = a.getString(R.styleable.MyCustomView_customAttr2); customAttr3 = a.getBoolean(R.styleable.MyCustomView_customAttr3, false); a.recycle(); } } ``` 在上面的代码中,`context.obtainStyledAttributes(attrs, R.styleable.MyCustomView)` 用于获取 TypedArray 对象,`R.styleable.MyCustomView` 是在 attrs.xml 文件中定义的自定义属性集合,`a.getInt()`、`a.getString()`、`a.getBoolean()` 用于从 TypedArray 对象中获取属性值,最后需要调用 `a.recycle()` 来回收 TypedArray 对象。 3. 在 XML 布局中使用自定义 View,并设置属性值。 ```xml <com.example.MyCustomView android:layout_width="match_parent" android:layout_height="wrap_content" app:customAttr1="123" app:customAttr2="hello" app:customAttr3="true" /> ``` 在上面的代码中,`app:customAttr1`、`app:customAttr2`、`app:customAttr3` 是在 attrs.xml 文件中定义的自定义属性名,可以在 XML 布局中使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值