照着书学习了以下例子
- package sucre.android;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class EX03_20 extends Activity {
- private Button mButton;
- private TextView mTextView;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mButton = (Button)findViewById(R.id.myButton);
- mTextView = (TextView)findViewById(R.id.myTextView);
- mButton.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- //定义一个弹出窗口
- new AlertDialog.Builder(EX03_20.this)
- //定义窗口名称
- .setTitle(R.string.str_alert_title)
- //定义窗口列表
- .setItems(R.array.items_irdc_dialog, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- CharSequence strDialogBody = getString(R.string.str_alert_body);
- String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog);
- //当点击每一行时,会弹出一个小的对话框
- new AlertDialog.Builder(EX03_20.this)
- .setMessage(strDialogBody+aryShop[which])
- //确认按钮
- .setNegativeButton(R.string.str_ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- .show();
- }
- })
- //取消按钮
- .setNegativeButton(R.string.str_exit,new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- })
- .show();
- }
- });
- }
- }
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">具有选择功能的对话框</string>
- <string name="app_name">EX03_20</string>
- <string name="str_button">按我开始选择</string>
- <string name="str_alert_title">按我开始选择</string>
- <string name="str_alert_body">你选择的是:</string>
- <string name="str_ok">确认</string>
- <string name="str_exit">取消</string>
- <array name="items_irdc_dialog">
- <item>萌点女佧餐厅</item>
- <item>一锅明日式小火锅</item>
- <item>好吃国巴西烧烤</item>
- </array>
- </resources>
本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/746352,如需转载请自行联系原作者