转载:在 android dialog中使用Autocompletetext

我想再android Dialog中使用 autocompletetext, 在开发测试过程中碰见不少问题,把最后的成品发布出来,避免大家走弯路,下面的代码是完整代码
详细代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
 
public class MainActivity extends Activity {
    private static Context context;
 
   Button okButton;
 
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
       final View addView = getLayoutInflater().inflate(R.layout.activity_main, null);
     context = this.getApplicationContext();
      Button search = (Button)findViewById(R.id.btn_save);
      search.setOnClickListener(showSearch);
        }
 
   private OnClickListener showSearch = new OnClickListener()
     {
 
       public void onClick(View v)
       {
              showDialogList();
       }
 
 
         };
 
   public static Context getAppContext() {
       return MainActivity.context;
   }
 
   static final String[] COUNTRIES = new String[] {
         "Afghanistan""Albania""Algeria""American Samoa""Andorra",
         "Angola""Anguilla""Antarctica""Antigua and Barbuda",
         "Argentina","Armenia""Aruba""Australia""Austria""Azerbaijan",
         "Bahrain""Bangladesh""Barbados""Belarus""Belgium"
       };
 
 
   private void showDialogList() {
        final Dialog dialog = new Dialog(this);
        View view = getLayoutInflater().inflate(R.layout.citylistview,null);
 
        ListView lv = (ListView) view.findViewById(R.id.List);
        final ArrayAdapter<String> adapter= new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, COUNTRIES);
        lv.setAdapter(adapter);
        AutoCompleteTextView  filterText = (AutoCompleteTextView ) view.findViewById(R.id.EditBox);
        filterText.setAdapter(adapter);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
 
        lv.setOnItemClickListener(new OnItemClickListener() {
 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
 
            }
        });
        dialog.show();
        filterText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
 
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
 
            }
            @Override
            public void afterTextChanged(Editable s) {
                 adapter.getFilter().filter(s);
            }
        });
    }
}


citylistview.xml 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <AutoCompleteTextView
        android:id="@+id/EditBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
 >
 </AutoCompleteTextView>
        <ListView
            android:id="@+id/List"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
 
</LinearLayout>


CityListDialog.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class CityListDialog extends Dialog implements OnClickListener {
 
private ListView list;
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
private static final String TAG = "CityList";
 
public CityListDialog(Context context, String[] cityList) {
    super(context);
 
    setContentView(R.layout.citylistview);
    this.setTitle("Select City");
    filterText = (EditText) findViewById(R.id.EditBox);
    filterText.addTextChangedListener(filterTextWatcher);
    list = (ListView) findViewById(R.id.List);
    adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, cityList);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            Log.d(TAG, "Selected Item is = "+list.getItemAtPosition(position));
                }
    });
}
public void onClick(View v) {
}
private TextWatcher filterTextWatcher = new TextWatcher() {
 
    public void afterTextChanged(Editable s) {
    }
 
    public void beforeTextChanged(CharSequence s, int start, int count,int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before,int count) {
        adapter.getFilter().filter(s);
    }
};
@Override
public void onStop(){
    filterText.removeTextChangedListener(filterTextWatcher);
}
}


AutoCompleteTextView组件是一个可编辑的文本视图,能显示用户键入的相关信息。建议列表显示一个下拉菜单,用户可以从中选择一项,以完成输入。建议列表是从一个数据适配器获取的数据。它有3个重要的方法。
 clearListSelection():清除选中的列表项。
 dismissDropDown():如果存在关闭下拉菜单。
 getAdapter():获取适配器。

恶魔的眼睛:http://www.cnblogs.com/ablansetaimeng/
原文地址:https://www.cnblogs.com/ablansetaimeng/p/4212736.html