- Xamarin 教程
- Xamarin - 主頁
- Xamarin - 安裝
- Xamarin - 第一個應用程式
- Xamarin - 應用程式清單
- Xamarin - Android 資源
- Xamarin - Android 活動生命週期
- Xamarin - 許可權
- Xamarin - 構建應用程式 GUI
- Xamarin - 選單
- Xamarin - 佈局
- Xamarin - Android 小部件
- Xamarin - Android 對話方塊
- Xamarin - 相簿
- Xamarin - Andriod 檢視
- Xamarin - 多屏應用程式
- Xamarin - 部署您的應用程式
- Xamarin 有用資源
- Xamarin - 快速指南
- Xamarin - 有用資源
- Xamarin - 討論
Xamarin - Android 對話方塊
警報對話方塊
在本節中,我們將建立一個按鈕,在點選時顯示一個警報對話方塊。該對話方塊包含兩個按鈕,即,刪除和取消按鈕。
首先,轉到main.axml,並在線性佈局中建立一個新按鈕,如下面的程式碼所示。
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:background = "#d3d3d3"
android:layout_height = "fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "Click to Delete"
android:textColor = "@android:color/background_dark"
android:background = "@android:color/holo_green_dark" />
</LinearLayout>
接下來,開啟MainActivity.cs來建立該警報對話方塊並新增其功能。
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate {
AlertDialog.Builder alertDiag = new AlertDialog.Builder(this);
alertDiag.SetTitle("Confirm delete");
alertDiag.SetMessage("Once deleted the move cannot be undone");
alertDiag.SetPositiveButton("Delete", (senderAlert, args) => {
Toast.MakeText(this, "Deleted", ToastLength.Short).Show();
});
alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => {
alertDiag.Dispose();
});
Dialog diag = alertDiag.Create();
diag.Show();
};
}
完成後,構建並執行您的應用程式來檢視結果。
在上面的程式碼中,我們建立了一個名為 aleartDiag 的警報對話方塊,它具有以下兩個按鈕 -
setPositiveButton - 它包含刪除按鈕操作,當點選時會顯示確認訊息已刪除。
setNegativeButton - 它包含一個取消按鈕,當點選時,只會關閉該警報對話方塊。
廣告