自定义动态设置check图片的ImageView取代checkBox

        本来checkBox是可以使用的,但是现在新需求里要增加一个状态是disabled,disabled状态要展示特定的图片,这个checkBox解决不了,而且状态也不好控制,所以干脆自己封装一个算了。

         先定义一个自定义attrs:

         

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ImageViewCheckBox">
        <attr name="default_state" format="integer" />
        <attr name="checked_bkg" format="reference" />
        <attr name="unchecked_bkg" format="reference" />
        <attr name="checked_disabled" format="reference" />
    </declare-styleable>
</resources>
        然后写这个自定义的imageView

package com.amuro.main;

import com.amuro.imageviewcheckboxtest.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class ImageViewCheckBox extends ImageView
{
	public static final int CHECK_STATE_DISABLED = 0;
	public static final int CHECK_STATE_UNCHECKED = 1;
	public static final int CHECK_STATE_CHECKED = 2;
	
	private int check_bkg_id;
	private int uncheck_bkg_id;
	private int disable_check_bkg_id;
	
	private int checkState;
	
	public ImageViewCheckBox(Context context)
	{
		this(context, null);
	}
	
	public ImageViewCheckBox(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
		
	}
	
	public ImageViewCheckBox(Context context, AttributeSet attrs, int defStyleAttr)
	{
		super(context, attrs, defStyleAttr);
		init(attrs);
	}

	private void init(AttributeSet attrs)
	{
		TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.ImageViewCheckBox); 
		checkState = t.getInteger(R.styleable.ImageViewCheckBox_default_state, CHECK_STATE_UNCHECKED);
		check_bkg_id = t.getResourceId(R.styleable.ImageViewCheckBox_checked_bkg, 0);
		uncheck_bkg_id = t.getResourceId(R.styleable.ImageViewCheckBox_unchecked_bkg, 0);
		disable_check_bkg_id = t.getResourceId(R.styleable.ImageViewCheckBox_checked_disabled, 0);		
		
		setBkgByCheckState();
		setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				changeState();
			}
		});
		
		t.recycle();
	}
	
	public void changeState()
	{
		if(checkState == CHECK_STATE_DISABLED)
		{
			return;
		}
		
		if(checkState == CHECK_STATE_UNCHECKED)
		{
			checkState = CHECK_STATE_CHECKED;
		}
		else if(checkState == CHECK_STATE_CHECKED)
		{
			checkState = CHECK_STATE_UNCHECKED;
		}
		
		setBkgByCheckState();
		notifyListner();
	}

	
	public void setCheckDisabled()
	{
		this.checkState = CHECK_STATE_DISABLED;
		setBkgByCheckState();
	}

	private void setBkgByCheckState()
	{
		if(checkState == CHECK_STATE_UNCHECKED)
		{
			setBackgroundResource(uncheck_bkg_id);
		}
		else if(checkState == CHECK_STATE_DISABLED)
		{
			setBackgroundResource(disable_check_bkg_id);
		}
		else
		{
			setBackgroundResource(check_bkg_id);
		}
		
	}

	public interface OnCheckStateChangedListener
	{
		public void onCheckStateChanged(boolean isChecked);
	}
	
	private OnCheckStateChangedListener listener;
	
	public void setOnCheckStateChangedListener(OnCheckStateChangedListener listener)
	{
		this.listener = listener;
	}
	
	private void notifyListner()
	{
		if(this.listener != null)
		{
			if(checkState == CHECK_STATE_UNCHECKED)
			{
				this.listener.onCheckStateChanged(false);
			}
			else if(checkState == CHECK_STATE_CHECKED)
			{
				this.listener.onCheckStateChanged(true);
			}
			
		}
	}
}
        imageView直接封装了click事件,各种状态下对应的图片都可直接从配置文件中读取,用户点击的时候直接进行图片的切换模拟checkBox效果,disable状态下changeState方法将直接无效。回调的形式和checkbox一样,使用者会感觉很亲切,哈哈。

最后使用的时候在xml里配置一下这个imageView就可以了:

         

<?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/com.amuro.imageviewcheckboxtest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <com.amuro.main.ImageViewCheckBox 
        android:id="@+id/ivcb"
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:default_state="1"
        app:checked_bkg="@drawable/paysdk2_icon_virtual_ticket_selected"
        app:unchecked_bkg="@drawable/paysdk2_icon_virtual_ticket_unselected"
        app:checked_disabled="@drawable/paysdk2_icon_virtual_ticket_select_disabled"
        />

</LinearLayout>
        以上,感谢收看~



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值