自定义PopupWindow2

系统 1702 0
上一节的PopupWindow基本能用
更通用一点的看下面,不过一行显示多少个小球需要gridView.setNumColumns(8)设定一下,这里我没抽成动态的,具体看源码:
    
package com.dl.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dl.app.R;

public class NumbersPickerPopupWindow extends PopupWindow{
	private Context context;
	private String[] balls;
	private final String split=" "; 
	private Button btn_ok;
	private String selectedNumbers;
	private String[] selectedNumbersArray;
	private GridViewAdapter adapter;
	/**
	 * 
	 * @param context
	 * @param view
	 * @param title 标题
	 * @param balls 生成的小球
	 * @param selectedNumbers 预选的小球
	 */
	public NumbersPickerPopupWindow(Context context,View view,String title,String[] balls,String selectedNumbers){
		super(view, LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, true);
		this.context=context;
		this.selectedNumbers=selectedNumbers;
		this.balls=balls;
		selectedNumbersArray=selectedNumbers.trim().split(split);
		this.setBackgroundDrawable(new BitmapDrawable());//必须设置background才能消失  
		this.setOutsideTouchable(false);
        
      //自定义动画  
      this.setAnimationStyle(R.style.PopupAnimation);
      //使用系统动画  
//      mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);  
      this.update();  
      this.setTouchable(true);
      this.setFocusable(false);
      
      GridView gridView=(GridView)view.findViewById(R.id.gridView);
//      gridView.setNumColumns(8);
      adapter=new GridViewAdapter(context);
      
      gridView.setAdapter(adapter);
      
      TextView tv_tips=(TextView)view.findViewById(R.id.tv_tips);
      tv_tips.setText(title);
      
      btn_ok=(Button)view.findViewById(R.id.btn_ok);
      btn_ok.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(isShowing())
					dismiss();
//				if(onOkClickListener!=null){
//					onOkClickListener.onOkClick(v);
//				}
			}
		});
      
      this.setOnDismissListener(onDismissListener);
      
	}
	
	private OnDismissListener onDismissListener=new OnDismissListener(){

		public void onDismiss() {
			// TODO Auto-generated method stub
			if(onOkClickListener!=null){
				onOkClickListener.onOkClick(btn_ok);
			}
		}
		
	};
	
	public void setSelectedNumbers(String selectedNumbers){
		this.selectedNumbers=selectedNumbers;
		selectedNumbersArray=selectedNumbers.trim().split(split);
		adapter.notifyDataSetChanged();
		
	}
	
	class GridViewAdapter extends BaseAdapter{

		private Context context;
    	public GridViewAdapter(Context context){
    		this.context=context;
    	}
    	
		public int getCount() {
			// TODO Auto-generated method stub
			return balls.length;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return balls[position];
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView==null){
				convertView=LayoutInflater.from(context).inflate(R.layout.simple_grid_item_1_red, null);
			}
			CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);//
			checkBox.setText(balls[position]);
			if(selectedNumbersArray!=null&&selectedNumbersArray.length>0){
				for(int i=0;i<selectedNumbersArray.length;i++){
					if(selectedNumbersArray[i].equals(balls[position])){
						checkBox.setChecked(true);
					}
				}
			}
			
			checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(onItemCheckedListener!=null){
						onItemCheckedListener.onItemCheckedChanged(buttonView,isChecked);
					}
				}
			});
			return convertView;
		}
		
	}
	//接口
	private OnItemCheckedListener onItemCheckedListener;
	public void setOnItemCheckedListener(OnItemCheckedListener onItemCheckedListener) {
		this.onItemCheckedListener = onItemCheckedListener;
	}

	public interface OnItemCheckedListener{
		public void onItemCheckedChanged(CompoundButton buttonView, boolean isChecked);
	}
	
	private OnOkClickListener onOkClickListener;
	public void setOnOkClickListener(OnOkClickListener onOkClickListener) {
		this.onOkClickListener = onOkClickListener;
	}

	public interface OnOkClickListener{
		public void onOkClick(View v);
	}
	
}

  

用法:
    
LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
    	View view = mLayoutInflater.inflate(R.layout.popwindow2, null);  

tips="至少选择1个数字";
			balls=new String[]{"1","2","3","4","5","6","7","8","9","10",
					"11","12","13","14","15","16","17","18","19","20",
					"21","22","23","24","25","26"
			};
selectedNumbers="4 7 24";//空格隔开!
picker=new NumbersPickerPopupWindow(context,view,tips,balls,selectedNumbers);
		picker.setOnItemCheckedListener(onItemCheckedListener);
		picker.setOnOkClickListener(onOkClickListener);
		if(!picker.isShowing()){
			picker.showAtLocation(views[index], Gravity.BOTTOM, 0, 0);
		}

  


自定义PopupWindow2

自定义PopupWindow2


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论