Android仿IOS UIAlertView对话框

 更新时间:2018年06月27日 14:16:27   作者:祝福  
这篇文章主要为大家详细介绍了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对话框

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

相关文章

  • android imageview图片居中技巧应用

    android imageview图片居中技巧应用

    做UI布局,尤其是遇到比较复杂的多重LinearLayout嵌套,常常会被一些比较小的问题困扰上半天,可是无论怎样设置layout_gravity属性,都无法达到效果
    2012-11-11
  • 浅谈android @id和@+id的区别

    浅谈android @id和@+id的区别

    这篇文章主要介绍了浅谈android @id和@+id的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Android scrollview实现底部继续拖动查看图文详情

    Android scrollview实现底部继续拖动查看图文详情

    这篇文章主要为大家详细介绍了Android scrollview实现底部继续拖动查看图文详情,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • Kotlin中let()with()run()apply()also()函数的使用方法与区别

    Kotlin中let()with()run()apply()also()函数的使用方法与区别

    在Kotlin中的源码标准库(Standard.kt)中提供了一些Kotlin扩展的内置函数可以优化kotlin的编码,今天为大家聊聊let,with,run,apply,also几个函数的用法与区别
    2018-03-03
  • Android中关于定时任务实现关闭订单问题

    Android中关于定时任务实现关闭订单问题

    在电商、支付等领域,往往会有这样的场景,用户下单后放弃支付了,那这笔订单会在指定的时间段后进行关闭操作,细心的你一定发现了像某宝、某东都有这样的逻辑,而且时间很准确,误差在1s内;那他们是怎么实现的呢?今天通过本文学习定时任务实现关闭订单问题
    2022-05-05
  • Android开发实现ScrollView中嵌套两个ListView的方法

    Android开发实现ScrollView中嵌套两个ListView的方法

    这篇文章主要介绍了Android开发实现ScrollView中嵌套两个ListView的方法,结合实例形式分析了Android ScrollView中嵌套两个ListView的操作技巧与相关注意事项,需要的朋友可以参考下
    2017-11-11
  • Android 5.0以上Toast不显示的解决方法

    Android 5.0以上Toast不显示的解决方法

    最近在开发中我们经常会在适配5.0以后的机型遇到各种各样的问题,其中有一个不大不小的问题就是:Toast不显示问题,这篇文章就给大家总结了Android 5.0以上Toast不显示的原因与解决方法,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-11-11
  • Android使用WebView.loadUri()打开网页的方法

    Android使用WebView.loadUri()打开网页的方法

    这篇文章主要介绍了Android使用WebView.loadUri()打开网页的方法,结合实例形式分析了Android中WebView控件的loadUri()打开网页的使用技巧,需要的朋友可以参考下
    2016-01-01
  • 解析Android游戏中获取电话状态进行游戏暂停或继续的解决方法

    解析Android游戏中获取电话状态进行游戏暂停或继续的解决方法

    本篇文章是对在Android游戏中获取电话状态进行游戏暂停或继续的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Android实现网络图片浏览功能

    Android实现网络图片浏览功能

    这篇文章主要为大家详细介绍了Android实现网络图片浏览功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论