android可编辑的下拉框Demo
最终实现效果:项目目录结构:
EditDropdownTextboxActivity.java
Java代码
1. package com.royal.editDropdownTextbox;
2.
3. import java.util.ArrayList;
4. import java.util.List;
5.
6. import android.app.Activity;
7. import android.content.Context;
8. import android.os.Bundle;
9. import android.view.LayoutInflater;
10. import android.view.MotionEvent;
11. import android.view.View;
12. import android.view.View.OnClickListener;
13. import android.view.View.OnTouchListener;
14. import android.view.ViewGroup;
15. import android.widget.BaseAdapter;
16. import android.widget.EditText;
17. import android.widget.ImageButton;
18. import android.widget.LinearLayout.LayoutParams;
19. import android.widget.ListView;
20. import android.widget.PopupWindow;
21. import android.widget.TextView;
22.
23. /**
24. * 可编辑的下拉框Demo
25. * @author royal
26. *
27. */
28. public class EditDropdownTextboxActivity extends Activity {
29.
30. private ImageButton ibtn_dropDown;
31. private PopupWindow pop;
32. private PopupAdapter adapter;
33. private ListView listView;
34. private EditText et_username;
35. // popupWindow listView是否显示
36. private boolean isShow = false;
37. private List<String> names;
38.
39. @Override
40. public void onCreate(Bundle savedInstanceState) {
41. super.onCreate(savedInstanceState);
42. setContentView(R.layout.main);
43. ibtn_dropDown = (ImageButton) findViewById(R.id.ibtn_dropDown);
44. et_username = (EditText) findViewById(R.id.et_username);
45.
46. // 图片按钮监听
47. setUpIbtnListeners();
48.
49. names = new ArrayList<String>();
50. names.add("曹操");
51. names.add("刘备");
52. names.add("孙权");
53. }
54.
55. /**
56. * 图片按钮监听 www.zzzyk.com
57. */
58. public void setUpIbtnListeners() {
59. ibtn_dropDown.setOnClickListener(new OnClickListener() {
60. @Override
61. public void onClick(View v) {
62. if (pop == null) {
63. if (adapter == null) {
64. adapter = new PopupAdapter(EditDropdownTextboxActivity.this);
65. listView = new ListView(EditDropdownTextboxActivity.this);
66. pop = new PopupWindow(listView, et_username.getWidth(), LayoutParams.WRAP_CONTENT);
67. listView.setAdapter(adapter);
68. pop.showAsDropDown(et_username);
69. isShow = true;
70. }
71. } else if (isShow) {
72. pop.dismiss();
73. isShow = false;
74. } else if (!isShow) {
75. pop.showAsDropDown(et_username);
76. isShow = true;
77. }
78. }
79.
80. });
81. }
82.
83. /**
84. * 自定义适配器,PopupWindow listView的数据处理
85. *
86. * @author royal
87. *
88. */
89. class PopupAdapter extends BaseAdapter {
90.
91. private LayoutInflater layoutInflater;
92. private Context context;
93. 
补充:移动开发 , Android ,