Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Xamarin Android警报对话框中保留选择_Xamarin_Android Alertdialog - Fatal编程技术网

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

如何在Xamarin Android警报对话框中保留选择,xamarin,android-alertdialog,Xamarin,Android Alertdialog,此对话框显示正确,但未捕获用户的选择: 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>

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

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

    }
var dialogView=LayoutInflater.Inflate(Resource.Layout.list_视图,null);
Android.App.AlertDialog;
var items=新字符串[]{“A”、“B”、“C”};
var adapter=new ArrayAdapter(这个,Android.Resource.Layout.SimpleListItem1,items);
使用(var dialog=newandroid.App.AlertDialog.Builder(此))
{
对话框。设置标题(“选择字母”);
SetMessage(“只需单击!”);
dialog.SetView(dialogView);
SetNegativeButton(“取消”(s,a)=>{});
对话框.SetPositiveButton(“确定”,(s,a)=>{
{
如果(a.哪个!=-1)
//但我不知道如何坚持这个选择
//当我点击其中一个字母时,它会简短地
//显示选项(背景短暂变灰)
//但这种选择并没有持续下去
//所以当我点击OK,a,它是-1
{
//有选择地做事
}
}});
alertDialog=dialog.Create();
}
dialogView.findviewbyd(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)如何保持该选择?

是否希望获得如下效果

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

这是我的清单

var adapter=new ArrayAdapter(此,Resource.Layout.list_项,项)

列出项目

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

这是我的演示


谢谢。这似乎就是我要找的。我明天会合并它,如果这解决了我的问题,我会在这里报告。非常感谢!!我刚刚下载了你的演示,这似乎完全符合我的需要,所以你解决了这个问题。谢谢!!!但有一个问题:在你的演示中,a总是-1。不知道为什么。使用演示的代码并不重要,但我很好奇。@Ron此值与AlertDialog中的按钮类型有关,如果按钮返回-2,如果按钮返回-1,按钮返回-3。你可以参考这篇安卓文章@Ron这个问题是否有任何更新,如果回复有帮助,请将其标记为答案,这将帮助其他有类似问题的人只需添加一个注释:Leon Lu的答案和github演示完全正确!
<?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"/>
 private void Listview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {

        e.View.Selected = true;


    }