我有一些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;
}
}
}
發佈你迄今試過的代碼 –
清理語法。 – Chuck
代碼只是例如chuck – joney