2015-10-08 58 views
-1

我有一些TextViews,我想要更改所有項目的顏色,因爲用戶只需一次點擊拖動光標即可。android setOnTouchListener更改多項

我用setontouchlistener,但是這個方法只爲第一個按鈕調用動作。無論如何,當他們拿着鼠標滑過它們時,在同一次點擊中,所有人都會這麼做嗎?

這是我的代碼:

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    TextView tv1,tv2,tv3,tv4,tv5; 
    Button bu,bu1,bu2; 


    MyTouchListener touchListener = new MyTouchListener(); 

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

     tv1.setId(1); 
     tv2.setId(2); 
     tv3.setId(3); 
     tv4.setId(4); 
     tv5.setId(5); 


     tv1.setOnTouchListener(touchListener); 
     tv2.setOnTouchListener(touchListener); 
     tv3.setOnTouchListener(touchListener); 
     tv4.setOnTouchListener(touchListener); 
     tv5.setOnTouchListener(touchListener); 
    } 

    public class MyTouchListener implements View.OnTouchListener { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch(v.getId()){ 
       case 1: 
        tv1.setTextColor(0xffff0000); 
        break; 
       case 2: 
        tv2.setTextColor(0xffff0000); 
        break; 
       case 3: 
        tv3.setTextColor(0xffff0000); 
        break; 
       case 4: 
        tv4.setTextColor(0xffff0000); 
        break; 
       case 5: 
        tv5.setTextColor(0xffff0000); 
        break; 
      } 
      return true; 
     } 

    } 

} 
+0

發佈你迄今試過的代碼 –

+0

清理語法。 – Chuck

+0

代碼只是例如chuck – joney

回答

0

如果我正確認識你,你想要的代碼,當用戶無論是觸摸屏或移動他們的手指在一個TextView的內容,在觸摸屏執行。由於所有觸摸事件都不是默認傳遞給子視圖,因此處理此操作的一種方法是覆蓋活動的onTouchEvent方法並檢查每個TextView的位置。我爲你做了一個這樣做的例子:

pulic class SomeActivity extends Activity { 
    private TextView tv01; 
    ... 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     // Get the center location of the TextView. 
     int[] tv01pos = {(int) tv01.getX(), (int) tv01.getY()}; 

     // Some debuging logs, may be deleted. 
     Log.d("position TV x", String.valueOf(tv01pos[0])); 
     Log.d("position TV y", String.valueOf(tv01pos[1])); 

     // Get the width and height of textView 01 for calculating the right and bottom boundaries. 
     int tv01Wide = tv01.getWidth(); 
     int tv01High = tv01.getHeight(); 

     // Get the location of the touch event, event may be of type Donw, Move, or Up. 
     // Keep in mind there are a LOT of these generated. 
     int[] touchPos = new int[2]; 
     touchPos[0] = (int) event.getAxisValue(MotionEvent.AXIS_X); 
     touchPos[1] = (int) event.getAxisValue(MotionEvent.AXIS_Y); 

     // More debug logs. 
     Log.d("position Tch x", String.valueOf(touchPos[0])); 
     Log.d("position Tch y", String.valueOf(touchPos[1])); 

     // Check if the touch event is in the boundaries of TextView 01 
     if ( (touchPos[0] > tv01pos[0]   ) && 
       (touchPos[0] < tv01pos[0] + tv01Wide) && 
       (touchPos[1] > tv01pos[1]   ) && 
       (touchPos[1] < tv01pos[1] + tv01High)) { 

      // A toast letting you know it's working. Put your code here. 
      Toast.makeText(view.getContext(), "In TextView01", Toast.LENGTH_SHORT).show(); 
     } 

     return true; 
    } 
} 
+0

謝謝Derek你給了一個好主意,我開始使用eventmotion.getRawY()和getRawX來放置觸摸,無論如何,你的示例需要api 16以上,但是用你的想法來獲得它,經過多次嘗試後,它的工作表示感謝 – joney