Xamarin.Android ダイアログ表示
【 Xamarin 記事一覧 】
まずは、基本?という事で各種のダイアログを表示してみました。
1 確認ダイアログ

AlertDialogのコンストラクタは全部protectedになっていて、直接使用することはできないので、AlertDialog.Builderを使用するようです。
WindowsのMessageBoxによく似ていますが、モーダルでは無いことに注意が必要です。
いろいろ試してみましたが、モーダルのようにスレッドを待機させる書き方は、Androidにはなじみにくいようです。(特にUIスレッドは絶対に止められない)
var dlg = new AlertDialog.Builder(this); dlg.SetTitle("タイトル"); dlg.SetMessage("メッセージ"); dlg.SetPositiveButton( //OKボタンの処理 "OK", (s, a) => Toast.MakeText(this, "OK", ToastLength.Short).Show()); dlg.SetNegativeButton( //Cancelボタンの処理 "Cancel", (s, a) => Toast.MakeText(this, "Cancel", ToastLength.Short).Show()); dlg.Create().Show();
2 リスト表示ダイアログ

先ほどのAlertDialog.Builderで、今度は、SetMessageの代わりに、SetItemsを使いました。
SetItemsでは、同時にイベントも記述します。
itemsをSystem.String[]で指定できるのは、地味に嬉しいです。基本的なオブジェクトで、使い慣れたDotNETのものが使えるのは、Xamarinの大きな魅力です。
調子に乗って、SetMessageとSetItemsを両方指定してみたら、まともに動きませんでしたw
var items = new[] { "item1", "item2", "item3" }; var dlg = new AlertDialog.Builder(this); dlg.SetTitle("タイトル"); dlg.SetItems( items, // リスト選択時の処理 //DialogClickEventArgs a.Which = 選択されたアイテムのインデックス (s, a) => Toast.MakeText(this,items[a.Which],ToastLength.Short).Show()); dlg.Create().Show();
3 チェック表示ダイアログ

SetPositiveButtonによる、OKボタン押下時のイベントでは、全チェック状態の取得ができなさそうなので、チェック状態変化のイベントで、逐次、chk[a.Which] = a.IsChecked;などとして記録しておく必要があるのか・・・も?
var items = new[] { "item1", "item2", "item3" }; //アイテム var chk = new[] { true, false, false };//チェック状態(初期化用) var dlg = new AlertDialog.Builder(this); dlg.SetTitle("タイトル"); dlg.SetMultiChoiceItems( items, chk, //チェック状態変更時の処理 //a.IsChecked チェック状態(変化後の状態)a.Which インデックス (s, a) =>chk[a.Which] = a.IsChecked); dlg.SetPositiveButton("OK", (s, e) =>{ //OK選択時の処理 }); dlg.Create().Show();
4 プログレスダイアログ

SetProgressStyleで、ProgressDialogStyle.HorizontalとProgressDialogStyle.Spinner(ぐるぐる回転するやつ)の2種類が指定できます。
下記のサンプルは、クラスグローバルな変数(private ProgressDialog _dlg;)を使用しています。
_dlg = new ProgressDialog(this); _dlg.SetTitle("スレッド実行中"); _dlg.SetMessage("しばらくお待ちください。"); _dlg.Indeterminate = false; _dlg.SetProgressStyle(ProgressDialogStyle.Horizontal); _dlg.Max = 100; // 最大値 _dlg.IncrementProgressBy(0); // 初期値 _dlg.Show(); // スレッド生成・実行 new System.Threading.Thread(new System.Threading.ThreadStart(() => { for (var i = 0; i < 100; i++){ System.Threading.Thread.Sleep(100); _dlg.Progress+ } _dlg.Dismiss(); // 実行中ダイアログを閉じる })).Start();
5 日付選択ダイアログ

データ取得がSystem.DateTimeなのが嬉しい。
日付処理のライブラリって、言語ごと微妙に違っていて、いろいろ調べるのが手間なのです。
.NETでそのまま書けるメリットは、個人的には大きいです。
var d = DateTime.Now;// 現在の日付を取得 var dlg = new DatePickerDialog(this, (e, a) =>{ //set(設定)ボタンクリック時の処理 // System.DateTime : a.Date }, d.Year, d.Month, d.Day); dlg.Show();
6 時間選択ダイアログ
こちらのデータ取得はSystem.DateTimeではありません。
a.HourOfDay(時) a.Minute(分)で取得します。
const int hour = 10; const int minute = 10; var dlg = new TimePickerDialog(this, (e, a) => { //set(設定)ボタンクリック時の処理 //a.HourOfDay a.Minute }, hour, minute, true); dlg.Show();
7 カスタムダイアログ

AlertDialog.BuilderのSetViewに、自前で用意したビューを指定することでカスタムなダイアログが表示できます。
//ビュー作成 var layout = new LinearLayout(this) { Orientation = Orientation.Vertical }; layout.SetGravity(GravityFlags.Left); var textUser = new EditText(this); var textPass = new EditText(this); layout.AddView(new TextView(this) { Text = "ユーザ名" }); layout.AddView(textUser); layout.AddView(new TextView(this) { Text = "パスワード" }); layout.AddView(textPass); var dlg = new AlertDialog.Builder(this); dlg.SetTitle("タイトル"); dlg.SetView(layout); //<=作成したビューを指定 dlg.SetPositiveButton("OK", (s, a) =>Toast.MakeText(this,String.Format("{0}:{1}",textUser.Text,textPass.Text),ToastLength.Short).Show()); dlg.Create().Show();
ビューは、axmlで作成したものも利用可ですが、この場合は、axml内のポインタの取得にも配慮が必要になります。
var layout = LayoutInflater.Inflate(Resource.Layout.view1, null); var dlg = new AlertDialog.Builder(this); dlg.SetTitle("タイトル"); dlg.SetView(layout); //<=axmlで指定したビューを指定 dlg.SetPositiveButton("OK", (s, a) =>{ //axml内のビューへのポインタの取得 var editText = layout.FindViewById<EditText>(Resource.Id.editText1); }); dlg.Create().Show();