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

问题描述 投票:0回答: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)如何保持该选择?

xamarin android-alertdialog
1个回答
0
投票

您是否想要获得像遵循GIF一样的结果?

enter image description here

如果是这样,您可以创建一个ListItem来替换Android.Resource.Layout.SimpleListItem1

这是我的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"/>

并实现listview.ItemClick += Listview_ItemClick;

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

        e.View.Selected = true;


    }

© www.soinside.com 2019 - 2024. All rights reserved.