本文实例讲述了Android编程自定义Dialog的方法。分享给大家供大家参考,具体如下: 功能: android 提供给我们的只有2种Dialog 即 AlertDialog
本文实例讲述了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;
@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中用Builder模式自定义Dialog的方法Android自定义Dialog实现文字动态加载效果Android 自定义Dialog 实例Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)Android中制作自定义dialog对话框的实例分享Android自定义dialog简单实现方法Android编程经典代码集锦(复制,粘贴,浏览器调用,Toast显示,自定义Dialog等)Android编程中自定义dialog用法实例Android 去掉自定义dialog的白色边框的简单方法Android 自定义dialog的实现代码
--结束END--
本文标题: Android编程自定义Dialog的方法分析
本文链接: https://www.lsjlt.com/news/28296.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0