(自定义)AlertDialog使用

22 篇文章 0 订阅
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
 9
10
11
12
13
14
15
16
17
18
<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
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值