如何在Xamarin Android中创建输入弹出窗口

问题描述 投票:0回答:1

试图创建一个弹出窗口,用户必须在其中键入'CONFIRM'才能继续。我知道如何开发一个带有'CONTINUE'或'CANCEL'的弹出窗口,但是不确定如何实现检查/验证用户输入的弹出窗口。通过C#在Android上使用本机Xamarin。

这是我到目前为止所拥有的。我只需要某种方法来比较用户输入的单词CONFIRM

EditText et = new EditText(this);
AlertDialog.Builder ad = new AlertDialog.Builder (this);
ad.setTitle ("Type text");
ad.setView(et); // <----
ad.show();
c# validation input xamarin.android dialog
1个回答
0
投票

使用名为CustomDialog.xml的EditText创建布局。

<?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">
   <EditText
       android:id="@+id/editText_Name"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity.cs OnCreate方法中的代码。

   var editText = LayoutInflater.Inflate(Resource.Layout.CustomDialog, null);

        var ad = (new AlertDialog.Builder(this)).Create();
        ad.SetTitle("Type text");
        ad.SetView(editText); // <----
        ad.SetButton("Confirm", ConfirmButton);
        ad.Show();

ConfirmButton的代码。

 void ConfirmButton(object sender, DialogClickEventArgs e)
    {
        var dialog = (AlertDialog)sender;
        var username = (EditText)dialog.FindViewById(Resource.Id.editText_Name);
        var name = username.Text;
        if (name=="hello")
        {

        }
    }

您现在可以获取EditText的文本。

enter image description here

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