1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="typ" style="color: #660066;">Androidで、選択肢を表示するダイアログの実装方法です。 AlertDialogを利用すれば簡単に作成できます。 <!--more--> AlertDialog</span><span class="pun" style="color: #666600;">.</span><span class="typ" style="color: #660066;">Builder</span><span class="pln" style="color: #000000;"> builder </span><span class="pun" style="color: #666600;">=</span><span class="kwd" style="color: #000088;">new </span><span class="typ" style="color: #660066;">AlertDialog</span><span class="pun" style="color: #666600;">.</span><span class="typ" style="color: #660066;">Builder</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">getActivity</span><span class="pun" style="color: #666600;">());</span><span class="pln" style="color: #000000;"> builder</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: #000000;">setTitle</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">R</span><span class="pun" style="color: #666600;">.</span><span class="kwd" style="color: #000088;">string</span><span class="pun" style="color: #666600;">.</span><span class="pun"><span style="color: #000000;">title</span></span><span class="pun" style="color: #666600;">) // ここにダイアログのタイトルを設定</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: #000000;">setItems</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">R</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: #000000;">array</span><span class="pun" style="color: #666600;">.dialog_array</span><span class="pun" style="color: #666600;">, // ここに選択肢を設定。R.arrayだけでなく、CharSequenceの配列でも可 </span><span class="kwd" style="color: #000088;"> new</span><span class="typ" style="color: #660066;">DialogInterface</span><span class="pun" style="color: #666600;">.</span><span class="typ" style="color: #660066;">OnClickListener</span><span class="pun" style="color: #666600;">()</span><span class="pun" style="color: #666600;">{</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #000088;">public</span><span class="kwd" style="color: #000088;">void</span><span class="pln" style="color: #000000;"> onClick</span><span class="pun" style="color: #666600;">(</span><span class="typ" style="color: #660066;">DialogInterface</span><span class="pln" style="color: #000000;"> dialog</span><span class="pun" style="color: #666600;">,</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: #000000;"> which</span><span class="pun" style="color: #666600;">)</span><span class="pun" style="color: #666600;">{</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #666600;">}</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #666600;">});</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #000088;">return</span><span class="pln" style="color: #000000;"> builder</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: #000000;">create</span><span class="pun" style="color: #666600;">();</span> |