我想要显示一个ListView
,其中包含一系列城市。当用户点击编辑文本时。我想打开一个对话框,我想要显示ListView
。用户选择我要设置的城市作为编辑文本的值。从编辑中选择值后,如果用户再次触摸编辑,我想打开对话框并将焦点放在用户之前的值上选择了。我们怎么能这样做请帮助我我是android的新手。
这是我的活动文件XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select City"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
答案 0 :(得分:3)
创建布局cities_listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
在EditText OnClickListner
public void searchCitiesList() {
final Dialog dialog = new Dialog(HotelSearch.this);
dialog.setContentView(R.layout.cities_listview);
dialog.setTitle("Select City");
listView = (ListView) dialog.findViewById(R.id.list);
String[] values = new String[] { "Delhi",
"Banglore", "Chennai",
"Luckhow", "Goa",
"Pune", "Agra",
"Dehradun" };
dialog.show();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int itemPosition = position;
String itemValue = (String) listView
.getItemAtPosition(position);
// Show Alert
Toast.makeText(
getApplicationContext(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_LONG).show();
dialog.cancel();
}
});