android AlertDialog的简单使用

当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用alertDialog来完成。下面来介绍常用的四种AlertDialog。

1、普通对话框

package com.example.yk.dialogtest;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

/**
 * AlertDialog普通对话框
 */
public class GeneralDialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general_dialog);
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
                .setTitle("操作title")//设置title
                .setMessage("操作message")//设置要显示的message
                .setCancelable(false)//表示点击dialog其它部分不能取消(除了“取消”,“确定”按钮)
                .setPositiveButton("确定", new
                        DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(GeneralDialogActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();

            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
//                dialogInterface.dismiss();
            }
        });
        alertDialog.show();//别忘了show
    }
}


2、单选对话框

package com.example.yk.dialogtest;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

/**
 * 单选对话框
 */
public class SingleDialogActivity extends AppCompatActivity {
    private String[] items={"java","php","c"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_dialog);
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
                .setTitle("提示title")
//                .setMessage("提示message")//在需要设置单选对话框的情况下是不能设置message的,否则单选对话框内容会失效
                .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {//checkedItem=-1表示默认不选中
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(SingleDialogActivity.this, "选中了"+items[i], Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
            alertDialog.show();
    }
}


3、多选对话框

package com.example.yk.dialogtest;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

/**
 * 多选对话框
 */
public class MultiChoiceDialogActivity extends AppCompatActivity {
    private String[] items={"java","php","c"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_choice_dialog);
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
                .setTitle("提示title")
                .setCancelable(false)
                .setMultiChoiceItems(items, new boolean[]{false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {


                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        if(b){
                            Toast.makeText(MultiChoiceDialogActivity.this, "选中了"+items[i], Toast.LENGTH_SHORT)
                                    .show();
                        }

                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
        alertDialog.show();
    }
}


4、进度条对话框

package com.example.yk.dialogtest;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

/**
 * 进度条对话框
 */
public class ProgressDialogActivity extends AppCompatActivity {
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_dialog);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("提示title");
        progressDialog.setCancelable(true);
//        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//旋转进度条,默认风格
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//横向进度条
        progressDialog.show();

    }
}



  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值