相信大家都有使用QQ的最近的登录的下拉框,点击可以显示最近登录的效果,点击选项可以将内容提交到输入框,实现快速输入数据的效果。
今天我们就来实现这个效果。
首先使用先对布局实现界面
<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" android:gravity="center_horizontal" tools:context=".MainActivity" > <EditText android:gravity="center_horizontal" android:id="@+id/tv_msg" android:layout_width="250dp" android:layout_height="wrap_content" android:hint="点击右侧快速选择" /> <ImageView android:clickable="true" android:layout_marginTop="10dp" android:layout_alignRight="@id/tv_msg" android:id="@+id/iv_down_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/down_arrow" /> </RelativeLayout>
使用到listview,listview的布局
<?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="match_parent" android:orientation="vertical" > <LinearLayout android:layout_marginBottom="5dp" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/iv_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/user" /> <TextView android:id="@+id/tv_msg" android:gravity="center_horizontal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="000000000" /> <ImageView android:id="@+id/iv_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/delete" /> </LinearLayout> </LinearLayout>
<span style="font-size:18px;">package comflyou.down.select; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.renderscript.Type.CubemapFace; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; public class MainActivity extends Activity { private EditText msg; private ImageView image; private PopupWindow popupWindow; private ArrayList<String> dataList; private ListView myListview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); msg = (EditText) findViewById(R.id.tv_msg); image = (ImageView) findViewById(R.id.iv_down_arrow); myListview = new ListView(MainActivity.this); popupWindow = new PopupWindow(getApplicationContext()); // 填充数据 dataList = new ArrayList<String>(); for (int i = 0; i < 20; i++) { dataList.add("flyou" + i); } // 初始化数据 init(); // 初始化lictview initListView(); } private void init() { image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 设置PopupWindow的宽度 popupWindow.setWidth(msg.getWidth()); // 设置PopupWindow的高度 popupWindow.setHeight(300); popupWindow.setContentView(myListview); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(msg, 0, 0); } }); } private void initListView() { myListview.setBackgroundResource(R.drawable.listview_background); // 设置listView // 背景 myListview.setDivider(null); // 设置条目之间的分隔线为null myListview.setVerticalScrollBarEnabled(false); // 关闭 myListview.setAdapter(new MyAdapter()); } public class MyAdapter extends BaseAdapter { @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(getApplicationContext(), R.layout.list_item, null); holder = new ViewHolder(); holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete); holder.iv_user = (ImageView) convertView.findViewById(R.id.iv_user); holder.tv_msg=(TextView)convertView.findViewById(R.id.tv_msg); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_msg.setText(dataList.get(position)); holder.iv_delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 移除数据 dataList.remove(position); //更新listview MyAdapter.this.notifyDataSetChanged(); } }); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { msg.setText(dataList.get(position)); popupWindow.dismiss(); } }); return convertView; } } //数据缓存 private class ViewHolder { ImageView iv_delete; TextView tv_msg; ImageView iv_user; } } </span>
原文地址:http://blog.csdn.net/u013616976/article/details/44841497