本文实例讲述了AndroID编程自定义Dialog的方法。分享给大家供大家参考,具体如下:
功能:
androID 提供给我们的只有2种Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特点:1. 不是 Activity 2. 开销比 Activity 小得多
鉴于以上的优点 我们就有定制自己Dialog 的需求
原理:
1. androID 系统提供了一个class: Dialog. 而且你可以把自己的工作放在"@H_419_20@protected voID onCreate(Bundle savedInstanceState)" 在里面先调用系统的"@H_419_20@super.onCreate(savedInstanceState)" 其就会保证调用这个method.
2. 至于 Dialog 界面的定制 可以写一个xml 文件 然后 在 "@H_419_20@voID onCreate(Bundle)" 通过 "@H_419_20@setContentVIEw()" 来使之生效
3. Dialog 使用问题: 1. d出:@H_419_20@show() 2. 取消:@H_419_20@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编程自定义Dialog的方法分析全部内容,希望文章能够帮你解决Android编程自定义Dialog的方法分析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)