Androidアプリ開発メモ061:独自のAlertDialog
関連する記事:Androidアプリ開発メモ046:スタイルとテーマ
AndroidManifest.xmlでテーマを設定するやり方と、レイアウトをXMLファイルで定義してAlertDialog.Builder#setView()でセットするやり方がある。
テーマを設定して背景色を変える
AlertDialogの背景色を設定したテーマを作り、それをAndroidManifest.xml のapplicaaction要素またはactivity要素にセットする。
ダイアログを出すメソッド/** レイアウトファイルにおいてButton要素のonClick属性に設定されているメソッド。 */ public void showAlertDialogMethod1(View v) { Log.v("TEST", "showAlertDialogMethod1():v.getTag()=" + v.getTag()); MyDialogListener listener = new MyDialogListener(); AlertDialog.Builder myDialogBuilder = new AlertDialog.Builder(this); myDialogBuilder.setTitle("タイトル") .setMessage("メッセージ") .setPositiveButton("Yes", listener) // ボタン押下の処理が必要なければ引数listenerはnullでよい .setNeutralButton("Maybe", listener) .setNegativeButton("No", listener) .setCancelable(false); AlertDialog myAlertDialog = myDialogBuilder.create(); myAlertDialog.show(); }
色の定義のファイル res/values/color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> <color name="green">#00ff00</color> <color name="blue">#0000ff</color> <color name="yellow">#ffff00</color> <color name="magenta">#ff00ff</color> <color name="cyan">#00ffff</color> <color name="pink">#ffc0cb</color> <color name="khaki">#f0e68c</color> <color name="skyblue">#87ceeb</color> <color name="saddlebrown">#8b4513</color> </resources>
テーマ・スタイルを定義したファイル res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme" parent="@android:style/Theme.Black"> <item name="android:alertDialogStyle">@style/AlertDialog</item> </style> <style name="AlertDialog"> <item name="android:fullBright">@color/red</item> <item name="android:fullDark">@color/green</item> <item name="android:topBright">@color/blue</item> <item name="android:topDark">@color/yellow</item> <item name="android:centerBright">@color/magenta</item> <item name="android:centerDark">@color/cyan</item> <item name="android:centerMedium">@color/pink</item> <item name="android:bottomBright">@color/khaki</item> <item name="android:bottomDark">@color/skyblue</item> <item name="android:bottomMedium">@color/saddlebrown</item> </style> </resources>
スタイルの定義のitem要素がよくわからない。なんとかBrightとかなんとかDarkとかなんとかMediumとか、それぞれどういう意味なんだか。
上記の設定・コードでアプリを作ってAVD上で出たボタン付きのAlertDialogは下のイメージのようになる。
上からタイトル、メッセージ、ボタンのAlertDialogでは
タイトル部分の背景はtopDarkで指定したyellow、メッセージ部分の背景はcenterDarkで指定したcyan、ボタンの部分の背景はbottomMediumで指定したsaddlebrown。
メッセージ、ボタンのAlertDialogでは
メッセージ部分の背景はtopDarkで指定したyellow、ボタンの部分の背景はbottomMediumで指定したsaddlebrown。
タイトル、リストのAlertDialogでは
タイトル部分の背景はtopDarkで指定したyellow、リスト部分の背景はbottomBrightで指定したkhaki。
タイトル、ラジオボタン付きリスト、ボタンのAlertDialogでは
タイトル部分の背景はtopDarkで指定したyellow、リストの部分の背景はcenterBrightで指定したmagenta、ボタンの部分の背景はbottomMediumで指定したsaddlebrown。
他のitemについては何の設定なのか不明。AlertController.java のソースをちょっと見てみたけどわからない。lightとかdarkってのがあるのでバックライトの点灯状態によって色が変わるとか、あと選択されているときとされていない時とかいろいろ考えたが、どちらも実際の動きを見た限りでは違うような気がする。
setView()で独自のレイアウトにする
独自のダイアログのレイアウトをXMLファイルで作成し、setView() でセットする。setView() の引数はリソースIDではなくビューである。
ビューはLayoutInflater#inflate()で作成する。
このやり方は自由度は高いがボタンの処理などを自前で実装する必要がある。下記のサンプルプログラムの実装で動きはするが、適切な方法かはわからない。
/** * レイアウトファイルでbutton2のonClick属性に設定されているメソッド。 * ボタンを押すとダイアログのEditTextに入力した値をActivityのTextViewにセットする。 */ public void showAlertDialogMethod2(View v) { //ViewGroup dialogRootLayout = (ViewGroup)findViewById(R.id.dlg2LinearLayout1); //ViewGroup dialogRootLayout = (ViewGroup)findViewById(R.id.dlg2Layout2); // inflate()の第2引数、nullでいいんだろうか?上の2つのようななにかビューを指定すべき? View dialog2 = getLayoutInflater().inflate(R.layout.dialog2, null); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); dialogBuilder .setView(dialog2) .setCancelable(false); final AlertDialog myAlertDialog = dialogBuilder.create(); final EditText dlg2EditText1 = (EditText)dialog2.findViewById(R.id.dlg2EditText1); // ダイアログのボタンの取得とリスナの設定 Button dlg2Button1 = (Button)dialog2.findViewById(R.id.dlg2Button1); dlg2Button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { aTextView.setText(dlg2EditText1.getText()); myAlertDialog.cancel(); } }); myAlertDialog.show(); }
ダイアログのレイアウトファイル res/layout/dialog2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dlg2LinearLayout1" android:orientation="vertical" android:background="@color/pink" android:layout_width="300dp" android:layout_height="200dp"> <EditText android:id="@+id/dlg2EditText1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/dlg2Button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="OK" /> </LinearLayout>
« Windows標準機能でZIPファイルを展開する | Main | #FF11 悪烈・粉砕のアートマをゲット »
「Androidアプリ開発」カテゴリの記事
- Androidアプリ開発メモ068:Tweenアニメーション(2012.11.18)
- Android SDK tools, Revision 21 に更新(2012.11.18)
- Androidアプリ開発メモ067:NDKのサンプルを動かしてみた(2012.09.10)
- Androidアプリ開発メモ066:Android SDK tools Rev.20 で変わった事(2012.07.12)
- Androidアプリ開発メモ065:ライブフォルダ(2012.07.11)
TrackBack
TrackBack URL for this entry:
http://app.cocolog-nifty.com/t/trackback/26461/53887529
Listed below are links to weblogs that reference Androidアプリ開発メモ061:独自のAlertDialog:
Comments