Android 选择按钮后,我拖动手指和释放
如何在拖动手指并释放后选择按钮 像这样: 我的密码是从 起始代码:Android 选择按钮后,我拖动手指和释放,android,Android,如何在拖动手指并释放后选择按钮 像这样: 我的密码是从 起始代码: imageButtonAppOptionsViewPage.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) {
imageButtonAppOptionsViewPage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
final Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
v.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE));
else
v.vibrate(100);
constraintLayoutAppOptionAddNewPage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionAddNewPage.startAnimation(animationShowButtonRight);
animationShowButtonRight.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
constraintLayoutAppOptionDeletePage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionDeletePage.startAnimation(animationShowButtonRightUp);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animationShowButtonRightUp.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
constraintLayoutAppOptionResetPage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionResetPage.startAnimation(animationShowButtonLeftUP); }
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animationShowButtonLeftUP.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
constraintLayoutAppOptionChangePage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionChangePage.startAnimation(animationShowButtonLeft); }
@Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_UP:
float endX = event.getX();
float endY = event.getY();
for(int i = 0; i < constraintLayoutAppOption.getChildCount(); i++){
if(constraintLayoutAppOption.getChildAt(i) instanceof ConstraintLayout){
ConstraintLayout constraintLayout = (ConstraintLayout) constraintLayoutAppOption.getChildAt(i);
for (int y = 0; y<constraintLayout.getChildCount();y++){
if(constraintLayout.getChildAt(y) instanceof Button){
Button b = (Button) constraintLayout.getChildAt(y);
if(isPointWithin((int)endX,(int)endY,b.getLeft(), b.getRight(), b.getTop(), b.getBottom())){
b.setBackgroundColor(Color.BLUE);
}else{
b.setBackgroundColor(Color.WHITE);
}
}
break;
}
}
}
break;
}
return true;
}
});
public static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
}
imageButtonAppOptionsViewPage.setOnTouchListener(新视图.OnTouchListener()){
@凌驾
公共布尔onTouch(视图、运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
最终可控震源v=(可控震源)context.getSystemService(context.可控震源_服务);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O)
v、 振动(振动效果.createOneShot(100,振动效果.默认振幅));
其他的
v、 振动(100);
constraintLayoutAppOptionAddNewPage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionAddNewPage.startAnimation(动画ShowButtonRight);
animationShowButtonRight.setAnimationListener(新建Animation.AnimationListener()){
@凌驾
onAnimationStart上的公共无效(动画){
}
onAnimationEnd上的公共无效(动画){
constraintLayoutAppOptionDeletePage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionDeletePage.startAnimation(动画显示按钮右起);
}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
});
animationShowButtonRightUp.setAnimationListener(新建Animation.AnimationListener()){
@凌驾
onAnimationStart上的公共无效(动画){
}
onAnimationEnd上的公共无效(动画){
constraintLayoutAppOptionResetPage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionResetPage.startAnimation(animationShowButtonLeftUP);}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
});
animationShowButtonLeftUP.setAnimationListener(新建Animation.AnimationListener()){
@凌驾
onAnimationStart上的公共无效(动画){
}
onAnimationEnd上的公共无效(动画){
constraintLayoutAppOptionChangePage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionChangePage.startAnimation(animationShowButtonLeft);}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
});
打破
case MotionEvent.ACTION\u UP:
float endX=event.getX();
float endY=event.getY();
for(int i=0;i 对于(int y=0;y我用它在按下按钮时在屏幕上移动texView,但也可以在视图上使用它
right.setOnTouchListener(new View.OnTouchListener() {
private Handler mHandler;
@Override public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mHandler != null) return true;
mHandler = new Handler();
mHandler.postDelayed(mAction, 100);
break;
case MotionEvent.ACTION_UP:
if (mHandler == null) return true;
mHandler.removeCallbacks(mAction);
mHandler = null;
break;
}
return false;
}
Runnable mAction = new Runnable() {
@Override public void run() {
System.out.println("Performing action...");
game.getTanks().get(game.getCurrentTank()).goRight();
mHandler.postDelayed(this, 100);
}
};
});
*我不太清楚问题是什么,这段代码的哪一部分起作用了,还有什么部分不起作用了?基本的想法是获得所有按钮的按钮和动作之间的距离,并相应地改变背景。@Joachim Haglund代码需要使一个按钮为蓝色,但所有按钮都为白色,这是因为任何按钮都没有按下@Rahul Kumar你能帮我吗?我不想移动视图,我想当我触摸按钮时,我会将手指移到按钮上,当我释放按钮时,botton将被按下并执行代码操作,你可以在这个链接中看到一个操作示例,然后当你触摸按钮并添加你想要执行的代码时,创建一个新的Runnable。我有4个按钮,系统如何运行tem会知道我按的按钮是谁吗?