如何在 Xamarin 中创建输入弹出窗口 Android
How to create a input popup in Xamarin Android
希望创建一个弹出窗口,用户必须在其中键入 'CONFIRM' 才能继续。我知道如何开发带有 'CONTINUE' 或 'CANCEL' 的弹出窗口,但不确定如何实现 checks/validates 用户输入的弹出窗口。
使用 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();
使用名为 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.csOnCreate 方法中的代码。
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的文本了。
更新:
在Xamarin.forms中,当你想显示提示时,你可以使用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...
}
}
显示弹出窗口:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/pop-ups
希望创建一个弹出窗口,用户必须在其中键入 'CONFIRM' 才能继续。我知道如何开发带有 'CONTINUE' 或 'CANCEL' 的弹出窗口,但不确定如何实现 checks/validates 用户输入的弹出窗口。 使用 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();
使用名为 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.csOnCreate 方法中的代码。
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的文本了。
更新:
在Xamarin.forms中,当你想显示提示时,你可以使用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...
}
}
显示弹出窗口:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/pop-ups