Android_对话框

目录

一、消息提示对话框

二、确认对话框

三、列表对话框

四、单选对话框

五、多选对话框

六、自定义对话框

七、PopupWindow

八、日期选择器

九、时间选择器

十、按返回键确认退出

十一、点击两次返回退出

 

十二、进度框

 

 

 

一、消息提示对话框

//1.创建AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);

//2.设置图标、标题、内容、按钮等区域
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("消息提示");
builder.setMessage("消息提示");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,"点击了确定",Toast.LENGTH_SHORT).show();
    }
} );

//3.调用create()方法创建,调用show()方法显示对话框
builder.create().show();
View Code

 

二、确认对话框

new AlertDialog.Builder(this)
        .setIcon(R.mipmap.ic_launcher)
        .setTitle("确认退出")
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //结束当前Activity生命周期
                finish();
                //Toast.makeText(MainActivity.this,"点击了确定",Toast.LENGTH_SHORT).show();
            }
        } )
        .setNegativeButton("取消",null)
        .show();
View Code

 

三、列表对话框

final String[] arr = {
        "江苏",
        "常州",
        "钟楼区"
};
new AlertDialog.Builder(this)
        .setIcon(R.mipmap.ic_launcher)
        .setTitle("地区")
        .setItems(arr, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //显示用户选择
                Toast.makeText(MainActivity.this,arr[which],Toast.LENGTH_SHORT).show();
            }
        })
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        } )
        .show();
View Code

 

四、单选对话框

int selectedId = 0 ;
public void onClick_4(View v) {
    final String[] arr = {
            "1星","2星","3星","4星","5星"
    };
    new AlertDialog.Builder(this)
            .setIcon(R.mipmap.ic_launcher)
            .setTitle("评分")
            .setSingleChoiceItems(arr, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selectedId = which;
                    Toast.makeText(MainActivity.this,"你选择了:"+arr[which],Toast.LENGTH_SHORT).show();
                }
            })
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this,"你确定了:"+arr[selectedId],Toast.LENGTH_SHORT).show();
                }
            } )
            .show();
}
View Code

 

五、多选对话框

ArrayList<Integer> choice = new ArrayList <Integer>();
public void onClick_5(View v) {
    final String[] arr = {
            "1星","2星","3星","4星","5星"
    };
    choice.clear();
    new AlertDialog.Builder(this)
            .setIcon(R.mipmap.ic_launcher)
            .setTitle("评分")
            .setMultiChoiceItems(
                    arr,
                    new boolean[]{false, false, false, false, false},
                    new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                            if(isChecked){
                                choice.add(which);
                            }else{
                                choice.remove(which);
                            }
                            Toast.makeText(MainActivity.this,"你选择了:"+choice.toString(),Toast.LENGTH_SHORT).show();
                        }
                    }
            )
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this,"你确定了:"+choice.toString(),Toast.LENGTH_SHORT).show();
                }
            } )
            .show();
}
View Code

 

六、自定义对话框

1.自定义.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="wrap_content"
    android:orientation="vertical"
    android:background="#fff"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#0e2438"
        android:gravity="center"
        android:orientation="horizontal"
        >
        <TextView
            android:text="提醒"
            android:textSize="18sp"
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp"
        >
        <TextView
            android:text="恭喜您,验证码发送成功"
            android:textSize="18sp"
            android:textColor="#000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn"
            android:text="确定"
            android:textColor="#fff"
            android:textSize="16sp"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_selector"
            android:layout_marginTop="10dp"/>

    </LinearLayout>

</LinearLayout>
View Code

2.编写Activity

Dialog dialog;
public void onClick_6(View v) {
    //1.获取LayoutInflater对象
    LayoutInflater inflater = LayoutInflater.from(this);

    //2.调用inflate()方法获取View对象
    final View myView = inflater.inflate(R.layout.layout_myself_dialog,null);

    //3.调用Builder对象的setView()方法设置View
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(myView);

    //4.获取输入内容或者监听点击事件等
    myView.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog = builder.create();
    dialog.show();

}
View Code

 

