Android - 从2个单独的onTouchListeners中检索变量

时间:2014-05-24 19:11:52

标签: java android

我目前有两个不同的onTouchListener个对象,每个对象都覆盖屏幕的一侧。我希望用户同时触摸两侧,我想计算两次触摸之间的时差。到目前为止,我已经设置了两个onTouchListener个对象,并在每个对象中定义了我需要的变量。但是,我很难找到两个变量之间的差异,因为它们在不同的onTouchListener对象中使用。这是我到目前为止所尝试的:

.java档案

public class Prompt5 extends Activity {

    long presstimeL1;
    long presstimeR1;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_prompt5);

            touchLayoutL.setOnTouchListener(new View.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event){

                    //More code..

                    //Set the value of the variable
                    presstimeL1 = System.currentTimeMillis();

                return true;
                }
            });

            touchLayoutR.setOnTouchListener(new View.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event){

                    //More code..

                    //Set the value of the variable
                    presstimeR1 = System.currentTimeMillis();

                return true;
                }
            });

    //Somehow find the difference between presstimeL1 and presstimeR1
    //The code below does not work because both presstimeL1 and presstimeR1 are 0 in this case
    long difference = presstimeL1 - presstimeR1;

如图所示,touchLayoutL覆盖屏幕左侧,touchLayoutR覆盖屏幕右侧,presstimeL1从屏幕左侧获取系统时间, presstimeR1从屏幕右侧获取时间。

正如您所看到的,我的目标是获得在不同onTouchListener个对象中使用的两个变量之间的区别。

0 个答案:

没有答案
相关问题