15/8/27/默认和自定义Toast/简单、多选项、单选、多选AlterDialog

Toast

AlterDialog

Toast分为默认的和自定义布局的Toast

1.Toast是独立, 不依赖于Activity,但是不能对他进行操作,因此通常用于提示信息,
2.默认的Toast比较简单其代码如下:

 Toast toast=Toast.makeText(getApplicationContext(),"恭喜你中奖了",Toast.LENGTH_LONG);
                Spanned spanned=Html.fromHtml("我是<img src=''><font color='#ff0000'>暴击</font>", new Html.ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
                        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                        return drawable;
                    }
                }, null);
                toast.setText(spanned);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();

3.上面是Spanner是富文本
4.自定义布局的Toast需要自定义一个布局,并且需要通过调用inflate方法来找到该布局设置成为View
5.自定义Toast的代码如下:

oast toast1=new Toast(getApplicationContext());
                LayoutInflater inflater=getLayoutInflater();
                View toastView=inflater.inflate(R.layout.toast_zidingyi,null);

                TextView textView= (TextView) toastView.findViewById(R.id.textview_toast1);
                textView.setText("弹出自定义Toast标题");

                ImageView imageView= (ImageView) toastView.findViewById(R.id.imageview_toast);
                imageView.setImageResource(R.mipmap.ic_launcher);

                TextView textView1= (TextView) toastView.findViewById(R.id.textview_toast2);
                textView1.setText("弹出自定义Toast结尾");

                toast1.setView(toastView);
                toast1.setDuration(Toast.LENGTH_SHORT);
                toast1.show();

6.最重要的一点是最后toast一定要调用show方法,这样才能进行显示


AlterDialog有多种方式

最重要的一点最后一定要调用creat方法和show方法
1.最简单的AlterDialog实现的代码如下:

 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.mipmap.ic_launcher).setTitle("恭喜").setMessage("恭喜你获得安卓玩偶一个,点击确定获取").setNegativeButton("NegativeButton",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击了NegativeButton", Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("NeutralButton",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击了NeutralButton", Toast.LENGTH_SHORT).show();
            }
        }).setPositiveButton("PositiveButton",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击了PositiveButton", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();

2.多选项的AlterDialog

private String[] mData={"Json","Kasi","Luxi","Major"};
AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
        builder1.setIcon(R.mipmap.ic_launcher);
        builder1.setTitle("标题");
        builder1.setItems(mData, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "支持了第" + which + "位", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog1=builder1.create();
        dialog1.show();

3.只能单选的AlterDialog

private String[] mSections={"Top","Mid","ADC","Sub","Ye"};
    private String mSection;
    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("标题");
        builder.setSingleChoiceItems(mSections,0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mSection=mSections[which];
                Toast.makeText(getApplicationContext(), "选择的位置是:" + mSections[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mButton3.setText(mSection);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

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

4.多选AlterDialog

private String[] mFavoer={"薇恩","伊泽瑞尔","凯特琳","希维尔","格雷福斯","库奇","图奇"};
    private boolean[] mMangerFavoer=new boolean[mFavoer.length];
    private StringBuffer favoer;
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("选择你最喜欢的英雄");
        builder.setMultiChoiceItems(mFavoer, mMangerFavoer, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                mMangerFavoer[which]=isChecked;
            }
        });
        builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                favoer=new StringBuffer();
                for(int i=0;i<mMangerFavoer.length;i++){
                    if(mMangerFavoer[i]){
                        favoer.append(mFavoer[i]);
                    }
                }
                mButton4.setText("ADC:"+favoer);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值