Android 自定义PopupWindow

实现一个带按钮的多级下拉框

自定义PopupWindow

public class Multspiner extends PopupWindow implements OnClickListener {

	private Activity activity;

	private OnItemClickListener onItemClickListener;

	//定义弹出框的组件
	private View mPopupMultiList;
	public PopupWindow mpopupWindow;
	private Spinner placeone_Spinner = null;  
	private Spinner placetwo_Spinner = null;     
	private EditText placethree =null;  

	private Button mChageTrue = null;
	private Button mChageFalse = null;


	private String placeone_text = "1";
	private String placetwo_text = "1";
	private String placethree_text = " ";
	public String place_text = "";


	public enum MENUITEM {
		placeone_Spinner ,placetwo_Spinner ,placethree ,mChageTrue ,mChageFalse
	}

	public Multspiner(final Activity activity) {
		super(activity);
		this.activity = activity;
		mPopupMultiList = LayoutInflater.from(activity).inflate(R.layout.popup_list_one,null,false);
		this.setContentView(mPopupMultiList);
		this.setWidth(700);
		this.setHeight(800);
		this.setFocusable(true);
		this.setTouchable(true); 
		this.setOutsideTouchable(true); 
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);

		//add by ws 多级下拉框功能
		placeone_Spinner = (Spinner)mPopupMultiList.findViewById(R.id.place_one);
		placetwo_Spinner = (Spinner)mPopupMultiList.findViewById(R.id.place_two);
		placethree = (EditText)mPopupMultiList.findViewById(R.id.place_three);

		mChageTrue = (Button)mPopupMultiList.findViewById(R.id.loction_chage_ture);
		mChageFalse = (Button)mPopupMultiList.findViewById(R.id.loction_chage_false);
		//绑定监听器
        //按钮留为外部接口
		mChageTrue.setOnClickListener(this);
		mChageFalse.setOnClickListener(this);
        //其他控机内部定义监听
		placeone_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView adapterView, View view, int pos, long id) {
				//获得选择的内容
				placeone_text = placeone_Spinner.getSelectedItem().toString();
			}
			@Override
			public void onNothingSelected(AdapterView parent) {
			}
		});

		placetwo_Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView adapterView, View view, int pos, long id) {
				placetwo_text = placetwo_Spinner.getSelectedItem().toString();
			}
			@Override
			public void onNothingSelected(AdapterView adapterView) {
			}
		});

		placethree.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				placethree_text = placethree.getText().toString();
			}
		});

	}


	public void showLocation(int resourId) {
		showAsDropDown(activity.findViewById(resourId));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		MENUITEM menuitem = null;
        //拼接多个控件的选择内容
		place_text = placeone_text+"-"+placetwo_text+"-"+placethree_text;
		String str = place_text;
		if ((Button)v == mChageTrue) {
			menuitem = MENUITEM.mChageTrue;
		} else if ((Button)v == mChageFalse) {
			menuitem = MENUITEM.mChageFalse;
		}
		if (onItemClickListener != null) {
			onItemClickListener.onClick(menuitem, str);
		}
		dismiss();
	}

	
	public interface OnItemClickListener {
        //设置外部监听参数
		public void onClick(MENUITEM item, String str);
	}

	
	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}

}

 spinner的下拉内容在xml中定义

在MainActivity中的自定义popupWindow调用

定义

private Multspiner mMultspiner;

调用

mMultspiner = new Multspiner(this);
mMultspiner.showLocation(R.id.tag_real_list_view);
				mMultspiner.setOnItemClickListener(new Multspiner.OnItemClickListener() {

					@Override
					public void onClick(Multspiner.MENUITEM item, String str) {
						if (item == Multspiner.MENUITEM.mChageTrue) {
                            Toast.makeText(mContext,"已修改地点为:"+str,Toast.LENGTH_SHORT).show();
						} else if (item == Multspiner.MENUITEM.mChageFalse) {
							Toast.makeText(mContext,"已取消修改",Toast.LENGTH_SHORT).show();
						}
					}
				});

R.id.tag_real_list_view是要显示的位置,这里选择在某个组件上显示

你可能感兴趣的:(安卓,android,javascript)