2017-03-21 99 views
0
private TextView tv2; 

tv2=(TextView)findViewById(R.id.textView2); 
tv2.setOnTouchListener(new CustomTouchListener()); 

public class CustomTouchListener implements View.OnTouchListener { 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      switch(motionEvent.getAction()){ 

       case MotionEvent.ACTION_DOWN: 
        Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 80); 
        Hauteur.startAnimation(AnimationUtils.loadAnimation(recuperationJson.this, android.R.anim.slide_in_left)); 
        break; 

       case MotionEvent.ACTION_UP: 
        // Action you you want on finger up 
        Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60); 

        break; 
      } 
      return true; 
     } 
    } 

嗨!我想對其他5個Textview使用相同的動作事件(相同的動作),所以我必須創建另外5個公共類CustomTouchListener1,2,3,4,5 ...或者有一種方法將它們全部組合在一起?觸摸多個Textviews的監聽器?

+0

感謝大家,我chosed穆沙希德卡特里解決方案 – AlexMel

回答

0

你不需要進行自定義觸摸聽者。

您可以通過下面的代碼

private TextView tv2,tv1; 

    tv1=(TextView)findViewById(R.id.textView1); 
    tv1.setOnTouchListener(onTouchListener); 

    tv2=(TextView)findViewById(R.id.textView2); 
    tv2.setOnTouchListener(onTouchListener); 



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




      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 80); 
        Hauteur.startAnimation(AnimationUtils.loadAnimation(recuperationJson.this, android.R.anim.slide_in_left)); 

        //Here if you want to know from wich touch this event has accured you can do following code 
        switch (v.getId()){ 
         case R.id.textView1: 
          break; 
         case R.id.textView2: 
          break; 
         case R.id.textView3: 
          break; 
        } 

        break; 
       case MotionEvent.ACTION_UP: 
        // Action you you want on finger up 
        Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60); 

        switch (v.getId()){ 
         case R.id.textView1: 
          break; 
         case R.id.textView2: 
          break; 
         case R.id.textView3: 
          break; 
        } 


        break; 
      } 

      return true; 
     } 
    }; 

你希望工作實現它。

+0

它的工作原理謝謝:) – AlexMel

0

您可以使用RecycleView或ListView代替TextView。

0

而是創建內部類,創建匿名類並直接將其放在View

private final View.OnTouchListener myTouchListener = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return false; 
    } 
} 

設置爲TextView

tv2.setOnTouchListener(myTouchListener) 
0

試試這個!創建一個匿名類

private final View.OnTouchListener myTouchListener = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return false; 
    } 
} 

現在設置這個觸摸事件你需要textviews

private TextView tv1,tv2.tv3,tv4,tv5; 
tv1=(TextView)findViewById(R.id.textView1); 
tv2=(TextView)findViewById(R.id.textView2); 
tv3=(TextView)findViewById(R.id.textView3); 
tv4=(TextView)findViewById(R.id.textView4); 
tv5=(TextView)findViewById(R.id.textView5); 

現在設置的觸摸事件

tv1.setOnTouchListener(myTouchListener); 
tv2.setOnTouchListener(myTouchListener); 
tv3.setOnTouchListener(myTouchListener); 
tv4.setOnTouchListener(myTouchListener); 
tv5.setOnTouchListener(myTouchListener); 
0

添加TextViews到列表,並將其傳遞給setListnerOnTextViews

private function setListnerOnTextViews(List<TextView> tvs){ 
    for(int i =0; i<tvs.size;i++){ 
    if(tvs.get(0) instance of TextView){ 
     tvs.get(0).setOnTouchListener(new CustomTouchListener()); 
    } 
    } 

} 
0

同樣OnTouchListener幾個TextView的,你不需要創建另一個類

 private TextView tv2, tv3, tv4, tv5; 
     tv2 = (TextView) findViewById(R.id.textView2); 
     tv3 = (TextView) findViewById(R.id.textView3); 
     tv4 = (TextView) findViewById(R.id.textView4); 
     tv5 = (TextView) findViewById(R.id.textView5); 

     View.OnTouchListener listener = new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       return false; 
      } 
     }; 
     tv2.setOnTouchListener(listener); 
     tv3.setOnTouchListener(listener); 
     tv4.setOnTouchListener(listener); 
     tv5.setOnTouchListener(listener);