2017-02-22 70 views
2

我有一個TextView,具有onLongClickListenerOnClick事件,對控股TextView,它的顏色變爲紅色,並且在釋放,它的顏色應該變爲白色。OnLongCLickListener不工作,因爲我需要

問題: 當我握住TextView和移動我的手指在它之外按住,然後離開了我的手指,它的顏色不會更改爲白色。

XML

<TextView 
    android:layout_width="match_parent" 
    android:text="hello" 
    android:textColor="#ffff" 
    android:id="@+id/timer" 
    android:layout_height="wrap_content" 
    /> 

的Java

final TextView t1 = (TextView) findViewById(R.id.timer); 
    t1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      t1.setTextColor(Color.WHITE); 

     } 
    }); 
    t1.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      t1.setTextColor(Color.RED); 
      return false; 
     } 
    }); 
+0

你嘗試過在你的onLongClick返回true()? – dave

+0

你應該使用OnTouchListener來實現所需的功能 –

+0

@Abhishec Kumar我的答案將幫助你解決你的問題^ _ ^如果你需要清除一些你可以問的問題 –

回答

2

View.OnClickListener - 回調接口定義,當一個觀點是點擊被調用。 View.OnLongClickListener - 視圖已被調用時回調的接口定義點擊並保持

所以你說的是100%的真實。它應該是紅色的,因爲它被點擊並按照你的方式舉行。

但是,當我抱着文本視圖和移動我的手指文本視圖 外按住,然後離開我的手指,它不改變其顏色,以 白色

你給白色文字查看時,它只有點擊!如果你想獲得的是白色像你說的(點擊並舉行時),你需要設置白顏色OnLongClickListener

給點意見,如果你要檢測你的意見觸摸和釋放,並改變相關的顏色那則需要使用OnTouchListener,而不是clickListeners

View.OnTouchListener - 接口定義一個回調,當觸摸事件被分派到這個觀點被調用。之前給視圖

 t1.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        t1.setTextColor(Color.RED); // pressed state 
       break; 

       case MotionEvent.ACTION_UP: 
        t1.setTextColor(Color.WHITE); // Released state 
       break; 
      } 
      return true; 
     } 
    }); 
+0

它的工作,謝謝 –

+0

@Abhishec庫馬爾我看到你雖然返回false這是爲了額外的知識http://stackoverflow.com/questions/21578476/what-actually-happens-if-i-return-false-in-a-ontouchlistener –

0

使用OnTouchListener這樣你可以向下和向上事件註冊觸摸觸摸事件的回調將被調用。 MotionEvent case MotionEvent.ACTION_DOWN:將在用戶觸摸TextView時將顏色設置爲紅色,並且case MotionEvent.ACTION_UP:將在用戶將手指從TextView上擡起時將顏色設置爲白色。

final TextView t1 = (TextView) findViewById(R.id.timer); 


t1.setOnTouchListener(new View.OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
     switch (event.getAction()) { 

     case MotionEvent.ACTION_DOWN: 
      t1.setTextColor(Color.RED); 
      break; 
     case MotionEvent.ACTION_UP: 
      t1.setTextColor(Color.WHITE); 
      break; 
    } 
return true; 
     } 
    }); 
+0

加入一個onclick正在工作 –

+0

感謝哥們,爲代碼 –

0

分配onTouch監聽器並查找MotionEvent.ACTION_DOWN和MotionEvent.ACTION_MOVE:

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     // Construct a rect of the view's bounds 
     rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); 
    } 

    if (event.getAction() == MotionEvent.ACTION_MOVE) { 
     if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { 
      // User moved outside bounds 
      t1.setTextColor(Color.WHITE); 
     } 
    } 
    return false; 
} 
相關問題