谁说Android没有好的微信对话框添加好友

1.写在前面
Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮)、列表、单选、多选、等待、进度条、编辑、自定义等多种形式,将在第2部分介绍。
有时,我们希望在对话框创建或关闭时完成一些特定的功能,这需要复写Dialog的create()、show()、dismiss()等方法,将在第3部分介绍。
2.代码示例
2.1 普通Dialog(图1与图2)
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonNormal = (Button) findViewById(R.id.button_normal);
buttonNormal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showNormalDialog();
private void showNormalDialog(){
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
* setXXX方法返回Dialog对象,因此可以链式设置属性
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle(&我是一个普通Dialog&)
normalDialog.setMessage(&你要点击哪一个按钮呢?&);
normalDialog.setPositiveButton(&确定&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//...To-do
normalDialog.setNegativeButton(&关闭&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//...To-do
normalDialog.show();
/* @setNeutralButton 设置中间的按钮
* 若只需一个按钮,仅设置 setPositiveButton 即可
private void showMultiBtnDialog(){
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle(&我是一个普通Dialog&).setMessage(&你要点击哪一个按钮呢?&);
normalDialog.setPositiveButton(&按钮1&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...To-do
normalDialog.setNeutralButton(&按钮2&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...To-do
normalDialog.setNegativeButton(&按钮3&, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...To-do
// 创建实例并显示
normalDialog.show();
2.2 列表Dialog(图3)
private void showListDialog() {
final String[] items = { &我是1&,&我是2&,&我是3&,&我是4& };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this);
listDialog.setTitle(&我是一个列表Dialog&);
listDialog.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// which 下标从0开始
// ...To-do
Toast.makeText(MainActivity.this,
&你点击了& + items[which],
Toast.LENGTH_SHORT).show();
listDialog.show();
2.3 单选Dialog(图4)
private void showSingleChoiceDialog(){
final String[] items = { &我是1&,&我是2&,&我是3&,&我是4& };
yourChoice = -1;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle(&我是一个单选Dialog&);
// 第二个参数是默认选项,此处设置为0
singleChoiceDialog.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
yourChoice =
singleChoiceDialog.setPositiveButton(&确定&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != -1) {
Toast.makeText(MainActivity.this,
&你选择了& + items[yourChoice],
Toast.LENGTH_SHORT).show();
singleChoiceDialog.show();
2.4 多选Dialog(图5)
ArrayList&Integer& yourChoices = new ArrayList&&();
private void showMultiChoiceDialog() {
final String[] items = { &我是1&,&我是2&,&我是3&,&我是4& };
// 设置默认选中的选项,全为false默认均未选中
final boolean initChoiceSets[]={false,false,false,false};
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
multiChoiceDialog.setTitle(&我是一个多选Dialog&);
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
yourChoices.add(which);
yourChoices.remove(which);
multiChoiceDialog.setPositiveButton(&确定&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
int size = yourChoices.size();
String str = &&;
for (int i = 0; i & i++) {
str += items[yourChoices.get(i)] + & &;
Toast.makeText(MainActivity.this,
&你选中了& + str,
Toast.LENGTH_SHORT).show();
multiChoiceDialog.show();
2.5 等待Dialog(图6)
private void showWaitingDialog() {
/* 等待Dialog具有屏蔽其他控件的交互能力
* @setCancelable 为使屏幕不可点击,设置为不可取消(false)
* 下载等事件完成后,主动调用函数关闭该Dialog
ProgressDialog waitingDialog=
new ProgressDialog(MainActivity.this);
waitingDialog.setTitle(&我是一个等待Dialog&);
waitingDialog.setMessage(&等待中...&);
waitingDialog.setIndeterminate(true);
waitingDialog.setCancelable(false);
waitingDialog.show();
2.6 进度条Dialog(图7)
private void showProgressDialog() {
/* @setProgress 设置初始进度
* @setProgressStyle 设置样式(水平进度条)
* @setMax 设置进度最大值
final int MAX_PROGRESS = 100;
final ProgressDialog progressDialog =
new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);
progressDialog.setTitle(&我是一个进度条Dialog&);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/* 模拟进度增加的过程
* 新开一个线程,每个100ms,进度增加1
new Thread(new Runnable() {
public void run() {
int progress= 0;
while (progress & MAX_PROGRESS){
Thread.sleep(100);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e){
e.printStackTrace();
// 进度达到最大值后,窗口消失
progressDialog.cancel();
}).start();
2.7 编辑Dialog(图8)
private void showInputDialog() {
/*@setView 装入一个EditView
final EditText editText = new EditText(MainActivity.this);
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(MainActivity.this);
inputDialog.setTitle(&我是一个输入Dialog&).setView(editText);
inputDialog.setPositiveButton(&确定&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}).show();
2.8 自定义Dialog(图9)
&!-- res/layout/dialog_customize.xml--&
&!-- 自定义View --&
&LinearLayout xmlns:android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&
android:id=&@+id/edit_text&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
&/LinearLayout&
private void showCustomizeDialog() {
/* @setView 装入自定义View ==& R.layout.dialog_customize
* 由于dialog_customize.xml只放置了一个EditView,因此和图8一样
* dialog_customize.xml可自定义更复杂的View
AlertDialog.Builder customizeDialog =
new AlertDialog.Builder(MainActivity.this);
final View dialogView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_customize,null);
customizeDialog.setTitle(&我是一个自定义Dialog&);
customizeDialog.setView(dialogView);
customizeDialog.setPositiveButton(&确定&,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 获取EditView中的输入内容
EditText edit_text =
(EditText) dialogView.findViewById(R.id.edit_text);
Toast.makeText(MainActivity.this,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show();
customizeDialog.show();
3.复写回调函数
/* 复写Builder的create和show函数,可以在Dialog显示前实现必要设置
* 例如初始化列表、默认选项等
* @create 第一次创建时调用
* @show 每次显示时调用
private void showListDialog() {
final String[] items = { &我是1&,&我是2&,&我是3&,&我是4& };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this){
public AlertDialog create() {
items[0] = &我是No.1&;
return super.create();
public AlertDialog show() {
items[1] = &我是No.2&;
return super.show();
listDialog.setTitle(&我是一个列表Dialog&);
listDialog.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...To-do
/* @setOnDismissListener Dialog销毁时调用
* @setOnCancelListener Dialog关闭时调用
listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
Toast.makeText(getApplicationContext(),
&Dialog被销毁了&,
Toast.LENGTH_SHORT).show();
listDialog.show();
阅读(...) 评论()&>&&>&移动开发&>&Android&>&android自定义实现好看的对话框
android自定义实现好看的对话框
上传大小:979KB
我们开发android,常常会使用到弹出式对话框。为了不重复造轮子,还有能弹出好看的对话框,能适应整体的app风格,使用系统自带的显示不行,我们需要自定义,自己的Dialog,把它封装在一个类里面,要用的时候,直接调用就是了。
该例子就是这样一个Dialog,使用很简单,传入相应参数就行,参数,自己设定,根据参数的不同,执行不同的操作(自己设定)。
综合评分:3.8(97位用户评分)
所需积分:
下载次数:385
审核通过送C币
微信公众号和支付方面
创建者:jiangsucsdn
Android 之多种网络请求实现方式
创建者:dickyqie
android开发
创建者:qq_
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var q = $("#form1").serializeArray();
console.log(q);
var res_area_r = $.trim($(".res_area_r").val());
if (res_area_r == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
var mess = $(".res_area_r").val();
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, _username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click",'.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
$(".res_area_r").val($.trim($(".res_area").val()));
评论共有56条
什么狗屁东西
不错的对话框,非常漂亮
完全被坑,说实话还不如系统直接给的好看
亏死我了,这要3分。
不是很好看,而且用了图片资源,要改一个对话框太麻烦。
3分,感觉不太值啊、、、
做个代码参考使用
布局不错, 可以使用
还行,可以做个参考
确实很好看的界面
上传者其他资源上传者专辑
zip压缩lib包
android edittext 输入表情textview显示表情
android中activity和service的交互
android聊天机器人
android百度地图
移动开发热门标签
VIP会员动态
前端开发重难点
17年软考最新真题及解析
物联网全栈开发专题
二十大技术领域优质资源
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
CSDN&vip年卡&4000万程序员的必选
android自定义实现好看的对话框
会员到期时间:剩余下载次数:
积分不足!
资源所需积分
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分
当前拥有积分
VIP年卡全年1200次免积分下载
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:Android自定义对话框的大小 - 点点滴滴,持之以恒 - ITeye博客
博客分类:
在Android做界面时要弹出对话框让用户输入内容,经常遇到开始的时候没有内容对话框一点点,看起来很别扭,查了下资料,修改对话框的WindowManager.LayoutParams可以达到修改对话框大小的目的。
从Dialog继承一个自定义对话框类,在其构造函数中加上如下代码:
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); //为获取屏幕宽、高
LayoutParams p = getWindow().getAttributes();
//获取对话框当前的参数值
p.height = (int) (d.getHeight() * 0.6);
//高度设置为屏幕的0.6
p.width = (int) (d.getWidth() * 0.95);
//宽度设置为屏幕的0.95
getWindow().setAttributes(p);
//设置生效
浏览 10220
在Activity中声明的内部类。getWindowManager是activity的此处举例用的是内部类,是可以访问getWindowManager()的,且是在setContentView之后。
浏览: 408605 次
来自: 成都
还有一个GZIP的问题,我怎么转都乱码最后是因为要解压一下ht ...
zyp09 写道很想知道在Mainactivity界面怎么获得 ...
楼主很用心,一定要顶
求代码,大神,}

我要回帖

更多关于 炉石传说对话框 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信