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

时间:2022-02-06 09:51:36

        本来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>
        以上,感谢收看~