在可扩展按钮中实现回调 class

Implementing callback in extendable button class

我有一个非常简单的 'CustomButton' class,它扩展了默认值 'Button' class。我的 CustomButton 使用 onTouchEvent,我想将一个函数从我的 Activity 传递给 CustomButton 并让它在按下时执行。

CustomButton class 工作正常,但我似乎无法弄清楚如何将函数传递给它。

Activity:

public class mainActivity extends Activity
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        Context context = getApplicationContext();
        setContentView( R.layout.main );

        LinearLayout root = (LinearLayout) findViewById( R.id.myLayout   );   
        View child1 = getLayoutInflater().inflate( R.layout.child, null );

        // Define the button
        final CustomButtom myCustomButton = (CustomButtom)child1.findViewById( R.id.button_id );   

        myCustomButtom.setCallback( test ); // <-- I want to pass my 'test' function to CustomButton class, 
                                            //     so it can get executed by the onTouchEvent

        root.addView( myCustomButton );

        super.onCreate( savedInstanceState );
    }

    private int test()
    {
        Log.d( "test", "Callback executed!" );
    }
}

这是我的自定义按钮 class:

public class CustomButtom extends Button
{
    private Function callback;

    public CustomButtom(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setOnTouchListener
        (
            new OnTouchListener() 
            {
                @Override
                public boolean onTouch(View v, MotionEvent event) 
                {
                    if(event.getAction() == MotionEvent.ACTION_DOWN) 
                    {
                        executeCallback(); // <-- My callback would get executed from here
                    } 

                    return true;
                }
            }
        );

    }

    public void setCallback(Function function)
    {
         callbackFunction = function; // Save the callback in a local variable
    }

    private boolean executeCallback()
    {
        return callbackFunction.execute(); // execute the callback
    }
}

是否有 'data type'(例如 'Function')我可以用于此目的,或者是否有其他方法可以实现此目的?谢谢!

当您想在 Java 中执行某个操作时,通常需要一个对象和一个函数。这就是接口的用途。您使用函数名称声明一个接口。您的 Activity 实现此接口和函数,然后您将 activity 的实例传递给按钮。在您的情况下,您可能正在寻找 OnTouchListener 或 OnClickListener?如果你想有一个特殊的接口,你可以用同样的方式声明它。

public class mainActivity extends Activity implements OnClickListener
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        // I do not see any reason to use the Application Context here. Your Activity has the right context for your UI
        // Context context = getApplicationContext();
        setContentView( R.layout.main );

        LinearLayout root = (LinearLayout) findViewById( R.id.myLayout   );   
        View child1 = getLayoutInflater().inflate( R.layout.child, null );

        // Define the button
        final CustomButtom myCustomButton = (CustomButtom)child1.findViewById( R.id.button_id );   

        myCustomButtom.setOnClickListener(this);

        root.addView( myCustomButton );

        // you did this already
        //super.onCreate( savedInstanceState );
    }

    public void onClick(View v)
    {
        Log.d( "test", "Callback executed!" );
    }
}

编辑:

要将逻辑保留在按钮内,您可以创建自己的界面(OnClickListener 是一个界面),如下所示:

public interface OnCustomActionListener {
    // you can remove the button as parameter if you do not care which button the action came from 
    void onCustomAction(CustomButton button);
}

public class CustomButtom extends Button {
    OnCustomActionListener onCustomActionListener;

    public void setOnCustomActionListener(OnCustomActionListener listener) {
        this.onCustomActionListener = listener;
    }

    /* Creator like in your question mentioned */

    private boolean executeCallback() {
        if (this.onCustomActionListener != null) {
            this.onCustomActionListener.onCustomAction(this);
        }
    }
}

在你的Activity中:

public class mainActivity extends Activity implements OnCustomActionListener {
...
     myCustomButtom.setOnCustomActionListener(this);
...
public void onCustomAction(CustomButton button) {
     // do something
}