沉默然后重启onTouchListener

时间:2014-05-20 15:39:19

标签: android

我有onTouchListener会记录接触点的坐标,但在接触点的坐标存储为变量var1后会静音。在此之后,我想开始另一个onTouchListener,它将记录下一个接触点的坐标,并将它们存储为变量var2。我尝试了这个,如下面的.java文件所示:

.java档案

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final View touchLayout = findViewById(R.id.touchLayout);
        final TextView item1 = (TextView)findViewById(R.id.item1);
        final TextView item2 = (TextView)findViewById(R.id.item2);

        \\First onTouchListener Begins
        touchLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Integer var1x = Integer.valueOf((int) event.getX());
                Integer var1y = Integer.valueOf((int) event.getY());
                item1.setText("Touch coordinates : " + touchvar1x + " x " + touchvar1y);

                touchLayout.setOnTouchListener(null);
                return true;

                }
                }); 

        \\Second onTouchListener Begins         
        touchLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Integer var2x = Integer.valueOf((int) event.getX());
                Integer var2y = Integer.valueOf((int) event.getY());
                item2.setText("Touch coordinates : " + touchvar2x + " x " + touchvar2y);

                touchLayout.setOnTouchListener(null);
                return true;

                }
                }); 
        }       


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
    }

}

正如你所看到的,我的尝试只是沉默然后启动另一个onTouchListener。但是,这会导致第一个onTouchListener根本不起作用。有没有理由发生这种情况,是否可以修复它?

提前致谢

1 个答案:

答案 0 :(得分:0)

由于您只需要发生两个事件,因此最好只在第一个侦听器中添加一个布尔表达式并一起删除第二个侦听器。

有些事情如下:

touchLayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(!userTouchedOnce){
            Integer var1x = Integer.valueOf((int) event.getX());
            Integer var1y = Integer.valueOf((int) event.getY());
            item1.setText("Touch coordinates : " + touchvar1x + " x " + touchvar1y);

            userTouchedOnce = true;
        }
        else
        {
            Integer var2x = Integer.valueOf((int) event.getX());
            Integer var2y = Integer.valueOf((int) event.getY());
            item2.setText("Touch coordinates : " + touchvar2x + " x " + touchvar2y);
        }
        return true;

    }
}); 

//编辑 - 你想办法做三件事。

int touchNumber = 1;

touchLayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(touchNumber) {
          case 1:  Integer var1x = Integer.valueOf((int) event.getX());
                   Integer var1y = Integer.valueOf((int) event.getY());
                   item1.setText("Touch coordinates : " + touchvar1x + " x " + touchvar1y);
                   touchNumber++;
                   break;
          case 2:  Integer var2x = Integer.valueOf((int) event.getX());
                   Integer var2y = Integer.valueOf((int) event.getY());
                   item2.setText("Touch coordinates : " + touchvar2x + " x " + touchvar2y);
                   touchNumber++;
                   break;
          case 3: // Do something else.
                   break;
          default: break;
        }

        return true;

    }
});