Dialog对话框的几种方式使用实现
package com.loaderman.dialogdemo; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 提示对话框 */ public void normal(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(this); //设置标题 builder.setTitle("对话框标题"); //设置内容 builder.setMessage("我是对话框信息"); //显示确定按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //点击确定按钮后调用 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "已确定", Toast.LENGTH_SHORT).show(); } }); //显示取消按钮 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //取消 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show(); } }); builder.show(); } /** * 进度条对话框 */ public void progress(View view) { final ProgressDialog pd = new ProgressDialog(this); //设置标题 pd.setTitle("进度条对话框"); //设置最大值 pd.setMax(100); //设置进度条的样式 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); new Thread() { public void run() { try { for (int i = 1; i < 101; i++) { sleep(100); //设置进度条的进度 pd.setProgress(i); } //让进度条对话框消失 pd.dismiss(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); //设置内容 // pd.setMessage("玩命下载中..."); pd.show(); } //列表单选对话框 public void single1(View view) { final String[] items = {"java", ".net", "php"}; new AlertDialog.Builder(MainActivity.this).setTitle("选择语言") .setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } }).show();//显示对话框 } //列表带框的单选对话框 public void single2(View view) { final String[] items = {"java", ".net", "php"}; new AlertDialog.Builder(MainActivity.this).setTitle("选择语言") .setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); dialog.cancel(); } }).show();//显示对话框 } //多选复选框 public void more(View view) { final String[] items = {"java", ".net", "php"}; new AlertDialog.Builder(MainActivity.this).setCancelable(false) .setTitle("选择语言") .setMultiChoiceItems(items, new boolean[]{false, true, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show(); } } }) //选项复选框 .setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialoginterface, int i) { dialoginterface.dismiss(); } }) //确认按钮 .show(); //显示对话框 } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.loaderman.dialogdemo.MainActivity"> <Button android:onClick="normal" android:layout_width="match_parent" android:text="提示对话框" android:layout_height="wrap_content"/> <Button android:onClick="progress" android:layout_width="match_parent" android:text="进度对话框" android:layout_height="wrap_content"/> <Button android:onClick="single1" android:layout_width="match_parent" android:text="列表对话框" android:layout_height="wrap_content"/> <Button android:onClick="single2" android:layout_width="match_parent" android:text="单选带框的对话框" android:layout_height="wrap_content"/> <Button android:onClick="more" android:layout_width="match_parent" android:text=" 多选对话框" android:layout_height="wrap_content"/> </LinearLayout>
效果图:
Context是Activity的父类
父类有的方法, 子类一定有,
子类有的方法,父类不一定有
当show一个Dialog时, 必须传Activity对象, 否则会出异常android.view.WindowManager$BadTokenException: Unable to add window-token null is
not for an application因为Dialog必须依赖Activity为载体才能展示出来,
所以必须将Activity对象传递进去,以后在使用Context的时候, 尽量传递Activity对象, 这样比较安全
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!