gpt4 book ai didi

android - 如何在 Android 的对话框窗口中使用自动完成 TextView

转载 作者:行者123 更新时间:2023-11-29 14:48:16 25 4
gpt4 key购买 nike

在我的应用程序中,我想在对话窗口中显示自动完成 TextView ,如果运行我的应用程序,我无法从 MainActivity 显示对话窗口。谁能帮帮我。

主 Activity .java

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"
};
}

城市列表对话框.java

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);
}
}

For example this image for show the autocompletetext in dialog window

最佳答案

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com