AlertDialog的嵌套--可以选择的对话框

点击Activity中按钮后,会生成一个列表样式的AlertDialog对话框,对话框中的选项可以选择,这个功能可以做投票,选择器等的使用                  
                                
                                         
布局代码main.xml:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
     android:id="@+id/myText1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical|center_horizontal" 
    android:text="@string/hello"
    />
     <Button
    android:id="@+id/myButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="100px"
    android:layout_y="30px"
    android:text="@string/str_button1" />
</AbsoluteLayout>

string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="hello">中午去谁家蹭饭?</string>
  <string name="app_name">饭小统</string>
  <string name="str_button1">按我开始选择</string>
  <string name="str_alert_title">按我开始选择</string>
  <string name="str_alert_body">你选择的是:</string>
  <string name="str_ok">确认</string>
  <array name="items_irdc_dialog">
    <item>大毛家</item>
    <item>二毛家</item>
    <item>三毛家</item>  
  </array>
</resources>


java代码:
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 DialogDemo extends Activity {
    private Button b1;
    private TextView t1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        b1 = (Button)findViewById(R.id.myButton1);
        t1 = (TextView)findViewById(R.id.myText1);
        b1.setOnClickListener(myShowAlertDialog);
        
    }
    
    Button.OnClickListener myShowAlertDialog = new Button.OnClickListener(){
        
        public void onClick(View arg0) {
            new AlertDialog.Builder(DialogDemo.this)
            .setTitle(R.string.str_alert_title)//以列表方式显示alertdialog
            .setItems(R.array.items_irdc_dialog, new DialogInterface.OnClickListener(){
              
                public void onClick(DialogInterface arg0, int whichcountry) {
                    CharSequence strDialogMsg = getString(R.string.str_alert_body);
                    //从资源文件中得到选项数组
                    String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog);
                    //创建一个alertDialog
                    new AlertDialog.Builder(DialogDemo.this)
                    .setMessage(strDialogMsg + aryShop[whichcountry])
                    .setNegativeButton(R.string.str_ok, new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface arg0, int arg1) {
                            /*在这里处理要作的事*/
                        }
                    }).show();
                }
            }).setNegativeButton("取消",new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface d, int arg1) {
                    d.dismiss(); //关闭取得焦点的对话框
                }
            }).show();
        
        }

 }


本文转自:http://www.eoeandroid.com/thread-97122-1-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值