一手遮天 Android - java: Callback

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - java: Callback

示例如下:

/java/CallbackDemo1.java

/**
 * 本例用于演示如何使用支持回调的类
 *
 * 定义回调接口参见:CallbackDemo1_CallbackInterface.java
 * 实现支持回调的类参见:CallbackDemo1_CallbackClass.java
 */

package com.webabcd.androiddemo.java;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.webabcd.androiddemo.R;

public class CallbackDemo1 extends AppCompatActivity {

    private TextView mTextView1;

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

        mTextView1 = findViewById(R.id.textView1);

        sample();
    }

    private void sample() {
        CallbackDemo1_CallbackClass xxx = new CallbackDemo1_CallbackClass(new CallbackDemo1_CallbackInterface() {
            @Override
            public void ok() {
                mTextView1.setText("ok");
            }

            @Override
            public void error(String errMsg) {
                mTextView1.setText("error: " + errMsg);
            }
        });
        xxx.execute();
    }
}


\java\CallbackDemo1_CallbackInterface.java

/**
 * 本例用于演示如何定义回调接口
 *
 * 实现支持回调的类参见:CallbackDemo1_CallbackClass.java
 * 使用支持回调的类参见:CallbackDemo1.java
 */

package com.webabcd.androiddemo.java;

public interface CallbackDemo1_CallbackInterface {
    void ok();
    void error(String errMsg);
}

\java\CallbackDemo1_CallbackClass.java

/**
 * 本例用于演示如何实现支持回调的类
 *
 * 定义回调接口参见:CallbackDemo1_CallbackInterface.java
 * 使用支持回调的类参见:CallbackDemo1.java
 */

package com.webabcd.androiddemo.java;

import java.util.Date;

public class CallbackDemo1_CallbackClass {

    private CallbackDemo1_CallbackInterface _callback;

    public CallbackDemo1_CallbackClass(CallbackDemo1_CallbackInterface callBack){
        this._callback = callBack;
    }

    public void execute() {
        if (_callback != null) {
            if (new Date().getTime() % 2 == 0) {
                _callback.ok();
            } else {
                _callback.error("错误信息");
            }
        }
    }
}

\java\CallbackDemo1_CallbackClass.java

/**
 * 本例用于演示如何实现支持回调的类
 *
 * 定义回调接口参见:CallbackDemo1_CallbackInterface.java
 * 使用支持回调的类参见:CallbackDemo1.java
 */

package com.webabcd.androiddemo.java;

import java.util.Date;

public class CallbackDemo1_CallbackClass {

    private CallbackDemo1_CallbackInterface _callback;

    public CallbackDemo1_CallbackClass(CallbackDemo1_CallbackInterface callBack){
        this._callback = callBack;
    }

    public void execute() {
        if (_callback != null) {
            if (new Date().getTime() % 2 == 0) {
                _callback.ok();
            } else {
                _callback.error("错误信息");
            }
        }
    }
}

/layout/activity_java_callbackdemo1.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 12:02  webabcd  阅读(68)  评论(0编辑  收藏  举报