如何在Xamarin Android警报对话框中保留选择
问题描述:
此对话框正确显示,但未捕获用户的选择:
This dialog box displays correctly, except that the user's choice is not captured:
var dialogView = LayoutInflater.Inflate(Resource.Layout.list_view, null);
Android.App.AlertDialog alertDialog;
var items = new string[] { "A","B","C" };
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
using (var dialog = new Android.App.AlertDialog.Builder(this))
{
dialog.SetTitle("Choose Letter");
dialog.SetMessage("Just Click!");
dialog.SetView(dialogView);
dialog.SetNegativeButton("Cancel", (s, a) => { });
dialog.SetPositiveButton("OK", (s, a) => {
{
if (a.Which!=-1)
//BUT I don't know how to persist the choice
//when I click on one of the letters, it briefly
//shows the choice (the background is briefly grayed
//but the choice doesn't persist
//so when I click OK, a.Which is -1
{
//do things with the choice
}
}});
alertDialog = dialog.Create();
}
dialogView.FindViewById<ListView>(Resource.Id.listview).Adapter = adapter;
alertDialog.Show();
}
这是axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我如何1)向用户显示的选择多于短时显示的线条,以及2)如何保持该选择?
How do I 1) show the user's choice more than the line being briefly grayed and 2) how do I persist that choice?
答
是否要获得遵循GIF的结果?
Do you want to achieve the result like following GIF?
如果是,则可以创建一个ListItem来替换Android.Resource.Layout.SimpleListItem1
If so, you can create a ListItem to replace the Android.Resource.Layout.SimpleListItem1
这是我的ListItem
Here is my ListItem
var adapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, items);
list_item
<?xml version="1.0" encoding="utf-8" ?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/mybackground"
android:gravity="center_vertical"
android:padding="3dp"
android:textSize="20sp"
/>
mybackground
mybackground
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/background_light"
android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@android:color/background_dark"
android:state_pressed="true"/>
<item android:drawable="@color/light_blue" android:state_pressed="false"
android:state_selected="true"/>
并实现listview.ItemClick += Listview_ItemClick;
private void Listview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
e.View.Selected = true;
}
这是我的演示.