Android ListPopupWindow的使用
其實像ListPopupWindow、PopupMenu的用法大致和PopupWindow的一樣!就不講了,相信用過PopupWindow的看一下就能明白。
先上個:
ListPopupWindow可以用在下拉式清單、查詢結果顯示、記錄、提示輸入等,當然PopupMenu也可以的。
直接貼代碼吧:
public class MainActivity extends ActionBarActivity {private EditText mEditText;private ListPopupWindow mListPop;private List lists = new ArrayList(); @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(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() {@Overridepublic void onItemClick(AdapterView parent, View view,int position, long id) {mEditText.setText(lists.get(position));mListPop.dismiss();}}); mEditText.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mListPop.show();}}); }}