public class MainActivity extends ActionBarActivity {
private EditText mEditText;
private ListPopupWindow mListPop;
private List<String> lists = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lists.add("one");
lists.add("two");
lists.add("three");
mEditText = (EditText) findViewById(R.id.editText1);
mListPop = new ListPopupWindow(this);
mListPop.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lists));
mListPop.setWidth(LayoutParams.WRAP_CONTENT);
mListPop.setHeight(LayoutParams.WRAP_CONTENT);
mListPop.setAnchorView(mEditText);//设置ListPopupWindow的锚点,即关联PopupWindow的显示位置和这个锚点
mListPop.setModal(true);//设置是否是模式
mListPop.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mEditText.setText(lists.get(position));
mListPop.dismiss();
}
});
mEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListPop.show();
}
});
}
}
布局就一个Edittext
<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="com.example.listpopupwindow.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</RelativeLayout>
http://blog.csdn.net/u010687392/article/details/45789517
http://blog.csdn.net/zhangphil/article/details/48900349
网友评论