其实像ListPopupWindow、PopupMenu的用法大致和PopupWindow的一样!就不讲了,相信用过PopupWindow的看一下就能明白。
先上个效果图:
ListPopupWindow可以用在下拉列表、查询结果显示、历史记录、提示输入等,当然PopupMenu也可以的。
直接贴代码吧:
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