[size=large][b]Android EditText可输入可选择的EditText[/b][/size]
[list=1]
[*]1.页面布局 layout 片段
android:id="@+id/projectmodule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40sp"
android:minHeight="50sp">
android:id="@+id/txt_project"
style="@style/label_text"
android:text="项目名称"/>
android:id="@+id/enrol_project_name"
style="@style/inputText"
android:layout_toRightOf="@id/txt_project"
android:hint="项目名称"
android:inputType="text|textPersonName"
android:paddingRight="50dp"
android:maxLength="20" />
android:id="@+id/open_arrow_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@id/enrol_project_name"
android:layout_alignRight="@id/enrol_project_name"
android:layout_alignTop="@id/enrol_project_name"
android:layout_marginRight="2dp"
android:background="@drawable/open_arrow" />
[*]2.创建新的布局文件 proname_spinner.xml 下拉列表的ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@color/popwin_bg">
android:id="@+id/proname_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#508a88"
android:dividerHeight="1dp">
[*]3.创建列表项布局文件 proname_spinner_item.xml
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pro_icon"
android:layout_marginLeft="5dp"
android:minHeight="40dp"/>
android:id="@+id/item_val"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:textColor="#4f5555"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="marquee"/>
[*]4.创建数据适配器文件 PopupAdapter.java
package com.photo.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.photo.activity.R;
/**
* @description 工程名称 带下拉选择的可编辑输入框,PopupWindow的数据适配器
*
* @author Li Junchao
* @date 2014年5月9日
*/
public class PopupAdapter extends BaseAdapter
{
private Context context;
private ArrayList list = new ArrayList();
@SuppressWarnings("unused")
private Handler handler = null;
private LayoutInflater layoutInflater;
public PopupAdapter()
{
}
public PopupAdapter(Context context,ArrayList list)
{
this.context = context;
this.list = list;
}
public PopupAdapter(Context context,ArrayList list,Handler handler)
{
this.context = context;
this.list = list;
this.handler = handler;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount()
{
return this.list != null ? list.size() : 0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position)
{
return list.get(position);
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position)
{
return position;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHoler holder = null;
if(null == convertView)
{
holder = new ViewHoler();
layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.proname_spinner_item,null);
holder.imgBtn = (ImageView)convertView.findViewById(R.id.item_icon);
holder.txtView = (TextView)convertView.findViewById(R.id.item_val);
//绑定每一列的数据对象在列表项上
convertView.setTag(holder);
}
else
{
holder = (ViewHoler)convertView.getTag();
}
if(holder != null)
{
//给每个列表项指定一个Id标识符,这个标识符是一个确切的数字
convertView.setId(position);
//同时设置列表项中每个元素的标识符,都是本列表项在列表集合中的索引
holder.setId(position);
//从数据集合中取值显示列表文本
holder.txtView.setText(list.get(position));
//设置触摸事件 这里最好不要为每一个列表项绑定touch事件,可以为ListView绑定OnItemClickListener
// holder.txtView.setOnTouchListener(new SelectedItemTouchListener(list,position,handler));
}
return convertView;
}
/**
* 列表项触摸事件
*/
public class SelectedItemTouchListener implements View.OnTouchListener
{
private ArrayList list = new ArrayList();
private int position = 0;
private Handler handler = null;
public SelectedItemTouchListener()
{
}
public SelectedItemTouchListener(ArrayList list,int position,Handler handler)
{
this.list = list;
this.position = position;
this.handler = handler;
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
Message message = new Message();
Bundle bundle = new Bundle();
// if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
// {
// message.what = 0;
// }
// else
// {
Log.d("listview item ontouch",list.get(position));
bundle.putString("value",list.get(position));
bundle.putInt("selectedIndex",position);
message.setData(bundle);
message.what = 1;
// }
//发送一个消息,在主界面里设置输入文本框的值
this.handler.sendMessage(message);
return true;
}
}
/**
* @description 记录列表项数据,绑定数据对象到每一个列表项
*
* @author Admin
* @date 2014年5月9日
*/
public class ViewHoler
{
private TextView txtView;
private ImageView imgBtn;
/**
* @description 列表项元素
*
* @param position
*/
public void setId(int position)
{
this.txtView.setId(position);
this.imgBtn.setId(position);
}
public TextView getTxtView()
{
return txtView;
}
public void setTxtView(TextView txtView)
{
this.txtView = txtView;
}
public ImageView getImgBtn()
{
return imgBtn;
}
public void setImgBtn(ImageView imgBtn)
{
this.imgBtn = imgBtn;
}
}
}
[*]5.在Activity里java代码片段
PopupWindow ppwin = null; //工程名下拉列表的Popupwindow
ListView listView = null;
ArrayList proNames = new ArrayList();
PopupAdapter adapter = null;
ImageButton arrowBtn = null;
boolean isInit = false;
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
{
//每次当前activity获得当前焦点都重新读取sd下已经创建的项目目录
Log.d("onWindowFocusChanged","重新获取工程目录");
getProjectNamesfromSD();
if(adapter != null && !adapter.isEmpty())
{
adapter.notifyDataSetChanged();
}
}
while(!isInit)
{
Log.d("EnrolmentActivity.onWindowFocusChanged","初始化popupwindow");
initPopupWindow();
isInit = true;
}
}
/**
* @description 从sd卡上读取项目名称,返回项目名称数据列表集合
*
* @return
*/
private void getProjectNamesfromSD()
{
proNames.clear();
//如果SD卡当前是已安装和准备好读写的状态
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File projectDir = new File(Environment.getExternalStorageDirectory(),"photepro");
//如果工程文件目录存在
if(projectDir.exists() && projectDir.isDirectory())
{
String[] _filesName = projectDir.list();
for(String name : _filesName)
{
if(!"tmp".equals(name))
{
proNames.add(name);
}
}
}
}
}
/**
* @description 初始化PopupWindow
*
*/
private void initPopupWindow()
{
// handler = new PopupHandler(this);
//项目名称下拉列表控件 下拉按钮
arrowBtn = (ImageButton)findViewById(R.id.open_arrow_btn);
arrowBtn.setOnClickListener(arrowBtnClick);
View view = (View)this.getLayoutInflater().inflate(R.layout.proname_spinner,null);
this.listView = (ListView)view.findViewById(R.id.proname_list);
this.adapter = new PopupAdapter(EnrolmentActivity.this,proNames);
listView.setAdapter(adapter);
listView.setOnItemClickListener(pronameItemClick);
ppwin = new PopupWindow(view,project.getWidth(),LayoutParams.WRAP_CONTENT);
ColorDrawable cd = new ColorDrawable(-0000);
ppwin.setBackgroundDrawable(cd);
ppwin.setOutsideTouchable(true);
ppwin.setTouchable(true);
ppwin.setFocusable(true);
ppwin.setTouchInterceptor(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
//如果点击了popwindow之外的地方
if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
{
ppwin.dismiss();
return true;
}
return false;
}
});
}
//工程名列表视图点选事件
private AdapterView.OnItemClickListener pronameItemClick = new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id)
{
PopupAdapter.ViewHoler holder = (PopupAdapter.ViewHoler)view.getTag();
project.setText(holder.getTxtView().getText());
popupWinDismiss();
}
};
/**
* @description 关闭popupwindow
*
*/
public void popupWinDismiss()
{
ppwin.dismiss();
}
/**
* @description 显示项目名称的popupwindow
*
*/
private void popwindowShowing()
{
ppwin.showAsDropDown(project);
}
[/list]