How to persist a choice in a Xamarin Android alert dialog

天大地大妈咪最大 提交于 2019-12-31 05:12:12

问题


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

    }

And this is the 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>

How do I 1) show the user's choice more than the line being briefly grayed and 2) how do I persist that choice?


回答1:


Do you want to achieve the result like following GIF?

If so, you can create a ListItem to replace the Android.Resource.Layout.SimpleListItem1

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

<?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"/>

And achieve the listview.ItemClick += Listview_ItemClick;

 private void Listview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {

        e.View.Selected = true;


    }

Here is my demo.

https://github.com/851265601/Xamarin.Android_ListviewSelect



来源:https://stackoverflow.com/questions/59020513/how-to-persist-a-choice-in-a-xamarin-android-alert-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!