七、PopupWindow

    PopupWindow popup;
    public void onClick_7(View v) {
        //1.获取LayoutInflater对象
        LayoutInflater inflater = LayoutInflater.from(this);

        //2.调用inflate()方法获取View对象
        final View myView = inflater.inflate(R.layout.layout_myself_dialog,null);

        //3.创建PopupWindow对象
        popup = new PopupWindow(myView,600,550);//px

        //4.获取输入内容或者监听点击事件等
        myView.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popup.dismiss();
            }
        });

        //5.调用PopupWindow显示方法
        //以下拉显示窗口
        //popup.showAsDropDown(v);
        //在指定位置上显示窗口
        popup.showAtLocation(v, Gravity.CENTER,0,0);//偏移量

    }
View Code

 

八、日期选择器

Calendar c = Calendar.getInstance();
        // 直接创建一个DatePickerDialog对话框实例,并将它显示出来
        new DatePickerDialog(this,
                // 绑定监听器
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker dp, int year,
                                          int month, int dayOfMonth) {
                        String text = "您选择了:" + year + "年" + (month + 1)
                                + "月" + dayOfMonth + "日";
                        Toast.makeText(MainActivity.this, text,
                                Toast.LENGTH_LONG).show();
                    }
                }
                //设置初始日期
                , c.get(Calendar.YEAR)
                , c.get(Calendar.MONTH)
                , c.get(Calendar.DAY_OF_MONTH)).show();
View Code

 

九、时间选择器

Calendar c = Calendar.getInstance();
        // 创建一个TimePickerDialog实例,并把它显示出来。
        new TimePickerDialog(this,
                // 绑定监听器
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker tp, int hourOfDay,
                                          int minute) {
                        String text = "您选择了:" + hourOfDay + "时" + minute
                                + "分";
                        Toast.makeText(MainActivity.this, text,
                                Toast.LENGTH_LONG).show();
                    }
                }
                //设置初始时间
                , c.get(Calendar.HOUR_OF_DAY)
                , c.get(Calendar.MINUTE)
                //true表示采用24小时制
                , true).show();
View Code

 

十、按返回键确认退出

    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("确认退出")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //结束当前Activity生命周期
                        System.exit(0);
                        //Toast.makeText(MainActivity.this,"点击了确定",Toast.LENGTH_SHORT).show();
                    }
                } )
                .setNegativeButton("取消",null)
                .show();
    }
View Code

 

十一、点击两次返回退出

    private long mExitTime;
    public void onBackPressed() {

        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(this, "在按一次退出",
                    Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }
View Code

 

十二、进度框

    ProgressDialog pd;
    Handler handler = new Handler(){
        //主线程
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                pd.dismiss();
            }else if (msg.what == 1){
                pd.setProgress(msg.arg1);
            }
        }
    };

    public void onClick(View v){
        showProgress();
        //新建子线程
        new Thread(){
            @Override
            public void run() {
                for(int i = 1 ;i<= 10 ; i++){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message msg = Message.obtain();
                    msg.arg1 = i;
                    msg.what = 1;
                    handler.sendMessage(msg);
                }
                handler.sendEmptyMessage(0);
            }
        }.start();
    }

    /*
    * 环形进度框
    * */
    public void showProgress_1(){
        pd = new ProgressDialog(this);
        pd.setTitle("任务执行中");
        pd.setMessage("任务执行中,请稍候...");
        //设置可取消
        pd.setCancelable(true);
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.show();
    }

    /*
    * 水平进度框
    * */
    public void showProgress(){
        pd = new ProgressDialog(this);
        pd.setTitle("任务执行中");
        pd.setMessage("任务执行中,请稍候...");
        //设置可取消
        pd.setCancelable(true);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(10);
        pd.show();
    }
View Code

 

posted @ 2017-03-11 13:35  Core丨  阅读(162)  评论(0编辑  收藏  举报