必威体育Betway必威体育官网
当前位置:首页 > IT技术

AndroidStudio中AlertDialog的四种用法

时间:2019-10-19 15:45:47来源:IT技术作者:seo实验室小编阅读:51次「手机版」
 

alertdialog

目录

1.默认样式

2.单选弹出框

3.多选弹出框

4.自定义弹出框

补充!!


1.默认样式

Android.support.v7.APP.alertdialog.builder builder = new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder.settitle("请回答");
builder.setmessage("你觉得我好看吗??");
builder.setpositivebutton("当然是好看了!!", new dialoginterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        toast.maketext(AlertDialogActivity.this, "嘻嘻嘻",Toast.LENGTH_SHORT).show();
    }
});
builder.setNeutralButton("我觉得一般", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(AlertDialogActivity.this,"那你再瞅瞅~",Toast.LENGTH_SHORT).show();
    }
});
builder.setNegativeButton("我觉得不好看", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(AlertDialogActivity.this,"嘤嘤嘤",Toast.LENGTH_SHORT).show();
    }
});
builder.show();


2.单选弹出框

final String[] gender = new String[]{"帅哥","美女"};
android.support.v7.app.AlertDialog.Builder builder1=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder1.setTitle("请选择你的性别");
builder1.setItems(gender, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
            ToastUtil.showMsg(AlertDialogActivity.this,gender[which]);
      }
});
builder1.show();

android.support.v7.app.AlertDialog.Builder builder2=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder2.setTitle("你最喜欢吃一下那种菜肴:");
final String[] dish =new String[]{"红烧牛肉","粉蒸排骨","油焖大虾","蒜蓉扇贝"};
builder2.setSingleChoiceItems(dish, 1, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        ToastUtil.showMsg(AlertDialogActivity.this,dish[which]);
         dialog.dismiss();
    }
});
builder2.setCancelable(false);
builder2.show();
break;


3.多选弹出框

String[] dessert = new String[]{"抹茶千层","芒果拿破仑","草莓雪媚娘","蓝莓慕斯"};
boolean[] begin = new boolean[]{false,false,false,false};
android.support.v7.app.AlertDialog.Builder builder3=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder3.setTitle("你吃过以下哪些甜点");
builder3.setMultiChoiceItems(dessert, begin, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

    }
});
builder3.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        ToastUtil.showMsg(AlertDialogActivity.this,"好的,我知道啦!");

    }
});
builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        ToastUtil.showMsg(AlertDialogActivity.this,"难道你都没有吃过??");
    }
});
builder3.show();
break;


4.自定义弹出框

自定义Dialog组件,CustomDialog.java

public class CustomDialog extends Dialog implements View.OnClickListener {
    //声明xml文件里的组件
    private TextView tv_title,tv_message;
    private Button bt_cancel,bt_confirm;

    //声明xml文件中组件中的text变量,为string类,方便之后改
    private String title,message;
    private String cancel,confirm;

    //声明两个点击事件,等会一定要为取消和确定这两个按钮也点击事件
    private IOnCancelListener cancelListener;
    private IOnConfirmListener confirmListener;

    //设置四个组件的内容
    public void setTitle(String title) {
        this.title = title;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public void setCancel(String cancel,IOnCancelListener cancelListener) {
        this.cancel = cancel;
        this.cancelListener=cancelListener;
    }
    public void setConfirm(String confirm,IOnConfirmListener confirmListener){
        this.confirm=confirm;
        this.confirmListener=confirmListener;
    }

    //CustomDialog类的构造方法
    public CustomDialog(@NonNull context context) {
        super(context);
    }
    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    //在app上以对象的形式把xml里面的东西呈现出来的方法!
    @Override
    protected void onCreate(Bundle savedinstanceState) {
        super.onCreate(savedInstanceState);
        //为了锁定app界面的东西是来自哪个xml文件
        setcontentView(R.layout.custom_dialog);

        //设置弹窗的宽度
        WindowManager m = getWindow().getWindowManager();
        display d = m.getDefaultDisplay();
        WindowManager.layoutparams p =getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int)(size.x * 0.8);//是dialog的宽度为app界面的80%
        getWindow().setAttributes(p);

        //找到组件
        tv_title=findViewById(R.id.tv_title);
        tv_message=findViewById(R.id.tv_message);
        bt_cancel=findViewById(R.id.bt_cancel);
        bt_confirm=findViewById(R.id.bt_confirm);

        //设置组件对象的text参数
        if (!TextUtils.isempty(title)){
            tv_title.setText(title);
        }
        if (!TextUtils.isEmpty(message)){
            tv_message.setText(message);
        }
        if (!TextUtils.isEmpty(cancel)){
            bt_cancel.setText(cancel);
        }

        //为两个按钮添加点击事件
        bt_confirm.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
     }

     //重写onClick方法
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_cancel:
                if(cancelListener!=null){
                    cancelListener.onCancel(this);
                }
                dismiss();
                break;
            case R.id.bt_confirm:
                if(confirmListener!=null){
                    confirmListener.onConfirm(this);
                }
                dismiss();//按钮按之后会消失
                break;
        }
    }

    //写两个接口,当要创建一个CustomDialog对象的时候,必须要实现这两个接口
    //也就是说,当要弹出一个自定义dialog的时候,取消和确定这两个按钮的点击事件,一定要重写!
    public interface IOnCancelListener{
        void onCancel(CustomDialog dialog);
    }
    public interface IOnConfirmListener{
        void onConfirm(CustomDialog dialog);
    }
}

使用自定义的弹出框组件

CustomDialog customDialog = new CustomDialog(CustomDialogActivity.this);
customDialog.setTitle("提醒");
customDialog.setMessage("你确定要删除吗?");
customDialog.setCancel("cancel", new CustomDialog.IOnCancelListener() {
    @Override
    public void onCancel(CustomDialog dialog) {
        Toast.makeText(CustomDialogActivity.this, "取消成功!",Toast.LENGTH_SHORT).show();
    }
});
customDialog.setConfirm("confirm", new CustomDialog.IOnConfirmListener(){
    @Override
    public void onConfirm(CustomDialog dialog) {
        Toast.makeText(CustomDialogActivity.this, "确认成功!",Toast.LENGTH_SHORT).show();
    }
});
customDialog.show();


以上代码中用到的ToastUtil类,是我自定义的一个类,目的是把Toast的使用稍微封装一下,偷一下懒。代码如下:

public class ToastUtil {
    public static Toast toast;
    public static void showMsg(Context context,String msg){
        if(toast==null){
            toast=Toast.makeText(context,msg,Toast.LENGTH_LONG);
        }else{
            toast.setText(msg);
        }
        toast.show();
    }
}

如果还有问题,欢迎告诉我哦~

相关阅读

关于AlertDialog的一些

 setView与setContentView的区别 看下面一段代码: AlertDialog.Builder builder = new AlertDialog.Builder(this); b

AlertDialog

package com.shenzhou.robot.custom.dialog; import android.app.Dialog; import android.content.Context; import android.vie

分享到:

栏目导航

推荐阅读

热门阅读