如何在Xamarin警报对话框中保留选择

时间:2019-11-24 22:20:48

标签: xamarin android-alertdialog

我不认为这是重复的问题。我可以保留从对话框返回的选择。问题是,它总是-1。

此对话框正确显示,除了未捕获用户的选择:

    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)如何保持该选择?

0 个答案:

没有答案