1、 private OptionsAdapter optionsAdapter = null; /下拉框选项数据源 private ArrayList datas = new ArrayList(); /下拉框依附组件 private LinearLayout parent; /下拉框依附组件宽度,也将作为下拉框的宽度 private int pwidth; /文本框 private EditText et; /下拉箭头图片组件 private ImageView image; /恢复数据源按钮 private Button button; /展示所有下拉选项的ListView private
2、ListView listView = null; /用来处理选中或者删除下拉项消息 private Handler handler; /是否初始化完成标志 private boolean flag = false; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.select); /* * 没有在onCreate方法中调用initWedget(),而是在onWindowFocusChanged方法中调用, *
3、 是因为initWedget()中需要获取PopupWindow浮动下拉框依附的组件宽度,在onCreate方法中是无法获取到该宽度的 */ public void onWindowFocusChanged(boolean hasFocus) super.onWindowFocusChanged(hasFocus); while(!flag) initWedget(); flag = true; * 初始化界面控件 private void initWedget() /初始化Handler,用来处理消息 handler = new Handler(SelectActivity.this); /
4、初始化界面组件 parent = (LinearLayout)findViewById(R.id.parent); et = (EditText)findViewById(R.id.edittext); image = (ImageView)findViewById(R.id.btn_select); /获取下拉框依附的组件宽度 int width = parent.getWidth(); pwidth = width; /设置点击下拉箭头图片事件,点击弹出PopupWindow浮动下拉框 image.setOnClickListener(new View.OnClickListener()
5、Override public void onClick(View v) if(flag) /显示PopupWindow窗口 popupWindwShowing(); ); /初始化PopupWindow initPopuWindow(); button = (Button)findViewById(R.id.refresh); /设置点击事件,恢复下拉框列表数据,没有什么作用,纯粹是为了方便多看几次效果而设置 button.setOnClickListener(new View.OnClickListener() initDatas(); optionsAdapter.notifyDataS
6、etChanged(); * 初始化填充Adapter所用List数据 private void initDatas() datas.clear(); datas.add(北京); datas.add(上海广州深圳重庆青岛石家庄 * 初始化PopupWindow */ private void initPopuWindow() initDatas(); /PopupWindow浮动下拉框布局 View loginwindow = (View)this.getLayoutInflater().inflate(R.layout.options, null); listView = (ListVie
7、w) loginwindow.findViewById(R.id.list); /设置自定义Adapter optionsAdapter = new OptionsAdapter(this, handler,datas); listView.setAdapter(optionsAdapter); selectPopupWindow = new PopupWindow(loginwindow, pwidth,LayoutParams.WRAP_CONTENT, true); selectPopupWindow.setOutsideTouchable(true); /这一句是为了实现弹出Popup
8、Window后,当点击屏幕其他部分及Back键时PopupWindow会消失, /没有这一句则效果不能出来,但并不会影响背景 /本人能力极其有限,不明白其原因,还望高手、知情者指点一下 selectPopupWindow.setBackgroundDrawable(new BitmapDrawable(); * 显示PopupWindow窗口 * * param popupwindow public void popupWindwShowing() /将selectPopupWindow作为parent的下拉框显示,并指定selectPopupWindow在Y方向上向上偏移3pix, /这是为
9、了防止下拉框与文本框之间产生缝隙,影响界面美化 /(是否会产生缝隙,及产生缝隙的大小,可能会根据机型、Android系统版本不同而异吧,不太清楚) selectPopupWindow.showAsDropDown(parent,0,-3); * PopupWindow消失 public void dismiss() selectPopupWindow.dismiss(); * 处理Hander消息 public boolean handleMessage(Message message) Bundle data = message.getData(); switch(message.what)
10、 case 1: /选中下拉项,下拉框消失 int selIndex = data.getInt(selIndex et.setText(datas.get(selIndex); dismiss(); break; case 2: /移除下拉项数据 int delIndex = data.getInt(delIndex datas.remove(delIndex); /刷新下拉列表 return false;自定义适配器Adapter代码:import android.view.LayoutInflater;import android.view.ViewGroup;import androi
11、d.widget.BaseAdapter;import android.widget.TextView;/自定义适配器Adapterpublic class OptionsAdapter extends BaseAdapter list = new ArrayList private Activity activity = null; * 自定义构造方法 * param activity * param handler * param list public OptionsAdapter(Activity activity,Handler handler,ArrayList list) thi
12、s.activity = activity; this.handler = handler; this.list = list; public int getCount() return list.size(); public Object getItem(int position) return list.get(position); public long getItemId(int position) return position; public View getView(final int position, View convertView, ViewGroup parent) V
13、iewHolder holder = null; if (convertView = null) holder = new ViewHolder(); /下拉项布局 convertView = LayoutInflater.from(activity).inflate(R.layout.option_item, null); holder.textView = (TextView) convertView.findViewById(R.id.item_text); holder.imageView = (ImageView) convertView.findViewById(R.id.delI
14、mage); convertView.setTag(holder); else holder = (ViewHolder) convertView.getTag(); holder.textView.setText(list.get(position); /为下拉框选项文字部分设置事件,最终效果是点击将其文字填充到文本框 holder.textView.setOnClickListener(new View.OnClickListener() Message msg = new Message(); Bundle data = new Bundle(); /设置选中索引 data.putInt
15、(, position); msg.setData(data); msg.what = 1; /发出消息 handler.sendMessage(msg); /为下拉框选项删除图标部分设置事件,最终效果是点击将该选项删除 holder.imageView.setOnClickListener(new View.OnClickListener() /设置删除索引 msg.what = 2; return convertView;class ViewHolder TextView textView; ImageView imageView; 主界面布局select.xml文件:LinearLayo
16、ut xmlns:android= android:orientation=verticallayout_width=fill_parentlayout_height=background=#EEEED1 LinearLayout android:id=+id/parentwrap_contenthorizontallayout_marginTop=50dplayout_marginLeft=30dpEditText android:+id/edittext200dpsingleLine=true40dpdrawable/bg1paddingLeft=3dp/ImageView android
17、:+id/btn_selectsrc=drawable/img1scaleType=fitXY/LinearLayoutButton android:+id/refresh45dptext=恢复textColor=#000000textSize=20spPopupWindow浮动下拉框布局options.xml文件:gravity=center_horizontalListView android:+id/listcacheColorHint=#00000000/ListView下拉选项布局option_item.xml文件:#235654RelativeLayoutcenter_vertic
18、alminHeight=+id/delImage20dpdrawable/del18splayout_alignParentRight=layout_marginRight=10dpTextView android:+id/item_textlayout_toLeftOf=id/delImage5dplayout_alignParentLeft=/TextView/RelativeLayout到此代码及布局文件基本都贴完了。哦,对了,我是在Android 2.2 测试运行的。本人对Android学习、了解十分有限,而且各种文件、变量、方法等等命名也很不规范,所以代码肯定有很多不足之处,还请各位海涵!当然,更希望各位能不吝赐教,本人将满怀荣幸与感激!
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1