android自定义模拟勾选框

android是默认是有CheckBox的,但是这种定死的多选框太难看了。

要想让其变得好看的话就需要重写其中的方法,但是重写原方法的文章我搜到的比较少,加上时间比较紧张了,所以就来不及去研究那些源码了。

所以就自己利用ImageButton做了一个类似的,这并不是真正意义上的多选框,但是实现了多选框的功能。感觉效果还不错。

效果图如下:

android自定义模拟勾选框_第1张图片

至于原理很简单:

每一个选项就是一个LinearLayout

每个LinearLayout里面包含一个TextView和一个ImageButton,而我们只需要对ImageButton添加监听就可以了。

代码如下:


public class SetRMPLDialog extends RelativeLayout{
	
	private Context parentContext;
	private android.view.ViewGroup.LayoutParams lp;
	
	/**布局*/
	private TextView showtitle;
	private int showtitleid=41310;
	
	private RelativeLayout allitem;
	private int allitemid=41320;
	private Map itemmap;
	
	private Button okBtn;
	private Button exitBtn;
	
	private PitchListen pitchlisten;//对按钮的监听
	private BtnListen btnlisten;//对确定和退出按钮的监听

	public SetRMPLDialog(Context context) {
		super(context);
		this.parentContext=context;
		pitchlisten=new PitchListen();
		btnlisten=new BtnListen();
		init();
	}
	
	private void init() {
		showtitle=new TextView(parentContext);
		showtitle.setText("请选择屏蔽号码的位数");
		showtitle.setId(showtitleid);
		LayoutParams lpshowtitle=new LayoutParams(-2,-2);
		lpshowtitle.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		lpshowtitle.addRule(RelativeLayout.CENTER_HORIZONTAL);
		
		
		allitem=new RelativeLayout(parentContext);
		allitem.setId(allitemid);
		LayoutParams lpallitem=new LayoutParams(-2,200);
		lpallitem.addRule(RelativeLayout.BELOW,showtitleid);
		itemmap=new HashMap();
		//创建显示列表
		createItem();
		
		okBtn=new Button(parentContext);
		okBtn.setText("确定");
		LayoutParams lpokBtn=new LayoutParams(-2,-2);
		lpokBtn.setMargins(20, 0, 0, 0);
		lpokBtn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lpokBtn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		
		exitBtn=new Button(parentContext);
		exitBtn.setText("退出");
		LayoutParams lpexitBtn=new LayoutParams(-2,-2);
		lpexitBtn.setMargins(0, 0, 20, 0);
		lpexitBtn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lpexitBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		
		addView(showtitle,lpshowtitle);
		addView(allitem,lpallitem);
		addView(okBtn,lpokBtn);
		addView(exitBtn,lpexitBtn);
	}

	public void createItem(){
		for(int i=5;i<=13;i++){
			OneItem oneitem=new OneItem(parentContext, i);
			allitem.addView(oneitem,oneitem.getLpitem());
			itemmap.put(oneitem.getI(), oneitem);
		}
	}

	public android.view.ViewGroup.LayoutParams getLp() {
		lp=new android.view.ViewGroup.LayoutParams(-1,300);
		return lp;
	}
	
	class OneItem extends LinearLayout{
		int i;
		TextView text;
		ImageButton sel;
		android.widget.RelativeLayout.LayoutParams lpitem;
		public OneItem(Context context,int i) {
			super(context);
			this.i=i;
			text=new TextView(parentContext);
			text.setText((i!=13)?i+"位:":"13+"+"位:");
			LayoutParams lptext=new LayoutParams(70,40);
			lptext.setMargins(0, 5, 0, 5);
			sel=new ImageButton(parentContext);
			//添加图片选择资源
			sel.setBackgroundResource(R.drawable.nopitch);
			LayoutParams lpsel=new LayoutParams(40,40);
			lpsel.setMargins(5, 5, 5, 5);
			addView(text,lptext);
			addView(sel,lpsel);
			//添加测试的背景颜色
			this.setBackgroundColor(Color.CYAN);
			
			//对选项按钮添加监听
			sel.setOnClickListener(pitchlisten);
		}
		
		public android.widget.RelativeLayout.LayoutParams getLpitem(){
			lpitem=new android.widget.RelativeLayout.LayoutParams(120,50);
			switch (i%3) {
			case 0:
				//6.9.12
				lpitem.addRule(RelativeLayout.CENTER_HORIZONTAL);
				break;
			case 1:
				//7,10,13
				lpitem.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				break;
				
			default:
				//5,8,11
				lpitem.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				break;
			}
			switch ((i-5)/3) {
			case 0:
				lpitem.addRule(RelativeLayout.ALIGN_PARENT_TOP);
				break;
			case 1:
				lpitem.addRule(RelativeLayout.CENTER_VERTICAL);
				break;
			default:
				lpitem.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				break;
			}
			
			return lpitem;
		}

		public int getI() {
			return i;
		}

		public void setI(int i) {
			this.i = i;
		}

		public TextView getText() {
			return text;
		}

		public void setText(TextView text) {
			this.text = text;
		}

		public ImageButton getSel() {
			return sel;
		}

		public void setSel(ImageButton sel) {
			this.sel = sel;
		}
		
		
	}

	class PitchListen implements OnClickListener {

		@Override
		public void onClick(View v) {
			Set pitchSet = DBHelper.getPitchSet();
			if (v instanceof ImageButton) {
				ImageButton sel = (ImageButton) v;
				OneItem oneitem = (OneItem) sel.getParent();
				int i=oneitem.getI();
				Log.i("tag", "选择了"+i+"号按钮");
				if(pitchSet.contains(i)){
					oneitem.getSel().setBackgroundResource(R.drawable.nopitch);
					pitchSet.remove(i);
				}else{
					oneitem.getSel().setBackgroundResource(R.drawable.pitch);
					pitchSet.add(i);
				}
			}
		}
	}
	
	class BtnListen implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v==okBtn){
				Log.i("tag", "okBtn");
			}else if(v==exitBtn){
				Log.i("tag", "exitBtn");
			}else{
				Log.e("error", this.toString()+"监听按钮出异常了");
			}
		}
		
	}
}

DBHelper类:

public class DBHelper {
	static Map blackmap;
	static Map whitemap;
	static Set selectset;
	static Map dustbin;
	static Set pitchset;
	
	public static Set getPitchSet(){
		if(pitchset==null){
			pitchset=new HashSet();
		}
		return pitchset;
	}
}

然后,大功告成了,应该实现了多选框的功能。

下面我还设定了2个按钮,分别为提交和取消,提交的话直接上传一个Set集合就可以了。

你可能感兴趣的:(android自定义模拟勾选框)