Android ListPopupWindow的使用,android中popupwindow
其實像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() {@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();}}); }}
布局檔案就一個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