gpt4 book ai didi

android - 从 ImageView 发送信息扩展到 MainClass

转载 作者:行者123 更新时间:2023-11-30 02:58:22 24 4
gpt4 key购买 nike

我有课

TouchImageView extends ImageView

然后我在这门课上:

setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {


switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:
//Tell main class action down
break;

case MotionEvent.ACTION_MOVE:
//Tell main class action move

break;

case MotionEvent.ACTION_UP:
//Tell main class action up
break;

}


}

在这个类中,我实现了 onTouch 函数。我需要的是此函数将在使用此 TouchImageView 的主类中执行某些操作;

private TouchImageView touch_iv = (TouchImageView) findViewById(R.id.imageView1);

最佳答案

简单的事情:实现一个setParent(...)方法
然后调用主类的特定函数:
我假设您的主类名为 Main.java

TouchImageView 中:

...

Main parent = null;

public void setParent(Main main)
{
parent = main;
}

...

setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
if (parent != null)
parent.ActionDown(TouchImageView.this, event);
break;

case MotionEvent.ACTION_MOVE:
if (parent != null)
parent.ActionMove(TouchImageView.this, event);
break;

case MotionEvent.ACTION_UP:
if (parent != null)
parent.ActionUp(TouchImageView.this, event);
break;
}
}
}

...

Main 中:

...

private TouchImageView touch_iv = (TouchImageView)findViewById(R.id.imageView1);
touch_iv.setParent(this);

...

public void ActionDown(TouchImageView view, MotionEvent event)
{
//handle down event "event" freom view "view"
}

public void ActionMove(TouchImageView view, MotionEvent event)
{
//handle move event "event" freom view "view"
}

public void ActionUp(TouchImageView view, MotionEvent event)
{
//handle up event "event" freom view "view"
}

...

应该可以。
现在每次 touchimageview 发送时你都会得到一个回调。

关于android - 从 ImageView 发送信息扩展到 MainClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com