Android Dialog常见使用

20 篇文章 0 订阅

  在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择,从而增强APP与用户之间的交互体验,使得APP灵活性提高了,也达到了Android最初的智能目标。这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助。

    下面就一一介绍Android开发常用的 几种 Dialog模式:

    一、该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。

      效果图:

      创建dialog对话框方法代码如下:

private void dialog() {
    AlertDialog.Builder builder = new Builder(MainActivity.this);
    builder.setMessage("确认退出吗?");
    builder.setTitle("提示");
    builder.setPositiveButton("确认", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
      MainActivity.this.finish();
     }
    });
    builder.setNegativeButton("取消", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
     }
    });
    builder.create().show();
}

      在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法 :

           public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
     dialog();
    }
    return false;
          }

      二、改变了对话框的图表,添加了三个按钮

        效果图:

       创建dialog的方法代码如下:

          private void show3ButtonDialog(){
Dialog dialog = new AlertDialog.Builder(MainActivity.this).setIcon(
       android.R.drawable.btn_star).setTitle("喜好调查").setMessage(
       "你喜欢李连杰的电影吗?").setPositiveButton("很喜欢",
       new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
         // TODO Auto-generated method stub
         Toast.makeText(MainActivity.this, "我很喜欢他的电影。",
           Toast.LENGTH_LONG).show();
        }
       }).setNegativeButton("不喜欢", new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       Toast.makeText(MainActivity.this, "我不喜欢他的电影。", Toast.LENGTH_LONG)
         .show();
      }
     }).setNeutralButton("一般", new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       Toast.makeText(MainActivity.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG)
         .show();
      }
     }).create();
     dialog.show();
}

      三、信息内容是一个简单的View类型

        效果图:

       创建dialog方法的代码如下:

           private void showView(){
new AlertDialog.Builder(this).setTitle("请输入").setIcon(
       android.R.drawable.ic_dialog_info).setView(
       new EditText(this)).setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();
}

       四、信息内容是一组单选框

         效果图:

         创建dialog方法的代码如下:

             private void showCheckSimgleView(){
new AlertDialog.Builder(this).setTitle("单选框").setIcon(
       android.R.drawable.ic_dialog_info).setSingleChoiceItems(
       new String[] { "Item1", "Item2" }, 0,
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
         dialog.dismiss();
        }
       }).setNegativeButton("取消", null).show();
}

       五、信息内容是一组多选框

           效果图:

          创建dialog方法的代码如下:

             private void showCheckView(){
            new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
       new String[] { "Item1", "Item2" }, null, null)
       .setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();
     }

      六、信息内容是一组简单列表项

         效果图:
         创建dialog方法的代码如下:

             private void showCheckListView(){
             new AlertDialog.Builder(this).setTitle("列表框").setItems(
       new String[] { "Item1", "Item2" }, null).setNegativeButton(
       "确定", null).show();
}

    七 、信息内容是一个自定义的布局

      效果图:

       dialog布局文件代码如下:

                <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content" android:layout_width="wrap_content"
   android:background="#ffffffff" android:orientation="horizontal"
   android:id="@+id/dialog">
   <TextView android:layout_height="wrap_content"
     android:layout_width="wrap_content"
    android:id="@+id/tvname" android:text="姓名:" />
   <EditText android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>
  </LinearLayout>

         创建dialog方法的代码如下:

           private void showInputViewDialog(){
            LayoutInflater inflater = getLayoutInflater();
                   View layout = inflater.inflate(R.layout.dialog,
                  (ViewGroup) findViewById(R.id.dialog));
                  new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
                        .setPositiveButton("确定", null)
                         .setNegativeButton("取消", null).show();
           } 

  

    总结:

   通过以上的 七种 Dialog 主要模型,可以自定义出以这些为模板的Dialog各式各样的UI样子,从而到达开发的需求文档目的。Android Dialog在每一个APP中或多或少都用到了,主要是起到 提示、交互 作用,跟网页的Alert一样的效果。

   如以下就是以 第一种 模板样式来自定义的功能:

   /**
* @category退出对话框
*/
public static void showExitDialog(final Activity ctx) {


new AlertDialog.Builder(ctx)
.setIcon(R.drawable.exit)
.setTitle(ctx.getString(R.string.exitAPP))
.setMessage(ctx.getString(R.string.toastExite))
.setPositiveButton(
ctx.getResources().getString(R.string.exitY),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
// 发送广播
ctx.finish();
System.exit(0);// 退出整个程序
android.os.Process
.killProcess(android.os.Process.myPid());
}
})
.setNegativeButton(
ctx.getResources().getString(R.string.exitN),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
}).show();
}

/**
* @category 切换用户
*/
public static void exitUserDialog(final Activity ctx) {


new AlertDialog.Builder(ctx)
.setIcon(R.drawable.exit)
.setTitle("切换用户")
.setMessage("您确定要退出当前用户,切换新的用户?")
.setPositiveButton(
ctx.getResources().getString(R.string.exitY),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Constants.userModel=null;
MyApplication app=(MyApplication) ctx.getApplicationContext();
app.getLockPatternUtils().clearLock();
ctx.startActivity(new Intent(ctx,LoginActivity.class));
AnimationUtil.finishActivityAnimation(ctx);

}
})
.setNegativeButton(
ctx.getResources().getString(R.string.exitN),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
}).show();
}

    


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值