我有一个非常简单的' CustomButton'扩展默认'按钮'类。我的CustomButton使用onTouchEvent,我想将一个函数从我的Activity传递给CustomButton并在触摸时执行它。
CustomButton类工作正常,但我似乎无法弄清楚如何将函数传递给它。
的活动:
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!" );
}
}
这是我的CustomButton类:
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
}
}
是否有&#39;数据类型&#39;例如&#39;功能&#39;我可以用于此目的还是有不同的方法如何实现这一目标?谢谢!
答案 0 :(得分:0)
当您想在Java中执行操作时,通常需要一个对象和一个函数。这就是Interfaces的用途。您声明一个具有函数名称的接口。您的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);
}
}
}
在您的活动中:
public class mainActivity extends Activity implements OnCustomActionListener {
...
myCustomButtom.setOnCustomActionListener(this);
...
public void onCustomAction(CustomButton button) {
// do something
}