关闭

Android_三种形式Dialog

标签: android布局
68人阅读 评论(0) 收藏 举报
分类:

TODO

android_dialog.xml

**<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/dialog_bt1" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="普通dialog"/>
    <Button
        android:id="@+id/dialog_bt2" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择dialog"/>
    <Button
        android:id="@+id/dialog_bt3" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选dialog"/>



</LinearLayout>
**

activity

package com.lxf;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

/**
 * @address BeiJing
 * @author LiXufei
 * @function  
 */
public class cDialog extends Activity{

    private CharSequence[] items = new CharSequence[]{"北京","上海","深圳","广东","河南","河北","山西","山东"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_dialog);

        /*
         * 普通对话框
         */
        findViewById(R.id.dialog_bt1).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(cDialog.this);
                builder.setTitle("普通对话框");  //设置对话框标题
                builder.setMessage("这是一个普通对话框");    //设置对话框信息
                builder.setNegativeButton("确定", new OnClickListener() { //设置对话框左按钮

                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(cDialog.this, "点击了确定", 0).show();
                    }
                });
                builder.setPositiveButton("取消", new OnClickListener() {     //设置对话框右按钮

                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(cDialog.this, "点击了取消", 0).show();
                    }
                });

                builder.create().show();

            }
        });

        /*
         * 选择对话框
         */
        findViewById(R.id.dialog_bt2).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(cDialog.this);
                builder.setTitle("选择对话框");
                builder.setItems(items, new OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(cDialog.this, "点击了"+items[which], 0).show();
                    }
                });

                builder.create().show();

            }
        });

        /*
         * 单选对话框
         */
        findViewById(R.id.dialog_bt3).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(cDialog.this);
                builder.setTitle("单选对话框");
                builder.setSingleChoiceItems(items, -1, new OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(cDialog.this, "点击了"+items[which], 0).show();
                    }
                });

                builder.create().show();

            }
        });


    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2981次
    • 积分:153
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论