UI效果(3): 自定义Dialog
来源:互联网 发布:决战武林进阶数据汇总 编辑:程序博客网 时间:2024/05/20 10:56
在这篇博客中,你可以了解到下面几项内容:
<1> LayoutInflater 的使用
<2> Dialog、AlertDialog与自定义布局
<3> Button 的onClick属性
运行,界面就是两个BUtton
自定义Dialog
自定义AlertDialog
完整源码下载地址:http://download.csdn.net/source/3512363
1. Activity代码
package mark.zhang;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;public class CustomDialogActivity extends Activity {Builder customAlertDialog = null;Dialog customDialog = null;LayoutInflater inflater = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); } /** * 为customDialog这个Button设置监听器 * * 注意:在xml文件中设置onClick属性要与改名称一致,否则报错 * * @param v * 该参数是必需的 */ public void customDialog(View v) { customDialog = new Dialog(this); customDialog.setContentView(R.layout.about); customDialog.setTitle("自定义Dialog"); //该句代码如果设置为false的话,点击"返回"按钮不可以退出该dialog //customDialog.setCancelable(false); //View view = inflater.inflate(R.layout.about, (ViewGroup)findViewById(R.id.AboutLinearView)); // 自定义布局中的Button final Button btn_ok = (Button) customDialog.findViewById(R.id.about_button); btn_ok.setOnClickListener(new OnClickListener() {// 该事件被触发@Overridepublic void onClick(View v) {Log.d("mark", "myself dialog!");}}); // 显示对话框 customDialog.show(); } /** * 为customAlertDialog这个Button设置监听器 * * 注意:在xml文件中设置onClick属性要与改名称一致,否则报错 * * @param v * 该参数是必需的 */ public void customAlertDialog(View v) { customAlertDialog = new AlertDialog.Builder(this); // /layout/about.xml自定义布局文件 View view = inflater.inflate(R.layout.about, (ViewGroup)findViewById(R.id.AboutLinearView)); customAlertDialog.setTitle("自定义AlertDialog").setView(view); customAlertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}}); // 自定义布局中的Button final Button btn_ok = (Button) view.findViewById(R.id.about_button); btn_ok.setOnClickListener(new OnClickListener() {// 该事件可以被触发@Overridepublic void onClick(View v) {Log.d("mark", "myself alertDialog!");// 其它操作,比如结束该Activity或者启动服务、另一个Activity等}}); // 显示对话框 customAlertDialog.show(); }}2. main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:text="customDialog" android:onClick="customDialog" /><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="customAlertDialog" android:onClick="customAlertDialog" /></LinearLayout>这里需要注意,两个Button都设置了onClick属性,看看Activity代码中有两个对应的方法。
3. 自定义布局文件about.xml
<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:id="@+id/AboutScrollView"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/AboutLinearView"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical">><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/AboutTextView"android:textColor="?android:attr/textColorPrimaryDisableOnly"android:textAppearance="?android:attr/textAppearanceSmall"android:autoLink="all" android:padding="11dp" android:text="@string/about_text" /><Button android:id="@+id/about_button" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:text="ok,I know!" /></LinearLayout></ScrollView>
- UI效果(3): 自定义Dialog
- Android自定义Dialog效果
- Android UI设计:自定义Dialog
- Android UI设计之<七>自定义Dialog,实现各种风格效果的对话框
- 自定义Dialog实现各种效果
- Dialog 自定义一些常见效果
- 自定义dialog弹框效果
- 经典效果组件篇3——自定义Dialog
- 深入浅出jQuery (五) 如何自定义UI-Dialog?
- Android之UI--打造万能自定义Dialog
- element UI 模态层dialog自定义大小
- Android自定义类似ProgressDialog效果的Dialog
- Android自定义类似ProgressDialog效果的Dialog
- Android自定义类似ProgressDialog效果的Dialog
- Android自定义类似ProgressDialog效果的Dialog
- Android自定义类似ProgressDialog效果的Dialog
- Android自定义类似ProgressDialog效果的Dialog
- Android自定义类似ProgressDialog效果的Dialog
- fedora 10启动nfs
- 在win7系统下eclipse上面的文字太小
- CSS网页布局中DIV和TABLE超出宽度自动换行分析
- Grid中下拉框解决办法(X5中)
- 中文显示为方块解决方法
- UI效果(3): 自定义Dialog
- 数字图像处理中常用到的一些英文词汇及其解释
- struts2一个学习地址
- 小谈网络游戏同步
- session_destroy
- H3C路由器做vpn配置详解
- C-to-hardware compiler (HLL synthesis)
- Hibernate 配置多个数据库 多个SessionFactory
- strtok函数用法