世上哪有树缠藤简谱,热血无赖买房,重生豪门淑媛
本文实例讲述了android编程自定义dialog的方法。分享给大家供大家参考,具体如下:
功能:
android 提供给我们的只有2种dialog 即 alertdialog & progressdialog 但是 dialog 有其自身的特点:1. 不是 activity 2. 开销比 activity 小得多
鉴于以上的优点 我们就有定制自己dialog 的需求
原理:
1. android 系统提供了一个class: dialog. 而且你可以把自己的工作放在"protected void oncreate(bundle savedinstancestate)
" 在里面先调用系统的"super.oncreate(savedinstancestate)
" 其就会保证调用这个method.
2. 至于 dialog 界面的定制 可以写一个xml 文件 然后 在 "void oncreate(bundle)
" 通过 "setcontentview()
" 来使之生效
3. dialog 使用问题: 1. 弹出:show()
2. 取消:dismiss()
代码:
1. 创建一个 dialog:
public class customdialog extends dialog { public customdialog(context context) { super(context); // todo auto-generated constructor stub } protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.custom_dialog); settitle("custom dialog"); textview text = (textview)findviewbyid(r.id.text); text.settext("hello, this is a custom dialog!"); imageview image = (imageview)findviewbyid(r.id.image); image.setimageresource(r.drawable.sepurple); button buttonyes = (button) findviewbyid(r.id.button_yes); buttonyes.setheight(5); buttonyes.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub dismiss(); } }); button buttonno = (button) findviewbyid(r.id.button_no); buttonno.setsingleline(true); buttonno.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub dismiss(); } }); } //called when this dialog is dismissed protected void onstop() { log.d("tag","+++++++++++++++++++++++++++"); } }
2. dialog 的使用:
public class customdialogusage extends activity { customdialog cd; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); cd = new customdialog(this); button buttonyes = (button) findviewbyid(r.id.main_button); buttonyes.setonclicklistener(new onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub cd.show(); } }); } }
3. dialog 的界面定制:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <imageview android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginright="10dp" /> <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px"> <textview android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textcolor="#fff" /> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px"> <button android:id="@+id/button_yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" yes " android:gravity="center" /> <button android:id="@+id/button_no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" no " android:gravity="center" /> </linearlayout> </linearlayout> </linearlayout>
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio 解决logcat无过滤工具栏的操作
Android Studio 恢复小窗口停靠模式(Docked Mode)
Android studio保存logcat日志到本地的操作
Android Studio快捷键生成TAG、Log.x日志输出介绍
网友评论