广告
返回顶部
首页 > 资讯 > 移动开发 >8种android 对话框(Dialog)使用方法详解
  • 486
分享到

8种android 对话框(Dialog)使用方法详解

方法dialogAndroid 2022-06-06 08:06:00 486人浏览 八月长安
摘要

本文汇总了Android 8种对话框(Dialog)使用方法,分享给大家供大家参考,具体内容如下 1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种

本文汇总了Android 8种对话框(Dialog)使用方法,分享给大家供大家参考,具体内容如下

1.写在前面

Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮)、列表、单选、多选、等待、进度条、编辑、自定义等多种形式,将在第2部分介绍。
有时,我们希望在对话框创建或关闭时完成一些特定的功能,这需要复写Dialog的create()、show()、dismiss()等方法,将在第3部分介绍。

2.代码示例

2.1 普通Dialog(图1与图2)

2个按钮


public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonNORMal = (Button) findViewById(R.id.button_normal);
    buttonNormal.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        showNormalDialog();
      }
    });
  }
  private void showNormalDialog(){
    
    final AlertDialog.Builder normalDialog = 
      new AlertDialog.Builder(MainActivity.this);
    normalDialog.setIcon(R.drawable.icon_dialog);
    normalDialog.setTitle("我是一个普通Dialog")
    normalDialog.setMessage("你要点击哪一个按钮呢?");
    normalDialog.setPositiveButton("确定", 
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        //...To-do
      }
    });
    normalDialog.setNegativeButton("关闭", 
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        //...To-do
      }
    });
    // 显示
    normalDialog.show();
  }
}

3个按钮



private void showMultiBtnDialog(){
  AlertDialog.Builder normalDialog = 
    new AlertDialog.Builder(MainActivity.this);
  normalDialog.setIcon(R.drawable.icon_dialog);
  normalDialog.setTitle("我是一个普通Dialog").setMessage("你要点击哪一个按钮呢?");
  normalDialog.setPositiveButton("按钮1", 
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      // ...To-do
    }
  });
  normalDialog.setNeutralButton("按钮2", 
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      // ...To-do
    }
  });
  normalDialog.setNegativeButton("按钮3", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      // ...To-do
    }
  });
  // 创建实例并显示
  normalDialog.show();
}

2.2 列表Dialog(图3)


private void showListDialog() {
  final String[] items = { "我是1","我是2","我是3","我是4" };
  AlertDialog.Builder listDialog = 
    new AlertDialog.Builder(MainActivity.this);
  listDialog.setTitle("我是一个列表Dialog");
  listDialog.setItems(items, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      // which 下标从0开始
      // ...To-do
      Toast.makeText(MainActivity.this, 
        "你点击了" + items[which], 
        Toast.LENGTH_SHORT).show();
    }
  });
  listDialog.show();
}

2.3 单选Dialog(图4)


int yourChoice;
private void showSingleChoiceDialog(){
  final String[] items = { "我是1","我是2","我是3","我是4" };
  yourChoice = -1;
  AlertDialog.Builder singleChoiceDialog = 
    new AlertDialog.Builder(MainActivity.this);
  singleChoiceDialog.setTitle("我是一个单选Dialog");
  // 第二个参数是默认选项,此处设置为0
  singleChoiceDialog.setSingleChoiceItems(items, 0, 
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      yourChoice = which;
    }
  });
  singleChoiceDialog.setPositiveButton("确定", 
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      if (yourChoice != -1) {
        Toast.makeText(MainActivity.this, 
        "你选择了" + items[yourChoice], 
        Toast.LENGTH_SHORT).show();
      }
    }
  });
  singleChoiceDialog.show();
}

2.4 多选Dialog(图5)


ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
  final String[] items = { "我是1","我是2","我是3","我是4" };
  // 设置默认选中的选项,全为false默认均未选中
  final boolean initChoiceSets[]={false,false,false,false};
  yourChoices.clear();
  AlertDialog.Builder multiChoiceDialog = 
    new AlertDialog.Builder(MainActivity.this);
  multiChoiceDialog.setTitle("我是一个多选Dialog");
  multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
    new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which,
      boolean isChecked) {
      if (isChecked) {
        yourChoices.add(which);
      } else {
        yourChoices.remove(which);
      }
    }
  });
  multiChoiceDialog.setPositiveButton("确定", 
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      int size = yourChoices.size();
      String str = "";
      for (int i = 0; i < size; i++) {
        str += items[yourChoices.get(i)] + " ";
      }
      Toast.makeText(MainActivity.this, 
        "你选中了" + str, 
        Toast.LENGTH_SHORT).show();
    }
  });
  multiChoiceDialog.show();
}

