Android仿IOS UIAlertView对话框

本文实例为大家分享了Android仿IOS UIAlertView对话框的具体代码,供大家参考,具体内容如下

显示效果:

我在参考链接中看到了作者的仿的qq提示框,但是在使用的时候并不是很方面,有一些不足,于是我参照Android系统AlertDialog,使用参考链接中的布局文件和style文件,用自己的方法自定义了一下这个仿IOS上面UIAlertView的效果,这样的话让我们可以想使用系统AlertDialog一样使用我自定义的CustomDialog。

CustomDialog使用代码:

package com.example.iosalertview; 

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; 

import com.example.iosalertview.CustomDialog.Builder; 

public class MainActivity extends Activity implements OnClickListener{
 private Button ios_dialog_btn,android_dialog_btn; 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main); 

  ios_dialog_btn = (Button) findViewById(R.id.ios_dialog_btn);
  android_dialog_btn = (Button) findViewById(R.id.android_dialog_btn); 

  ios_dialog_btn.setOnClickListener(this);
  android_dialog_btn.setOnClickListener(this); 

 } 

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.ios_dialog_btn:
   CustomDialog.Builder builder = new Builder(MainActivity.this);
   builder.setTitle(R.string.prompt);
   builder.setMessage(R.string.exit_app);
   builder.setPositiveButton(R.string.confirm, null);
   builder.setNegativeButton(R.string.cancel, null);
   builder.create().show();
   break;
  case R.id.android_dialog_btn:
   AlertDialog.Builder mbuilder = new AlertDialog.Builder(MainActivity.this);
   mbuilder.setTitle(R.string.prompt);
   mbuilder.setMessage(R.string.exit_app);
   mbuilder.setPositiveButton(R.string.confirm, null);
   mbuilder.setNegativeButton(R.string.cancel, null);
   mbuilder.create().show();
   break; 

  default:
   break;
  }
 } 

} 

自定义CustomDialog代码:

package com.example.iosalertview; 

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; 

public class CustomDialog extends Dialog { 

 public CustomDialog(Context context) {
  super(context);
 } 

 public CustomDialog(Context context, int theme) {
  super(context, theme);
 } 

 public static class Builder {
  private Context context; //上下文对象
  private String title; //对话框标题
  private String message; //对话框内容
  private String confirm_btnText; //按钮名称“确定”
  private String cancel_btnText; //按钮名称“取消”
  private View contentView; //对话框中间加载的其他布局界面
  /*按钮坚挺事件*/
  private DialogInterface.OnClickListener confirm_btnClickListener;
  private DialogInterface.OnClickListener cancel_btnClickListener; 

  public Builder(Context context) {
   this.context = context;
  } 

  /*设置对话框信息*/
  public Builder setMessage(String message) {
   this.message = message;
   return this;
  } 

  /**
   * Set the Dialog message from resource
   *
   * @param title
   * @return
   */
  public Builder setMessage(int message) {
   this.message = (String) context.getText(message);
   return this;
  } 

  /**
   * Set the Dialog title from resource
   *
   * @param title
   * @return
   */
  public Builder setTitle(int title) {
   this.title = (String) context.getText(title);
   return this;
  } 

  /**
   * Set the Dialog title from String
   *
   * @param title
   * @return
   */
  public Builder setTitle(String title) {
   this.title = title;
   return this;
  } 

  /**
   * 设置对话框界面
   * @param v View
   * @return
   */
  public Builder setContentView(View v) {
   this.contentView = v;
   return this;
  } 

  /**
   * Set the positive button resource and it's listener
   *
   * @param confirm_btnText
   * @return
   */
  public Builder setPositiveButton(int confirm_btnText,
    DialogInterface.OnClickListener listener) {
   this.confirm_btnText = (String) context
     .getText(confirm_btnText);
   this.confirm_btnClickListener = listener;
   return this;
  } 

