android开发 怎样出现二级对话框,Android开发之对话框案例详解(五种对话框)

下面通过实例代码给大家分享5种android对话框,具体内容详情如下所示:

1 弹出普通对话框 --- 系统更新

2 自定义对话框-- 用户登录

3 时间选择对话框 -- 时间对话框

4 进度条对话框 -- 信息加载..

5 popuwindow对话框

1 弹出普通对话框 --- 系统更新

//弹出普通对话框

public void shownormaldialog(view v) {

alertdialog.builder builder = new builder(this);

//设置dialog的图标

builder.seticon(r.drawable.ic_launcher);

//设置对话框的标题

builder.settitle("更新");

//设置message

builder.setmessage("发现新版本是否更新?");

//确定按钮 取消按钮

builder.setpositivebutton("确定",new onclicklistener() {

/**

* 点击确定按钮 回调该方法

*/

@override

public void onclick(dialoginterface dialog, int which) {

//到服务器去下载新的版本 duration单词意思:时长

toast.maketext(mainactivity.this, "开始下载新版本", toast.length_short).show();

}

});

builder.setnegativebutton("取消", new onclicklistener() {

/**

* 点击取消按钮 回调该方法

*/

@override

public void onclick(dialoginterface dialog, int which) {

//到服务器去下载新的版本 duration单词意思:时长

toast.maketext(mainactivity.this, "不需要更新", toast.length_short).show();

}

});

builder.setneutralbutton("下一次", new onclicklistener() {

@override

public void onclick(dialoginterface dialog, int which) {

//到服务器去下载新的版本 duration单词意思:时长

toast.maketext(mainactivity.this, "下一次吧", toast.length_short).show();

}

});

//通过建造这老构建一个对话框

dialog dialog = builder.create();

//显示

dialog.show();

}

2 自定义对话框-- 用户登录

布局文件:

user_name_dialog.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="10dip"

android:orientation="vertical" >

android:id="@+id/tv_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录信息"

android:gravity="center"

android:textappearance="?android:attr/textappearancelarge" />

android:id="@+id/tv_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="用户名:" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入用户名"/>

android:id="@+id/tv_pwd"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密 码:" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputtype="textpassword"

android:hint="请输入密码"/>

android:id="@+id/btn_confirm"

android:layout_width="150dip"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="登录" />

android:id="@+id/btn_cancel"

android:layout_width="150dip"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="取消" />

java代码:

//自定义对话框

dialog cus_dialog ;

public void showcustomdialog(view v){

alertdialog.builder builder = new builder(this);

// 布局填充器

layoutinflater inflater = layoutinflater.from(this);

view view = inflater.inflate(r.layout.user_name_dialog, null);

// 设置自定义的对话框界面

builder.setview(view);

// 获取用户名密码

final edittext name = (edittext) view.findviewbyid(r.id.et_name);

final edittext pwd = (edittext) view.findviewbyid(r.id.et_pwd);

button btn_confirm = (button) view.findviewbyid(r.id.btn_confirm);

btn_confirm.setonclicklistener(new view.onclicklistener() {

@override

public void onclick(view v) {

// todo 自动生成的方法存根

if(name.gettext().tostring().trim().equals("abc")){

showtoastmsg("用户名正确");

// 对话框消失

cus_dialog.dismiss();

}

else{

showtoastmsg("用户名错误");

}

}

});

button btncancel = (button) view.findviewbyid(r.id.btn_cancel);

btncancel.setonclicklistener(new view.onclicklistener() {

@override

public void onclick(view v) {

// 对话框消失

cus_dialog.dismiss();

}

});

cus_dialog = builder.create();

cus_dialog.show();

}

下载地址:http://www.jinhusns.com/products/download/?type=yhq

3 时间选择对话框 -- 时间对话框

// 时间选择对话框

public void showtimepickerdialog(view v){

calendar sysdate = calendar.getinstance();

//设置系统时间

sysdate.settimeinmillis(system.currenttimemillis());

int hour = sysdate.get(calendar.hour_of_day);

int minute = sysdate.get(calendar.minute);

timepickerdialog time = new timepickerdialog(this,

new ontimesetlistener() {

@override

public void ontimeset(timepicker view, int hourofday, int minute) {

// todo 自动生成的方法存根

showtoastmsg(" hourofday:" + hourofday + " minute:" + minute);

}

}, //callback 选择时间后的回调方法

hour,//hourofday 当前系统时间

minute,//hourofday 当前系统时间

true);//是否24小时制

time.show();

}

4 进度条对话框 -- 信息加载..

/**

* 进度条对话框

* @param v

*/

public void showprogressdialog(view v){

final progressdialog progress = new progressdialog(this);

progress.setprogress(r.drawable.img2);

progress.settitle("标题");

progress.setmessage("加载中...");

//样式1 进度条样式

progress.setprogressstyle(progressdialog.style_horizontal);

//样式2 有加载图标

//progress.setprogressstyle(progressdialog.style_spinner);

//最大

progress.setmax(100);

//当前

progress.setprogress(50);

progress.setbutton("确定", new onclicklistener() {

@override

public void onclick(dialoginterface dialog, int which) {

// todo 自动生成的方法存根

showtoastmsg("确定按钮");

}

});

progress.setbutton2("取消", new onclicklistener() {

@override

public void onclick(dialoginterface dialog, int which) {

// todo 自动生成的方法存根

showtoastmsg("取消按钮");

}

});

progress.show();

new thread(new runnable() {

@override

public void run() {

// todo auto-generated method stub

int i = 0;

while (i < 100) {

try {

thread.sleep(200);

// 更新进度条的进度,可以在子线程中更新进度条进度

progress.incrementprogressby(5);

// progress.incrementsecondaryprogressby(10);//二级进度条更新方式

i += 5;

} catch (exception e) {

// todo: handle exception

}

}

// 在进度条走完时删除dialog

progress.dismiss();

}

}).start();

}

5 popuwindow对话框

button btn_popu;

//popuwindow对话框

public void showpopuwindow(view v){

btn_popu = (button) v;

// 设置布局

view view = layoutinflater.from(this).inflate(r.layout.pop_window, null);

popupwindow window = new popupwindow(this);

window.setcontentview(view);

window.setwidth(360);

window.setheight(200);

int[] location = new int[2];

// 获取按钮坐标

btn_popu.getlocationinwindow(location);

window.setfocusable(true);

window.setbackgrounddrawable(getresources().getdrawable(r.drawable.back_null));

window.showatlocation(btn_popu, gravity.left |gravity.top , location[0]+ btn_popu.getwidth(), location[1] + 0 );

//showtoastmsg("" + (location[0]+ btn_popu.getwidth())+" "+ (location[1] + btn_popu.getheight() / 2));

imageview img_start = (imageview) view.findviewbyid(r.id.img_start);

img_start.setonclicklistener(new view.onclicklistener() {

@override

public void onclick(view v) {

// todo 自动生成的方法存根

showtoastmsg("点击了启动");

}

});

}

总结

以上所述是小编给大家介绍的详解android开发之对话框案例详解(五种对话框),希望对大家有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值