public class AllDialogDemoActivity extends Activity {    private static final String TAG = "dzh";    ProgressDialog proDialog;    ProgressDialog progressDialog;    ProgressThread progressThread;    Handler handler = new Handler() {        public void handleMessage(Message msg) {            int total = msg.getData().getInt("total");            progressDialog.setProgress(total);            Log.d(TAG, "setProgress---------------------");            if (total >= 100) {                progressDialog.dismiss();                progressThread.setState(ProgressThread.STATE_DONE);            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    /** 按钮的监听 */    public void ButtonClick(View v) {        switch (v.getId()) {        case R.id.AlertDialog:            showDialog();            break;        }    }    /**     * alertdialog分类选项     * */    public void showDialog() {        final String items[] = { "退出Dialog", "itemsDialog", "单选Dialog",                "等待Dialog", "下载Dialog","自定义Dialog一" ,"自定义Dialog二"};        /** 多选项的Dialog */        AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setTitle("多选项").setIcon(R.drawable.ic_launcher)                .setItems(items, new OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        // 可以在switch中做相应的处理                        switch (which) {                        case 0:                            showDialogOne();                            Toast.makeText(getApplicationContext(),                                    items[which], 1).show();                            break;                        case 1:                            showDialogTwo();                            Toast.makeText(getApplicationContext(),                                    items[which], 1).show();                            break;                        case 2:                            showDialogThree();                            Toast.makeText(getApplicationContext(),                                    items[which], 1).show();                            break;                        case 3:                            showProgressDialogOne();                            break;                        case 4:                            showProgressDialogTwo();                            break;                        case 5:                            showCustomDialogOne();                            break;                        case 6:                            showCustomDialogTwo();                            break;                        }                    }                });        AlertDialog alert = builder.create();        alert.show();    }    /**     * 自定义Dialog       * 直接用AlertDialog使用相对于Dialog因为没有SetcontenView显示自定义的布局,但有setView显示自定义的布局。     * 需要用LayoutInflater 获得view     * */    public void showCustomDialogTwo()    {        View  cus_layout = LayoutInflater.from(AllDialogDemoActivity.this).inflate(R.layout.cus_layout_dialog, null);        AlertDialog.Builder builder =new AlertDialog.Builder(AllDialogDemoActivity.this);        TextView text = (TextView) cus_layout.findViewById(R.id.text);        text.setText("Hello, this is a custom dialog!");        ImageView image = (ImageView) cus_layout.findViewById(R.id.image);        image.setImageResource(R.drawable.ic_launcher);//      builder.setTitle("AlertDialog自定义dialog");  //可以不定义        builder.setView(cus_layout);        AlertDialog alert = builder.create();        alert.show();    }   /** * 自定义Dialog   * 直接用dialong使用 * */public void showCustomDialogOne(){    Dialog dialog = new Dialog(AllDialogDemoActivity.this);    dialog.setContentView(R.layout.cus_layout_dialog);    dialog.setTitle("自定义Dialog");    TextView tv = (TextView) dialog.findViewById(R.id.text);    tv.setText("Hello, this is a custom dialog!");    ImageView  image = (ImageView) dialog.findViewById(R.id.image);    image.setImageResource(R.drawable.ic_launcher);    dialog.show();}       /** 下载进度条 */    public void showProgressDialogTwo() {        progressDialog = new ProgressDialog(AllDialogDemoActivity.this);        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        progressDialog.setMessage("Loading...");        progressThread = new ProgressThread(handler);        progressThread.start();        progressDialog.show();        Log.d(TAG, "showProgressDialogTwo");    }    /**     * 开启线程下载     * */    public class ProgressThread extends Thread {        final static int STATE_DONE = 0;        final static int STATE_RUNNING = 1;        Handler mHandler;        int mState;        int total;        public ProgressThread(Handler h) {            mHandler = h;        }        public void run() {            mState = STATE_RUNNING;            total = 0;            while (mState == STATE_RUNNING) {                try {                    Thread.sleep(100);                    Log.d(TAG, "run"+total);                } catch (InterruptedException e) {                    Log.e("ERROR", "Thread Interrupted");                    e.printStackTrace();                }                /**                 * mHandler.obtainMessage() 从全局地址池中获得一个消息                 * */                Message message = mHandler.obtainMessage();                Bundle b = new Bundle();                b.putInt("total", total);                message.setData(b);                mHandler.sendMessage(message);                total++;            }        }        /*         * sets the current state for the thread, used to stop the thread         */        public void setState(int state) {            mState = state;        }    }    /**     * 第一个进度条dialog     * */    public void showProgressDialogOne() {        proDialog = new ProgressDialog(AllDialogDemoActivity.this);        proDialog.setMessage("Loading.....");        proDialog.show();    }    /**     * 第三种 带单选框的Dialog     * */    public void showDialogThree() {        final CharSequence[] items = { "Red", "Green", "Blue" };        AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setTitle("选择颜色")                // 为 -1说明默认没有被选中,                .setSingleChoiceItems(items, -1,                        new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog,                                    int which) {                                Toast.makeText(getApplicationContext(),                                        items[which], 1).show();                            }                        }).setPositiveButton("确定", new OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.dismiss();                    }                }).setNegativeButton("取消", new OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.cancel();                    }                });        AlertDialog alert = builder.create();        alert.show();    }    /**     * 第二种     * */    public void showDialogTwo() {        final String items[] = { "看书", "敲代码", "玩游戏" };        /** 多选项的Dialog */        AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setTitle("多选项").setItems(items, new OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                // 可以在switch中做相应的处理                switch (which) {                case 0:                    Toast.makeText(getApplicationContext(), items[which], 1)                            .show();                    break;                case 1:                    Toast.makeText(getApplicationContext(), items[which], 1)                            .show();                    break;                }            }        });        AlertDialog alert = builder.create();        alert.show();    }    /**     * 第一种     * */    public void showDialogOne() {        /** dialog最多有三个Button,分别是确定,取消,中立的 */        AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setMessage("你要退出吗?")                .setPositiveButton("是", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        // 退出程序                        finish();                        Toast.makeText(AllDialogDemoActivity.this, "是", 1)                                .show();                    }                })                .setNegativeButton("否", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.cancel();                        Toast.makeText(AllDialogDemoActivity.this, "否", 1)                                .show();                    }                })                .setNeutralButton("中立的", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.cancel();                        Toast.makeText(AllDialogDemoActivity.this, "中立的", 1)                                .show();                    }                });        /** 调用这句话创建dialog */        AlertDialog alert = builder.create();        /** 显示dialog */        alert.show();    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        // switch (keyCode)        // {        // case KeyEvent.KEYCODE_BACK:        // if(proDialog != null)        // {        // proDialog.dismiss();        // }        // break;        //        // }        //        return super.onKeyDown(keyCode, event);    }}

activity_main.xml 布局如下

 1 2 3 4 5 6 7 8 9101112131415161718
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"   ><Button    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="AlertDialog"    android:id="@+id/AlertDialog"    android:onClick="ButtonClick"    ></Button></LinearLayout>

自定义AlertDialog布局如下

 1 2 3 4 5 6 7 8 91011121314151617181920212223
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout_root"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal"    android:padding="10dp" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:layout_marginRight="10dp" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:textColor="#FFF"         android:gravity="center_vertical"        /></LinearLayout>

更多相关文章

  1. Android中ListView,SQLite,BaseAdapter的结合
  2. Android(安卓)深入理解Android中的自定义属性
  3. Android(安卓)屏幕旋转问题
  4. Android开源项目集合
  5. # android笔记 #
  6. Android应用借助LinearLayout实现垂直水平居中布局
  7. Android(安卓)自定义标签 Imagebutton实现ImageButton里放置文字
  8. 模仿墨迹天气设置界面布局
  9. Pro Android学习笔记(五):了解Content Provider(上)

随机推荐

  1. MySQL语句加锁的实现分析
  2. mysql 启动1067错误及修改字符集重启之后
  3. MySQL优化总结-查询总条数
  4. winx64下mysql5.7.19的基本安装流程(详细
  5. MySQL 语句注释方式简介
  6. mysql分页性能探索
  7. MySQL5.7.17安装及配置图文教程
  8. 详细解读分布式锁原理及三种实现方式
  9. mysql Community Server 5.7.19安装指南(
  10. 史上最简单的MySQL数据备份与还原教程(上