android自定义Dialog仿QQ手机浏览器.docx
- 文档编号:8111665
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:13
- 大小:88.75KB
android自定义Dialog仿QQ手机浏览器.docx
《android自定义Dialog仿QQ手机浏览器.docx》由会员分享,可在线阅读,更多相关《android自定义Dialog仿QQ手机浏览器.docx(13页珍藏版)》请在冰豆网上搜索。
android自定义Dialog仿QQ手机浏览器
欢迎a88541521我的:
收件箱资源博客空间设置|帮助|退出
∙CSDN首页
∙资讯
∙论坛
∙博客
∙下载
∙搜索
更多
hungxn的专栏
∙
目录视图
∙
摘要视图
∙
订阅
用开源IaaS构建自己的云——OpenStack征稿启事不用买彩票,就有408万!
CSDN博客频道“移动开发之我见”主题征文活动2012CSDN网站八大职位急聘
android自定义Dialog,仿QQ手机浏览器
分类:
Android学习2011-12-3118:
52126人阅读评论(0)收藏举报
以前一直不大喜欢android系统的对话框,这两天上班比较空闲,就试着模仿一下QQ浏览器的对话框。
本来以前想写个类继承Dialog,但受水平限制,没有实现。
主要代码如下:
[java]viewplaincopy
1publicclassCustomAlertDialog
2{
3privateAlertDialogdialog;
4privateLayoutInflaterinflater;
5privateButtonleftBtn,midBtn,rightBtn;
6privateLinearLayouttitleLay,bottomLay;
7privateScrollViewcontentLay;
8privateTextViewtitle,contentMsg;
9privateContextcontext;
10privateViewdialogView;
11
12publicCustomAlertDialog(Contextcontext)
13{
14this.context=context;
15inflater=LayoutInflater.from(context);
16dialogView=inflater.inflate(R.layout.dialog,null);
17
18bottomLay=(LinearLayout)dialogView.findViewById(R.id.dialog_btns);
19contentLay=(ScrollView)dialogView.findViewById(R.id.dialog_content_lay);
20dialog=newAlertDialog.Builder(context).create();
21}
22
23
24publicvoidshow(){
25dialog.show();
26dialog.getWindow().setContentView(dialogView);
27}
28
29publicvoiddismiss(){
30if(dialog!
=null&&dialog.isShowing()){
31dialog.dismiss();
32}
33}
34
35publicbooleanisShowing(){
36if(dialog!
=null)
37returndialog.isShowing();
38returnfalse;
39}
40
41publicvoidsetCancelable(booleanbool){
42dialog.setCancelable(bool);
43}
44
45publicvoidsetContentView(Viewview){
46contentLay.removeAllViews();
47contentLay.addView(view);
48}
49
50publicvoidsetMessage(Stringmsg){
51contentMsg=(TextView)dialogView.findViewById(R.id.dialog_content);
52contentMsg.setText(msg);
53contentMsg.setVisibility(View.VISIBLE);
54}
55
56publicvoidsetTitle(Stringtitle){
57titleLay=(LinearLayout)dialogView.findViewById(R.id.dialog_title_lay);
58this.title=(TextView)dialogView.findViewById(R.id.dialog_title);
59titleLay.setVisibility(View.VISIBLE);
60this.title.setText(title);
61}
62
63publicvoidsetOnPositiveButton(Stringstr,View.OnClickListenerlistener){
64bottomLay.setVisibility(View.VISIBLE);
65leftBtn=(Button)dialogView.findViewById(R.id.dialog_left_btn);
66leftBtn.setVisibility(View.VISIBLE);
67leftBtn.setText(str);
68leftBtn.setOnClickListener(listener);
69}
70
71publicvoidsetOnNegativeButton(Stringstr,View.OnClickListenerlistener){
72bottomLay.setVisibility(View.VISIBLE);
73rightBtn=(Button)dialogView.findViewById(R.id.dialog_right_btn);
74rightBtn.setVisibility(View.VISIBLE);
75rightBtn.setText(str);
76rightBtn.setOnClickListener(listener);
77}
78
79publicvoidsetOnNeutralButton(Stringstr,View.OnClickListenerlistener){
80bottomLay.setVisibility(View.VISIBLE);
81midBtn=(Button)dialogView.findViewById(R.id.dialog_mid_btn);
82midBtn.setVisibility(View.VISIBLE);
83midBtn.setText(str);
84midBtn.setOnClickListener(listener);
85}
86}
这个对话框的布局由标题部分,内容部分,和底部按钮部分组成,最多3个按钮,需要哪些按钮,只要设置这些按钮的listener就可以显示出来;内容部分,可以设置为自定义的View,也可以是简单的一个TextView(通过setMessage方法)。
布局如下dialog.xml:
[html]viewplaincopy
87
xmlversion="1.0"encoding="utf-8"?
>
88 android=" 89android: layout_width="180dip"android: layout_height="160dip"> 90 id="@+id/dialog_title_lay"android: layout_width="fill_parent"android: layout_height="wrap_content" 91android: background="@drawable/alertdlg_title_bg"android: gravity="center_vertical"android: visibility="gone"> 92 id="@+id/dialog_title"android: layout_width="fill_parent"android: layout_height="wrap_content" 93android: text="标题"android: textSize="18sp"android: textColor="#000000"android: textStyle="bold" 94android: layout_marginLeft="10dip"android: layout_marginRight="10dip"/> 95 96 id="@+id/dialog_content_lay"android: layout_width="fill_parent"android: layout_height="100dip" 97android: background="@drawable/alertdlg_content_bg"android: gravity="center" 98android: layout_below="@id/dialog_title_lay"> 99 id="@+id/dialog_content"android: layout_width="wrap_content"android: layout_height="wrap_content" 100android: text="内容"android: textSize="16sp"android: textColor="#000000"android: visibility="gone" 101android: layout_marginLeft="10dip"android: layout_marginRight="10dip"android: layout_marginTop="10dip"/> 102 103 id="@+id/dialog_btns"android: layout_width="fill_parent"android: layout_height="wrap_content" 104android: background="@drawable/alertdlg_bottom_bg"android: layout_below="@id/dialog_content_lay" 105android: gravity="center"android: visibility="gone"> 106 id="@+id/dialog_left_btn"android: layout_width="wrap_content"android: layout_height="wrap_content" 107android: text="左按钮"android: background="@drawable/button_blue_normal"android: layout_marginRight="10dip" 108android: layout_marginLeft="10dip"android: layout_weight="1"android: visibility="gone"/> 109 id="@+id/dialog_mid_btn"android: layout_width="wrap_content"android: layout_height="wrap_content" 110android: text="中按钮"android: background="@drawable/button_blue_normal"android: layout_marginRight="10dip" 111android: layout_marginLeft="10dip"android: layout_weight="1"android: visibility="gone"/> 112 id="@+id/dialog_right_btn"android: layout_width="wrap_content"android: layout_height="wrap_content" 113android: text="右按钮"android: background="@drawable/button_blue_pressed"android: layout_marginRight="10dip" 114android: layout_marginLeft="10dip"android: layout_weight="1"android: visibility="gone"/> 115 116 117 测试界面,主要就2个按钮,主要看看不同的2个dialog,第一个显示3个按钮,中间内容是一个TextView;第二个显示一个按钮,中间是自定义的View: [java]viewplaincopy 118publicclassTestCustomDialogActivityextendsActivity{ 119Buttonbutton1,button2; 120LayoutInflaterinflater; 121CustomAlertDialogcustomAd; 122/**Calledwhentheactivityisfirstcreated.*/ 123@Override 124publicvoidonCreate(BundlesavedInstanceState){ 125super.onCreate(savedInstanceState); 126setContentView(R.layout.main); 127button1=(Button)findViewById(R.id.button1); 128button2=(Button)findViewById(R.id.button2); 129inflater=LayoutInflater.from(this); 130button1.setOnClickListener(newOnClickListener() 131{ 132 133@Override 134publicvoidonClick(Viewv){ 135customAd=newCustomAlertDialog(TestCustomDialogActivity.this); 136customAd.setTitle("密码被盗"); 137customAd.setMessage("互联网用户数据泄密事件继续发酵。 继CSDN、天涯、新浪等互联网公司后,"+ 138"京东商城、网易公司、支付宝也被卷入“泄密门”。 昨日又有网友爆料称,交通银行和民生银行用户资料也遭外泄。 "+ 139"昨日中午,有爆料称交通银行7000万客户、民生银行3500万用户以及工行用户资料外泄,"+ 140"泄露数据包括用户的姓名、卡号、密码等敏感信息,并发布信息截图。 "+ 141"对此,三家银行昨天均发布声明否认有用户资料外泄。 业内专家也指出,银行卡交易系统不可能存在明文密码。 "); 142customAd.setOnPositiveButton("确定",newOnClickListener() 143{ 144@Override 145publicvoidonClick(Viewv) 146{ 147Toast.makeText(TestCustomDialogActivity.this,"左按钮",Toast.LENGTH_SHORT).show(); 148customAd.dismiss(); 149} 150}); 151 152customAd.setOnNeutralButton("其他",newOnClickListener() 153{ 154@Override 155publicvoidonClick(Viewv) 156{ 157Toast.makeText(TestCustomDialogActivity.this,"中按钮",Toast.LENGTH_SHORT).show(); 158customAd.dismiss(); 159} 160}); 161 162customAd.setOnNegativeButton("取消",newOnClickListener() 163{ 164 165@Override 166publicvoidonClick(Viewv) 167{ 168Toast.makeText(TestCustomDialogActivity.this,"右按钮",Toast.LENGTH_SHORT).show(); 169customAd.dismiss(); 170} 171}); 172customAd.setCancelable(false); 173customAd.show(); 174 175} 176}); 177 178button2.setOnClickListener(newOnClickListener() 179{ 180 181@Override 182publicvoidonClick(Viewv) 183{ 184customAd=newCustomAlertDialog(TestCustomDialogActivity.this); 185Viewview=inflater.inflate(R.layout.dialog_content,null); 186customAd.setContentView(view); 187customAd.setTitle("退出"); 188customAd.setOnPositiveButton("确定",newOnClickListener() 189{ 190@Override 191publicvoidonClick(Viewv) 192{ 193Toast.makeText(TestCustomDialogActivity.this,"确定退出",Toast.LENGTH_SHORT).show(); 194customAd.dismiss(); 195} 196}); 197customAd.setCancelable(true); 198customAd.show(); 199} 200}); 201} 202 203} 分享到: ∙上一篇: 【转载】布局技巧: 使用ViewStub ∙下一篇: 移动联通电信wap和net接入点判断 查看评论 暂无评论 窗体顶端 发表评论 ∙用户名: ∙a8854152 ∙评论内容: ∙ 窗体底端 *以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场 个人资料 hungxn ∙访问: 1795次 ∙积分: 44分 ∙排名: 千里之外 ∙原创: 1篇 ∙转载: 6篇 ∙译文: 0篇 ∙评论: 0条 文章搜索 窗体顶端 窗体底端 文章分类 ∙Android笔记(6) ∙Android学习 (1) 文章存档 ∙2012年01月 (1) ∙2011年12月 (1) ∙2011年02月(4) ∙2011年01月 (1) 阅读排行 ∙【转载】Gallery和ImageSwi...(229) ∙移动联通电信wap和net接入点判断(203) ∙【转载】Android之GridView...(162) ∙android自定义Dialog,仿QQ...(126) ∙【转载】监听SMS消息(119) ∙【转载】Invalidate和postI...(50) ∙【转载】布局技巧: 使用ViewStub(48) 评论排行 ∙【转载】监听SMS消息(0) ∙【转载】Invalidate和postI...(0) ∙【转载】Android之GridView...(0) ∙【转载】Gallery和ImageSwi...(0) ∙【转载】布局技巧: 使用ViewStub(0) ∙android自定义Dialog,仿QQ...(0) ∙移动联通电信wap和net接入点判断(0) 推荐文章 公司简介|招贤纳士|广告服务|银行汇款帐号|联系方式|版权声明|法律顾问|问题报告 京ICP证070598号 北京创新乐知信息技术有限公司版权所有 联系邮箱: webmaster@ Copyright©1999-2012,CSDN.NET,AllRightsReserved
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- android 自定义 Dialog QQ 手机 浏览器