Android studio AlertDialog对话框

用法1
AlertDialog(1)

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/but"
    android:onClick="Dialog"
    android:text="退出登录"
    />

java部分:

  public void Dialog(View view) {
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("温馨提示") //提示
                .setMessage("确定要退出登录吗?")  //内容
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "已取消退出登录", Toast.LENGTH_SHORT).show();
                    }
                })  //取消,对应监听事件
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "已退出登录", Toast.LENGTH_SHORT).show();
                    }
                })  //确定,对应监听事件
                .create();  //创建
        alertDialog.show(); //显示对话框

    //Negative 否定的,拒绝的
    //Positive 积极的
    }

效果:

用法2

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/but"
        android:onClick="Dialog"
        android:text="设置字体大小"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="一行字体"
        android:id="@+id/tv"
        />

java

public class DialogActivity extends AppCompatActivity {
    public String list[]={"小号","默认号","中号","大号","超大号"};
    public int  textsize[]={10,20,30,40,50};
    public int index;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        tv = findViewById(R.id.tv);
    }

    public void Dialog(View view) {
        new AlertDialog.Builder(this)
                .setTitle("请选择字体大小")
                .setSingleChoiceItems(list, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    index=which;
                    }
                })
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
             tv.setTextSize(textsize[index]);
                    }
                })
                .create().show();

    }
}

用法3

xml


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/but"
        android:onClick="Dialog"
        android:text="选择喜欢的兴趣爱好"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:id="@+id/tv"
        />

java

   public void Dialog(View view) {
        new AlertDialog.Builder(this)
                .setTitle("选择喜欢的选项")
                .setMultiChoiceItems(list, state, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    state[which]=isChecked;
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuffer stringBuffer = new StringBuffer();//StringBuffer 是一个可变的字符序列
                        for (int j = 0; j < state.length; j++) {
                        if (state[j]){
                            stringBuffer.append(list[j]).append(" ");//如果 state[j] 为 true,则执行这行代码。
                            // 它将 list 数组中索引为 j 的元素追加到 stringBuffer 对象中,然后追加一个空格。
                            // append 方法用于将指定的字符串添加到 StringBuffer 的末尾。
                        }
                        }
                        TextView tv = findViewById(R.id.tv);
                        tv.setText("我喜欢:"+stringBuffer.toString());
                    }
                })
                .setNegativeButton("取消",null)
                .create().show();
    }

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值