2.5 等待Dialog(图6)


private void showWaitingDialog() {
  
  ProgressDialog waitingDialog= 
    new ProgressDialog(MainActivity.this);
  waitingDialog.setTitle("我是一个等待Dialog");
  waitingDialog.setMessage("等待中...");
  waitingDialog.setIndeterminate(true);
  waitingDialog.setCancelable(false);
  waitingDialog.show();
}

2.6 进度条Dialog(图7)


private void showProgressDialog() {
  
  final int MAX_PROGRESS = 100;
  final ProgressDialog progressDialog = 
    new ProgressDialog(MainActivity.this);
  progressDialog.setProgress(0);
  progressDialog.setTitle("我是一个进度条Dialog");
  progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  progressDialog.setMax(MAX_PROGRESS);
  progressDialog.show();
  
  new Thread(new Runnable() {
    @Override
    public void run() {
      int progress= 0;
      while (progress < MAX_PROGRESS){
        try {
          Thread.sleep(100);
          progress++;
          progressDialog.setProgress(progress);
        } catch (InterruptedException e){
          e.printStackTrace();
        }
      }
      // 进度达到最大值后,窗口消失
      progressDialog.cancel();
    }
  }).start();
}

2.7 编辑Dialog(图8)


private void showInputDialog() {
  
  final EditText editText = new EditText(MainActivity.this);
  AlertDialog.Builder inputDialog = 
    new AlertDialog.Builder(MainActivity.this);
  inputDialog.setTitle("我是一个输入Dialog").setView(editText);
  inputDialog.setPositiveButton("确定", 
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(MainActivity.this,
      editText.getText().toString(), 
      Toast.LENGTH_SHORT).show();
    }
  }).show();
}

2.8 自定义Dialog(图9)


<!-- res/layout/dialog_customize.xml-->
<!-- 自定义View -->
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    />
</LinearLayout>
private void showCustomizeDialog() {
  
  AlertDialog.Builder customizeDialog = 
    new AlertDialog.Builder(MainActivity.this);
  final View dialogView = LayoutInflater.from(MainActivity.this)
    .inflate(R.layout.dialog_customize,null);
  customizeDialog.setTitle("我是一个自定义Dialog");
  customizeDialog.setView(dialogView);
  customizeDialog.setPositiveButton("确定",
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      // 获取EditView中的输入内容
      EditText edit_text = 
        (EditText) dialogView.findViewById(R.id.edit_text);
      Toast.makeText(MainActivity.this,
        edit_text.getText().toString(),
        Toast.LENGTH_SHORT).show();
    }
  });
  customizeDialog.show();
}

3.复写回调函数



private void showListDialog() {
  final String[] items = { "我是1","我是2","我是3","我是4" };
  AlertDialog.Builder listDialog = 
    new AlertDialog.Builder(MainActivity.this){
    @Override
    public AlertDialog create() {
      items[0] = "我是No.1";
      return super.create();
    }
    @Override
    public AlertDialog show() {
      items[1] = "我是No.2";
      return super.show();
    }
  };
  listDialog.setTitle("我是一个列表Dialog");
  listDialog.setItems(items, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      // ...To-do
    }
  });
  
  listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    public void onDismiss(DialogInterface dialog) {
      Toast.makeText(getApplicationContext(),
        "Dialog被销毁了", 
        Toast.LENGTH_SHORT).show();
    }
  });
  listDialog.show();
}
您可能感兴趣的文章:Android中自定义对话框(Dialog)的实例代码Android实现底部对话框BottomDialog弹出实例代码Android实现点击AlertDialog上按钮时不关闭对话框的方法实例详解Android自定义ProgressDialog进度条对话框的实现Android中AlertDialog各种对话框的用法实例详解Android 自定义ProgressDialog进度条对话框用法详解Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)Android修改源码解决Alertdialog触摸对话框边缘消失的问题属于自己的Android对话框(Dialog)自定义集合Android自定义Dialog实现通用圆角对话框


--结束END--

本文标题: 8种android 对话框(Dialog)使用方法详解

本文链接: https://www.lsjlt.com/news/25441.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作