PopupWindow在Editext中使用

1:新建一个Android项目将相关资源文件拷贝到如图所示文件夹中

PopupWindow在Editext中使用

2:在activity_main.xml布局文件中资源文件布局如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText 
        android:id="@+id/et_test"
        android:layout_width="180dip"
        android:layout_height="wrap_content"
        android:hint="请输入QQ号码"
        />
   <ImageButton 
       android:id="@+id/ib_test"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/button"
       android:layout_alignTop="@id/et_test"
       android:layout_alignRight="@id/et_test"
       android:layout_alignBottom="@id/et_test"
       />
</RelativeLayout>
       list_itam.xml布局如下(用来显示在Popupwindow每一个条目的布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dip"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
     >
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/user"
        />
       <TextView 
        android:id="@+id/tv_test"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />
       <ImageView 
           android:id="@+id/ib_test1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/delete"
        />
</LinearLayout>
3:在MainActivity代码书写如下

package com.example.youku_spinner;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity {

	protected static final String TAG = "MainActivity";
	private EditText et_test;
	private ImageButton ib;
	private PopupWindow pw;//用来显示下拉菜单条
	private ListView listview;
	private ArrayList<String> qqstrs;//用来显示号码的集合
	private SpninnerListViewAdapter sladapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et_test = (EditText) findViewById(R.id.et_test);
		ib = (ImageButton) findViewById(R.id.ib_test);
		qqstrs=new ArrayList<String>();
		//初始化20个qq号码
		for(int i=12345678;i<12345698;i++){
			qqstrs.add(i+"");
		}
		ib.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(TAG, "onClick");
				if(null==pw||!pw.isShowing()){
				  showPopupwindow();
				}else{
				  dimissPopupwindow();
				}	
			}
		});
	}
	/**
	 * 显示popupwindow
	 */
	public void showPopupwindow(){
		if(null==pw){
		  listview = new ListView(this);
		  sladapter = new SpninnerListViewAdapter();
		  listview.setAdapter(sladapter);
		  listview.setDivider(null);
		  listview.setCacheColorHint(0x00000000);
		  pw = new PopupWindow(listview, et_test.getWidth(), LayoutParams.WRAP_CONTENT, true);
		  pw.setBackgroundDrawable(new ColorDrawable(0x00000000));//目的是让 显示popupwindow后点击popupwindow后,讲自动隐藏popupwindow
		}
		pw.showAsDropDown(et_test, 0,0);
	}
	/**
	 * 隐藏popupwindow
	 */
	public void dimissPopupwindow(){
		if(null!=pw){
			pw.dismiss();
		}
	}
	private class SpninnerListViewAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return qqstrs.size();
		}
		@Override
		public Object getItem(int arg0) {
			return qqstrs.get(arg0);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if(null==convertView){
				convertView=View.inflate(getApplicationContext(), R.layout.list_item, null);
			}
			final TextView tv_test=(TextView) convertView.findViewById(R.id.tv_test);
			tv_test.setText(qqstrs.get(position));
			tv_test.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					et_test.setText(tv_test.getText());
					dimissPopupwindow();
				}
			});
			//给imagebutton设置点击事件,用来当点击时,讲该条目删除掉
			convertView.findViewById(R.id.ib_test1).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					qqstrs.remove(position);//将点击的当前view的QQ号码从集合中移除掉
					sladapter.notifyDataSetChanged();//通知SpninnerListViewAdapter适配器数据发生改变了
				}
			});
			return convertView;
		}
	}
}

Ok 搞定!希望分享给大家! 谢谢

PopupWindow在Editext中使用

上一篇:win7 iis7.5 配置错误解决办法


下一篇:MacBook 关闭访客登陆