Xamarin-andriod-dialogs

提供:Dev Guides
移動先:案内検索

Xamarin-Androidダイアログ

警告ダイアログ

このセクションでは、クリックするとアラートダイアログボックスを表示するボタンを作成します。 ダイアログボックスには、2つのボタン、 Delete および Cancel ボタンが含まれています。

まず、 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();
   };
}

完了したら、アプリケーションをビルドして実行し、結果を表示します。

削除の確認

上記のコードでは、alertDiagというアラートダイアログを作成し、次の2つのボタンを備えています-

  • setPositiveButton -クリックすると確認メッセージ Deleted を表示する Delete ボタンアクションが含まれます。
  • setNegativeButton -クリックすると、アラートダイアログボックスを単純に閉じる*キャンセル*ボタンが含まれます。