问题
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;
}
}
Hi ! I would like to the same Motion Event (same actions) on 5 other Textview's, so i have to create 5 others public class CustomTouchListener1,2,3,4,5 ... or there's a way to group them all ?
回答1:
You don't need to make Custom Touch Listner.
You can achieve it by following code
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;
}
};
Hope work for you.
回答2:
You may use RecycleView or ListView instead of TextView.
回答3:
Try this! Create an anonymous class
private final View.OnTouchListener myTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
Now set this touch event to your required 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);
Now set the touch event
tv1.setOnTouchListener(myTouchListener);
tv2.setOnTouchListener(myTouchListener);
tv3.setOnTouchListener(myTouchListener);
tv4.setOnTouchListener(myTouchListener);
tv5.setOnTouchListener(myTouchListener);
回答4:
add your TextViews
to list and pass it to 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());
}
}
}
回答5:
The same OnTouchListener for several TextView, you don't need create another class
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);
回答6:
Rather creating Inner class, create anonymous class and set it directly on View
.
private final View.OnTouchListener myTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
To set on TextView
.
tv2.setOnTouchListener(myTouchListener)
来源:https://stackoverflow.com/questions/42923286/touch-listener-on-multiple-textviews