android AlertDialog多种使用方法详解

作者:爱dy 时间:2021-09-14 05:57:10 

当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用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
}
}

android AlertDialog多种使用方法详解

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();
}
}

android AlertDialog多种使用方法详解

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();
}
}

android AlertDialog多种使用方法详解

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();

}
}

android AlertDialog多种使用方法详解

标签:android,AlertDialog
0
投稿

猜你喜欢

  • C#中判断本地系统的网络连接状态的方法

    2023-07-02 15:39:41
  • IDEA与模拟器安装调试失败的处理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES

    2022-08-25 13:57:53
  • c#详解datetime使用示例

    2021-05-26 22:49:04
  • Java rmi远程方法调用基本用法解析

    2023-02-20 08:44:19
  • C#字体池技术实现代码详解

    2022-12-30 06:18:02
  • Android使用GPS获取用户地理位置并监听位置变化的方法

    2022-03-29 14:24:17
  • unity 切换场景不销毁物体问题的解决

    2022-04-29 11:26:06
  • 详解java IO流之缓冲流的使用

    2023-08-08 18:33:16
  • IDEA连接Mysql数据库的详细图文教程

    2023-10-09 09:51:24
  • 详解C# partial 关键字的使用

    2023-06-07 20:46:09
  • JPA如何设置表名和实体名,表字段与实体字段的对应

    2022-07-14 14:36:27
  • Android RecyclerView的简单使用

    2022-08-07 22:04:23
  • Spring Boot项目@RestController使用重定向redirect方式

    2023-12-11 15:21:56
  • Java编程实现对象克隆(复制)代码详解

    2023-02-13 10:12:31
  • Spring中BeanFactory FactoryBean和ObjectFactory的三种的区别

    2022-02-21 22:43:58
  • 企业级Kubernetes管理平台Wayne功能特性介绍

    2021-11-05 08:58:33
  • Android语音识别技术详解及实例代码

    2022-09-23 03:46:33
  • ReentrantLock源码详解--条件锁

    2023-01-01 15:36:22
  • Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码

    2023-11-23 08:23:43
  • springboot日期转换器实现实例解析

    2023-01-31 13:16:45
  • asp之家 软件编程 m.aspxhome.com