从ImageView发送信息扩展到MainClass

时间:2014-04-08 12:39:49

标签: android

我有一个班级

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);

3 个答案:

答案 0 :(得分:1)

简单的事情:实施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发送一个回调时,你都会收到回调。

答案 1 :(得分:1)

在这种情况下,我会使用interface

创建一个界面,让我们说

public interface PassData {
    public void doSomethingTouchUp(); //your methods
    public void doSomethingTouchDown();
}

TouchImageView中获取参考变量,例如

public PassData delagateObj;//make the ref. public, so no getter/setter needed

接下来,在您的TouchImageView课程中,替换

//Tell main class action down

delagateObj.doSomethingTouchUp(); //similar for your other method

最后,在MainClass或您使用TouchImageView的任何地方,需要做两件事:实现PassData并为delegateObj赋值,然后覆盖方法:

public class MainClass implements PassData {

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

    @Override
    public void doSomethingTouchUp(){
        System.out.println("My method implemented!");
    } //your methods
}

答案 2 :(得分:-1)

看看这个:

handler.post(new Runnable() {
            @Override
            public void run() {
                progressBar.setProgress(value);
            }
        });

请参阅此示例http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/