问题
In my app, I have an activity in which there are 9 buttons and 1 textView
. I am making use of OnTouchListener
to change the text in the textView
as I move my finger accross the buttons.
This is what my activity looks like:
Here is my java code:
button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 1");
return false;
}
});
button2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 2");
return false;
}
});
button3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 3");
return false;
}
});
button4.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 4");
return false;
}
});
button5.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 5");
return false;
}
});
button6.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 6");
return false;
}
});
button7.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 7");
return false;
}
});
button8.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 8");
return false;
}
});
button9.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Button 9");
return false;
}
});
But the problem with this code is that it only gets the OnTouchListener
of the first button I touched. In simple words, when i move my finger, the button that I touched first changes the textView
while the rest of the buttons don't.
I searched on the net and stackoverflow as well and I came across something like MotionEvent.ACTION_MOVE
but I don't know how to use it and I am not sure if it is applicable in this case.
Please help me solve this problem! thank you for your time.
回答1:
This won't work in Android. If underlying View doesn't consume first event (ACTION_DOWN) by returning true, any other events that occur outside of it will be handled by parent View only. If it consume event - all events until ACTION_UP and it itself will be delivered to this View.
If you want to implement such behavior, you need to process all events within a single View, or override dispatchTouchEvent method of parent View to define your own behavior in that case.
回答2:
you can use a switch case :
//
button1.setOnTouchListener(this);
button2.setOnTouchListener(this);
button3.setOnTouchListener(this);
button4.setOnTouchListener(this);
button5.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getID())
{
case(R.id.btn1):
{
textView.setText("Button 1");
break;
}
case(R.id.btn2):
{
textView.setText("Button 2");
break;
}
case(R.id.btn3):
{
textView.setText("Button 3");
break;
}
case(R.id.btn4):
{
textView.setText("Button4");
break;
}
case(R.id.btn5):
{
textView.setText("Button 5");
break;
default: // bla
}
return true;
}
回答3:
you can use setOnClickListener
or switch case for doing whatever you want
either you can implements OnTouchListener
on your activity and use ACTION_UP
i am doing a first method for you here
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Button 1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Button 2");
}
});
you can continue with this ...
来源:https://stackoverflow.com/questions/21813946/change-textview-while-moving-finger-across-buttons