ANDROID如何在两个类之间传递变量

时间:2015-04-23 10:26:36

标签: android class

我有两个课程:SingleTouchEventViewOutput

如何将变量eventXSingleTouchEventView传递到Output并在文本视图中显示?

public class SingleTouchEventView extends View {
    private Paint paint = new Paint();
    private Path path = new Path();
    private final String TAG = "LOG";

    public static Handler handler; 
    private long delay;
    protected boolean hideView = false;

    public void setDelay(long delay) {
        this.delay = delay;
    }

    Handler h2 = new Handler() {
    };
    Runnable run = new Runnable() {

        @Override
        public void run() {
            onTouchUp();
        }
    };
    public SingleTouchEventView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);     
    }
    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                h2.removeCallbacks(run);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                h2.postDelayed(run, delay);
                if(hideView) {
                    return false;
                }
                break;
            default:
                return false;
        }

        invalidate();
        return true;
    }
    protected void onTouchUp() {

    }    
    public void clear() {

    }

}

派生类

public class Output extends SingleTouchEventView

1 个答案:

答案 0 :(得分:1)

如果不深入了解您的核心结构,我可以提出一个建议。尝试使用事件总线传输数据。有很多可用的,我喜欢GreenRobots EventBus

您只需发布一个包含所需数据的类,任何已注册的类都可以监听此数据。