2014-05-20 62 views
0

我有一個onTouchListener將記錄一個接觸點的座標,但將在接觸點的座標存儲爲變量var1後沉默。在此之後,我想開始另一個onTouchListener,它將記錄下一個接觸點的座標,並將它們存儲爲變量var2。我嘗試這是表現在以下.java文件:沉默,然後重新啓動onTouchListener

.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根本不起作用。是否有這種情況發生的原因,是否有可能解決這個問題?

在此先感謝

回答

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; 

    } 
}); 
+0

很酷的想法,你知道我怎麼可能爲3,而不是2擴大呢? – user3616949

+0

查看我的更新回答 – MJ93

+0

很好,謝謝! – user3616949

相關問題