  /**
   * Set the positive button and it's listener
   *
   * @param confirm_btnText
   * @return
   */
  public Builder setPositiveButton(String confirm_btnText,
    DialogInterface.OnClickListener listener) {
   this.confirm_btnText = confirm_btnText;
   this.confirm_btnClickListener = listener;
   return this;
  } 

  /**
   * Set the negative button resource and it's listener
   *
   * @param confirm_btnText
   * @return
   */
  public Builder setNegativeButton(int cancel_btnText,
    DialogInterface.OnClickListener listener) {
   this.cancel_btnText = (String) context
     .getText(cancel_btnText);
   this.cancel_btnClickListener = listener;
   return this;
  } 

  /**
   * Set the negative button and it's listener
   *
   * @param confirm_btnText
   * @return
   */
  public Builder setNegativeButton(String cancel_btnText,
    DialogInterface.OnClickListener listener) {
   this.cancel_btnText = cancel_btnText;
   this.cancel_btnClickListener = listener;
   return this;
  } 

  public CustomDialog create() {
   LayoutInflater inflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   // instantiate the dialog with the custom Theme
   final CustomDialog dialog = new CustomDialog(context, R.style.mystyle);
   View layout = inflater.inflate(R.layout.customdialog, null);
   dialog.addContentView(layout, new LayoutParams(
     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
   // set the dialog title
   ((TextView) layout.findViewById(R.id.title)).setText(title);
   ((TextView) layout.findViewById(R.id.title)).getPaint().setFakeBoldText(true);;
   // set the confirm button
   if (confirm_btnText != null) {
    ((Button) layout.findViewById(R.id.confirm_btn))
      .setText(confirm_btnText);
    if (confirm_btnClickListener != null) {
     ((Button) layout.findViewById(R.id.confirm_btn))
       .setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         confirm_btnClickListener.onClick(dialog,
           DialogInterface.BUTTON_POSITIVE);
        }
       });
    }
   } else {
    // if no confirm button just set the visibility to GONE
    layout.findViewById(R.id.confirm_btn).setVisibility(
      View.GONE);
   }
   // set the cancel button
   if (cancel_btnText != null) {
    ((Button) layout.findViewById(R.id.cancel_btn))
      .setText(cancel_btnText);
    if (cancel_btnClickListener != null) {
     ((Button) layout.findViewById(R.id.cancel_btn))
       .setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         cancel_btnClickListener.onClick(dialog,
           DialogInterface.BUTTON_NEGATIVE);
        }
       });
    }
   } else {
    // if no confirm button just set the visibility to GONE
    layout.findViewById(R.id.cancel_btn).setVisibility(
      View.GONE);
   }
   // set the content message
   if (message != null) {
    ((TextView) layout.findViewById(R.id.message)).setText(message);
   } else if (contentView != null) {
    // if no message set
    // add the contentView to the dialog body
    ((LinearLayout) layout.findViewById(R.id.message))
      .removeAllViews();
    ((LinearLayout) layout.findViewById(R.id.message)).addView(
      contentView, new LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT));
   }
   dialog.setContentView(layout);
   return dialog;
  } 

 }
}

