Android小項目之---吃飯選哪?--》選擇對話框(附源碼)
2010-05-23 21:23 Terry_龙 阅读(1266) 评论(2) 编辑 收藏 举报還記得早先我們做的記算器的例子嗎?當中的驗證判斷用到了對話框,今天我們來做一個不一樣的對話框,要做的這個小例子是一個可供選擇效果的對話框
即層層迭迭的Alert Dialog;界面方面我們擺放一個Button來做一個按鈕事件,解發這個按鈕事件后,再通過類似列表項目的方式呈現在Alert Dialog 裏面。
功能用途:投票、選擇器、遙控器等類型。。。最后返回程序,取得用戶選擇菜單項目結果。
效果圖:
![](https://images.cnblogs.com/cnblogs_com/terryblog/Choose.png)
![](https://images.cnblogs.com/cnblogs_com/terryblog/choose2.png)
程序中用到的數據資源如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<resources>
<string name="hello">K..L.. 好餓呀,要上哪吃飯</string>
<string name="app_name">有選擇的對話框</string>
<string name="Choose">按我開始</string>
<string name="Tittle">按我開始選擇</string>
<string name="body">你選選擇的是:</string>
<string name="Ok">確定</string>
<string-array name="Dialog">
<item>韓國餐廳</item>
<item>一站式餐廳</item>
<item>回家湘</item>
<item>富州酒店</item>
</string-array>
</resources>
<string name="hello">K..L.. 好餓呀,要上哪吃飯</string>
<string name="app_name">有選擇的對話框</string>
<string name="Choose">按我開始</string>
<string name="Tittle">按我開始選擇</string>
<string name="body">你選選擇的是:</string>
<string name="Ok">確定</string>
<string-array name="Dialog">
<item>韓國餐廳</item>
<item>一站式餐廳</item>
<item>回家湘</item>
<item>富州酒店</item>
</string-array>
</resources>
功能代碼如下:因為使用了選擇功能的對話框,可能初期看代碼有點複雜,建議利用eclipse的文檔大綱先把代碼層次分好,有利用觀看和理解代碼。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1
2 import android.app.Activity;
3 import android.app.AlertDialog;
4 import android.content.DialogInterface;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.*;
9 public class ChosseDialog extends Activity {
10 private Button mButton;
11 private TextView mTextView;
12 private CharSequence Text;
13 /** Called when the activity is first created. */
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17 mButton=(Button)findViewById(R.id.Button01);
18 mTextView=(TextView)findViewById(R.id.TextView01);
19 mButton.setText(R.string.Choose);
20 mTextView.setText("請選擇");
21 mButton.setOnClickListener(new OnClickListener() {
22
23 @Override
24 public void onClick(View v) {
25 // TODO Auto-generated method stub
26 new AlertDialog.Builder(ChosseDialog.this)
27 .setTitle(R.string.hello)
28 .setItems(R.array.Dialog, new DialogInterface.OnClickListener() {
29
30 @Override
31 public void onClick(DialogInterface dialog, int which) {
32 // TODO Auto-generated method stub
33 CharSequence strDialog=getString(R.string.body);
34 String[] aryShop=getResources().getStringArray(R.array.Dialog);
35 Text=strDialog+aryShop[which];
36 new AlertDialog.Builder(ChosseDialog.this)
37 .setTitle(R.string.Tittle)
38 .setMessage(Text)
39 .setNegativeButton(R.string.Ok, new DialogInterface.OnClickListener() {
40
41 @Override
42 public void onClick(DialogInterface dialog, int which) {
43 // TODO Auto-generated method stub
44 mTextView.setText(Text);
45 }
46 }).show();
47 }
48 })
49 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
50
51 @Override
52 public void onClick(DialogInterface dialog, int which) {
53 // TODO Auto-generated method stub
54 dialog.dismiss();
55 }
56 })
57 .show();
58 }
59 });
60 }
2 import android.app.Activity;
3 import android.app.AlertDialog;
4 import android.content.DialogInterface;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.*;
9 public class ChosseDialog extends Activity {
10 private Button mButton;
11 private TextView mTextView;
12 private CharSequence Text;
13 /** Called when the activity is first created. */
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17 mButton=(Button)findViewById(R.id.Button01);
18 mTextView=(TextView)findViewById(R.id.TextView01);
19 mButton.setText(R.string.Choose);
20 mTextView.setText("請選擇");
21 mButton.setOnClickListener(new OnClickListener() {
22
23 @Override
24 public void onClick(View v) {
25 // TODO Auto-generated method stub
26 new AlertDialog.Builder(ChosseDialog.this)
27 .setTitle(R.string.hello)
28 .setItems(R.array.Dialog, new DialogInterface.OnClickListener() {
29
30 @Override
31 public void onClick(DialogInterface dialog, int which) {
32 // TODO Auto-generated method stub
33 CharSequence strDialog=getString(R.string.body);
34 String[] aryShop=getResources().getStringArray(R.array.Dialog);
35 Text=strDialog+aryShop[which];
36 new AlertDialog.Builder(ChosseDialog.this)
37 .setTitle(R.string.Tittle)
38 .setMessage(Text)
39 .setNegativeButton(R.string.Ok, new DialogInterface.OnClickListener() {
40
41 @Override
42 public void onClick(DialogInterface dialog, int which) {
43 // TODO Auto-generated method stub
44 mTextView.setText(Text);
45 }
46 }).show();
47 }
48 })
49 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
50
51 @Override
52 public void onClick(DialogInterface dialog, int which) {
53 // TODO Auto-generated method stub
54 dialog.dismiss();
55 }
56 })
57 .show();
58 }
59 });
60 }
功能模塊到此一個簡單的選擇功能己經成型。。。
如有不懂之處請QQ:285735942 或 Email:terryyhl@gmail.com