作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我想在对话窗口中显示自动完成 TextView ,如果运行我的应用程序,我无法从 MainActivity 显示对话窗口。谁能帮帮我。
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);
MainActivity.context = getApplicationContext();
Button search = (Button)findViewById(R.id.btn_save);
search.setOnClickListener(showSearch);
}
private OnClickListener showSearch = new OnClickListener()
{
public void onClick(View v)
{
CityListDialog obj=new CityListDialog(context, COUNTRIES);
}
};
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"
};
}
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);
}
}
最佳答案
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 代码
<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>
关于android - 如何在 Android 的对话框窗口中使用自动完成 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27778586/
我是一名优秀的程序员,十分优秀!