Android编程自定义Dialog的方法分析

 更新时间:2017年03月04日 14:36:14   作者:aloxc  
这篇文章主要介绍了Android编程自定义Dialog的方法,结合实例形式分析了Android自定义Dialog的界面布局、功能实现及相关注意事项,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • 玩转Kotlin 彻底弄懂Lambda和高阶函数

    玩转Kotlin 彻底弄懂Lambda和高阶函数

    这篇文章主要帮助大家彻底弄懂Lambda和高阶函数,玩转Kotlin,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android原生视频播放VideoView的使用

    Android原生视频播放VideoView的使用

    这篇文章主要为大家详细介绍了Android原生视频播放VideoView的使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • Android编程中的消息机制实例详解

    Android编程中的消息机制实例详解

    这篇文章主要介绍了Android编程中的消息机制,结合实例形式较为详细的分析了Android中消息机制的原理,注意事项与相关使用技巧,需要的朋友可以参考下
    2016-01-01
  • Android  LayoutInflater加载布局详解及实例代码

    Android LayoutInflater加载布局详解及实例代码

    这篇文章主要介绍了Android LayoutInflater加载布局详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • Android 动态的显示时间

    Android 动态的显示时间

    本文给大家分享一段代码实现android动态显示时间,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2016-12-12
  • Android 使用Vitamio打造自己的万能播放器(1)——准备

    Android 使用Vitamio打造自己的万能播放器(1)——准备

    本文主要介绍Android Vitamio,在Android开发视频播放器的时候,大家经常会遇到系统版本和不同的Android手机不同导致开发的软件不能完美适用,这里给大家介绍个播放器插件可以适应所有Android设备
    2016-07-07
  • 安卓逆向案例分析之蝉妈妈sign破解

    安卓逆向案例分析之蝉妈妈sign破解

    这篇文章主要为大家介绍了安卓逆向案例分析蝉妈妈sign破解的方式讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • Android开发之merge结合include优化布局

    Android开发之merge结合include优化布局

    这篇文章主要为大家详细介绍了Android开发之merge结合include优化布局,感兴趣的朋友可以参考一下
    2016-06-06
  • Android启动相机拍照并返回图片

    Android启动相机拍照并返回图片

    Android启动相机拍照并返回图片,事先定义拍照方法,在启动拍照之前先判断内存是否可用,通过重写onactivityresult()方法,获取拍好的图片。对下文感兴趣的朋友可以参考下
    2015-10-10
  • 往Android系统中添加服务的方法教程

    往Android系统中添加服务的方法教程

    最近因为平台升级,需要在系统中添加一些服务,所以将整个过程总结一下,下面这篇文章主要给大家介绍了往Android系统中添加服务的方法教程,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05

最新评论