Как создать всплывающее окно ввода в Xamarin Android - PullRequest
0 голосов
/ 17 марта 2020

Нужно создать всплывающее окно, в котором пользователь должен нажать «ПОДТВЕРДИТЬ», чтобы продолжить. Я знаю, как разработать всплывающее окно с надписью «ПРОДОЛЖИТЬ» или «ОТМЕНА», но не знаю, как реализовать всплывающее окно, которое проверяет / проверяет вводимые пользователем данные. Использование нативного Xamarin на Android с C#.

Это то, что я имею до сих пор. Мне просто нужен какой-то способ сравнения того, что пользователь вводит со словом CONFIRM

EditText et = new EditText(this);
AlertDialog.Builder ad = new AlertDialog.Builder (this);
ad.setTitle ("Type text");
ad.setView(et); // <----
ad.show();

1 Ответ

0 голосов
/ 18 марта 2020

Создание макета с EditText с именем CustomDialog.xml.

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

Обновлено:

В Xamarin. формы, когда вы хотите отобразить подсказку, вы можете использовать DisplayPromptAsync.

 protected override void OnAppearing()
    {
        base.OnAppearing();
        PopUp();
    }
    public async void PopUp()
    {
        string s = await DisplayPromptAsync("Pop up Window", "Type text:", "Confirm", keyboard: Keyboard.Text);
        if (s == "hello")
        {
           //do something here...
        }
    }

enter image description here

Отображение всплывающих окон: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/pop-ups

...