浅谈android回调

95 篇文章 0 订阅

                                      安卓回调机制在很多场合会用到,比如我们用的监听都是回调接口,这里我用一个小获取头像demo来演示下回调


XML代码:

mainactivity的xml代码;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dialogfragmentdemo.MainActivity">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

//dialogframgent的xml代码

<!--注意背景颜色设置半透明#6000000-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dialog">

    <TextView
        android:id="@+id/tv_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        android:gravity="center"
        android:padding="10dp"
        android:text="从相机获取"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="18sp" />

    <View
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_above="@id/tv_camera"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/line"
        android:background="@android:color/white"
        android:gravity="center"
        android:padding="10dp"
        android:text="从相册获取"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="18sp" />
</RelativeLayout>


java代码

//dialogfragment代码

<!--注意背景颜色设置半透明#6000000-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dialog">

    <TextView
        android:id="@+id/tv_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        android:gravity="center"
        android:padding="10dp"
        android:text="从相机获取"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="18sp" />

    <View
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_above="@id/tv_camera"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/line"
        android:background="@android:color/white"
        android:gravity="center"
        android:padding="10dp"
        android:text="从相册获取"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="18sp" />
</RelativeLayout>

//mainactivity代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView iv_icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        iv_icon = (ImageView) findViewById(R.id.iv_icon);
        iv_icon.setOnClickListener(this);
    }

    //监听点击头像
    @Override
    public void onClick(View v) {

        CustomDialogFragment dialog = new CustomDialogFragment();
        //启动
        // 括号参数(fragment管理员,注意你导包是上面fragment就用什么管理员,随便定义个字符串,用于区分多个dialog用)
        dialog.show(getFragmentManager(), "tag");

       /* //第四步 通过接口所在的类点击接口对象
        CustomDialogFragment.ICallBack iCallBack = new CustomDialogFragment.ICallBack() {
            @Override
            public void getImage(Uri uri) {
                //获取数据;接口括号参数就是结果
                iv_icon.setImageURI(uri);
            }
        };

        //第五步,把接口对象设置到接口定义的set方法里面
        dialog.setiCallBack(iCallBack);*/

        //简写通过接口所在的类的对象调用用接口的set方法;实现接口获取接口括号参数,就是结果;
        dialog.setiCallBack(new CustomDialogFragment.ICallBack() {
            @Override
            public void getImage(Uri uri) {
                iv_icon.setImageURI(uri);
            }
        });


    }
}

//总结回调实现

  第一 在获取数据,需要把数据传递出去的类中 定义一个接口,需要传递数据就在接口里面定义一个方法括号参数就是传递的数据

//通过回调把数据传给mainActivity
//第一步定义个接口(如果需要带结果回去就,在里面定义个方法括号参数就是带回去的结果)
public interface ICallBack {
    void getImage(Uri uri);
}


//第二步定义个接口对象 创建个set方法括号参数有接口
private ICallBack iCallBack;

public void setiCallBack(ICallBack iCallBack) {
    this.iCallBack = iCallBack;
}

//地三步获取数据给接口方法里面的参数赋值
iCallBack.getImage(uri);

//第四步 通过接口所在的类的对象调用用接口的set方法;实现接口获取接口括号参数,就是结果;
dialog.setiCallBack(new CustomDialogFragment.ICallBack() {
    @Override
    public void getImage(Uri uri) {
        iv_icon.setImageURI(uri);
    }
});


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值