Android回调机制之Activity与DialogFragment数据传递

公众号同名

#Android回调机制之Activity与DialogFragment数据传递
在实际编程过程中,我们经常碰到这样一种情况:

在新建的Dialog或者Fragment中修改数据,但是要在Activity或其他位置进行数据更新,那怎么处理呢?此时我们的回调机制就派上了用场。

那么什么是回调机制呢,回调机制是一种依赖于接口的传递机制,旨在为其他类暴漏方法以使得其他类具备获取数据权限(PS:这里的权限指的是修饰词引起的访问权限)。说这么多,估计大多数人还是一头雾水,接下来,让我们写一个Demo实践一下吧。

我们在新建的项目中新建CustomDialogFragment类,继承自DialogFragment并重写基本构造方法,代码如下:

public class CustomDialogFragment extends DialogFragment {
    public CustomDialogFragment() {
        super();
    }
}

接着创建CustomDialogFragment的布局文件,并重写其onCreateView()方法加载自定义布局。

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="#FFFFBB33"
        android:contentDescription="@string/app_name"
        android:scaleType="center"
        />

    <EditText
        android:id="@+id/id_txt_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
       
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值