demo下载地址:Android仿IOS UIAlertView对话框

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android仿QQ消息提示实现弹出式对话框

    本文在<7种形式的Android Dialog使用实例>在这篇文章的基础进行学习,具体内容如下 1.概述 android原生控件向来以丑著称(新推出的Material Design当另说),因此几乎所有的应用都会特殊定制自己的UI样式.而其中弹出式提示框的定制尤为常见,本篇我们将从模仿QQ退出提示框来看一下常见的几种自定义提示框的实现方式. 这里使用的几种弹出框实现方法概括为以下几种: 自定义Dialog 自定义PopupWindow 自定义Layout View Activity的Dialo

  • Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)

    虽然Android给我们提供了众多组件,但是使用起来都不是很方便,我们开发的APK都有自己的风格,如果使用了系统自带的组件,总是觉得和应用的主题不着边际并且看起来也不顺心,那我们就需要自定义了,为了方便大家对自定义组件的学习,我接下来准备了几遍有关自定义的Dialog的文章,希望对大家有帮助. 在开发APK中最常见的估计就数弹出对话框了,这种对话框按照按钮数量来分大致是三种:一个按钮,两个按钮,三个按钮.现在要讲的就是按照按钮数量分为以上三类吧(当然了可以有更多的按钮,只要你愿意). 自定义Di

  • Android中AlertDialog各种对话框的用法实例详解

    目标效果: 程序运行,显示图一的几个按钮,点击按钮分别显示图二到图六的对话框,点击对话框的某一项或者按钮,也会显示相应的吐司输出. 1.activity_main.xml页面存放五个按钮. activity_main.xml页面: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&

  • android 对话框弹出位置和透明度的设置具体实现方法

    例如,屏幕的上方或下方.要实现这种效果.就需要获得对话框的Window对象,获得这个Window对象有多种方法.最容易的就是直接通过AlertDialog类的getWindow方法来获得Window对象. 复制代码 代码如下: AlertDialog dialog = new AlertDialog.Builder(this).setTitle("title")                       .setMessage("message").create(

  • Android加载对话框同时异步执行实现方法

    Android中通过子线程连接网络获取资料,同时显示加载进度对话框给用户的操作,需要Thread和Handler来完成,在Thread中执行比较耗时的代码,完成后再通过Handler发送消息给主线程,由主线程刷新UI. 在实现上比较的烦琐,为简化此方法,花了点时间封装了Thread和Handler,现在通过简单的代码就可以实现相同的功能,而把更多精力放到业务逻辑处理上! 效果如图:   复制代码 代码如下: LoadingDialog loadingDialog = new LoadingDia

  • Android中自定义对话框(Dialog)的实例代码

    1.修改系统默认的Dialog样式(风格.主题) 2.自定义Dialog布局文件 3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类 第一步: 我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了

  • 实例详解Android自定义ProgressDialog进度条对话框的实现

    Android SDK已经提供有进度条组件ProgressDialog组件,但用的时候我们会发现可能风格与我们应用的整体风格不太搭配,而且ProgressDialog的可定制行也不太强,这时就需要我们自定义实现一个ProgressDialog. 通过看源码我们发现,ProgressDialog继承自Alertdialog,有一个ProgressBar和两个TextView组成的,通过对ProgressDialog的源码进行改进就可以实现一个自定义的ProgressDialog. 1.效果: 首先

  • Android实现底部对话框BottomDialog弹出实例代码

    最近项目上需要实现一个底部对话框,要实现这样的功能其实很简单,先看代码: private void show1() { Dialog bottomDialog = new Dialog(this, R.style.BottomDialog); View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal, null); bottomDialog.setContentView(contentV

  • Android使用setCustomTitle()方法自定义对话框标题

    Android有自带的对话框标题,但是不太美观,如果要给弹出的对话框设置一个自定义的标题,使用AlertDialog.Builder的setCustomTitle()方法. 运行效果如下,左边是点击第一个按钮,弹出Android系统自带的对话框(直接用setTitle()设置标题):右边是点击第二个按钮,首先inflate一个View,然后用setCustomTitle()方法把该View设置成对话框的标题. 定义一个对话框标题的title.xml文件: <?xml version="1.

  • Android实现点击AlertDialog上按钮时不关闭对话框的方法

    本文实例讲述了Android实现点击AlertDialog上按钮时不关闭对话框的方法.分享给大家供大家参考.具体如下: 开发过程中,有时候会有这样的需求: 点击某个按钮之后显示一个对话框,对话框上面有一个输入框,并且有"确认"和"取消"两个按钮.当用户点击确认按钮时,需要对输入框的内容进行判断.如果内容为空则不关闭对话框,并toast提示. 使用AlertDialog.Builder创建对话框时,可以使用builder.setNegativeButton和build

随机推荐