上一节的PopupWindow基本能用
更通用一点的看下面,不过一行显示多少个小球需要gridView.setNumColumns(8)设定一下,这里我没抽成动态的,具体看源码:
用法:
更通用一点的看下面,不过一行显示多少个小球需要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); }