Android仿IOS UIAlertView对话框
作者:祝福 发布时间:2023-09-28 09:00:12
标签: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对话框
来源:https://blog.csdn.net/zhufuing/article/details/18735371


猜你喜欢
- 今天在使用Nlog的时候,发现了一个之前没注意的问题。以前,我的app配置文件都是这么写的,当然配置比较多的时候会改用xml。 如
- using System;using System.Collections.Generic;using System.IO;using Sy
- 本文实例讲述了C#检测上传文件真正类型的方法。分享给大家供大家参考。具体分析如下:对于用户上传的文件如果只是根据扩展名判断,很容易上传上来可
- 开篇Mybatis有个实用的功能就是逆向工程,能根据表结构反向生成实体类,这样能避免手工生成出错。市面上的教程大多都很老了,大部分都是针对m
- 首先:我们要建一个web项目接着: 我们先来导入struts的xml文件第一步:右击你的项目名,鼠标到MyEclipse会看到一个add s
- Android 使用AsyncTask设置请求超时的注意事项final AsyncTaskTools task = new AsyncTas
- 1. 用indexof的方法:public class Test11 {private static int counter = 0;/**
- 公司app要求做一个扭蛋功能,其实就是一个可拖动层叠卡片列表,原理还是由一个自定义Recyclerview和LayoutManager来实现
- 下载地址在这里:http://dotnetzip.codeplex.com/下载到的包里有很多个dll文件,一般引用Ionic.Zip.dl
- 前言日常编码过程中,最重要的技能不是说你学会使用很多最新的编程技术或者做出一个高大上的系统。而是你在写代码过程中,对异常的处理,是否系统可以
- JNA(Java Native Access):建立在JNI之上的Java开源框架,SUN主导开发,用来调用C、C++代码,尤其是底层库文件
- 有时您可能想限制可以在参数化类型中用作类型参数的类型。 例如,对数字进行操作的方法可能只希望接受Number或其子类的实例。 这就是有界类型
- 本文实例讲述了C#动态执行批处理命令的方法。分享给大家供大家参考。具体方法如下:C# 动态执行一系列控制台命令,并允许实时显示出来执行结果时
- 本文实例讲述了C#迷你猜数。分享给大家供大家参考。具体如下:using System; using System.Collections.G
- 本篇开始介绍Jetpack Compose 中的修饰符Modifier。修饰符可以用来执行以下操作:更改可组合项的大小、布局、行为和外观。添
- Android实现界面内嵌多种卡片视图,具体内容如下效果如图所示:1.选择某个界面时,对应的第几个小圆点亮:通过selector制造圆点和进
- 一、HTTP http请求 一般一个http请求包括以下三个部分: 1 请求方法,如get,pos
- 由于工作的需要,最近要对Android客户端软件进行测试,便学习了一下Android客户端测试的方法,现在与大家分享一下。1.在Androi
- 【SpringBoot】通过Feign调用传递Header中参数如何通过Feign传递Header参数问题描述我们在SpringCloud中
- 1:什么是Socket所谓套接字(Socket),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。一个套接字就是网络上进程通信