gpt4 book ai didi

java - 在屏幕上移动手指时 OnTouchListener ACTION_MOVE 不起作用

转载 作者:行者123 更新时间:2023-12-04 09:31:23 25 4
gpt4 key购买 nike

这是我想要的:

  1. 触摸屏幕。
  2. 继续移动手指。
  3. 如果您在移动手指的同时触摸屏幕上的任何按钮,按钮的颜色按钮的颜色变为绿色。

问题:“if”条件在第 57 行 (.java) 中不起作用

if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) 直到它变为 if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) 这是我不想。

请看下面的 .java 文件:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener , GestureDetector.OnGestureListener{

private Button bt1,bt2,bt3

private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// ------ buttons findViewByID

bt1 = (Button) findViewById(R.id.b1);
bt2 = (Button) findViewById(R.id.b2);
bt3 = (Button) findViewById(R.id.b3);


//------Set UP id for buttons

bt1.setId(1);
bt2.setId(2);
bt3.setId(3);

//-------set up touchlisteners for buttons

bt1.setOnTouchListener(this);
bt2.setOnTouchListener(this);
bt3.setOnTouchListener(this);

//
mGestureDetector = new GestureDetector(this,this);

}
//---------Override Event
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

/*----------When finger touched + finger is moving. But the condition below is not working finger moving event (ACTION_MOVE) not working. but finger touch event(ACTION_DOWN) is working*/
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) // condition doesn't work until it changes //if(motionEvent.getAction() == MotionEvent.ACTION_DOWN).
{
switch (view.getId())
{
case 1:
bt1.setBackgroundColor(GREEN);
break;
case 2:
bt2.setBackgroundColor(GREEN);
break;
case 3:
bt3.setBackgroundColor(GREEN);
break;
}

}

return true;
}
//--------Other Overrides
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}

@Override
public void onShowPress(MotionEvent motionEvent) {

}

@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}

@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {

return false;
}

@Override
public void onLongPress(MotionEvent motionEvent) {

}

@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}



}

请看下面的 .XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/lin"

tools:context=".MainActivity">

//------Mode: HORIZONTAL
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
// ------ BUTTONS
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:layout_weight="1"/>
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:layout_weight="1" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:layout_weight="1"/>
</LinearLayout>

</LinearLayout>

最佳答案

那是因为当您在按钮上设置 onTouchListener 并首先开始移动容器 (LinearLayout) 触摸事件时,它不会传递给子(按钮)。要使其工作,您必须在容器上设置一个监听器,然后检查触摸坐标是否包含在按钮矩形中。

代码:

private Button bt1, bt2, bt3;
private LinearLayout lin;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ------ buttons findViewByID
bt1 = (Button) findViewById(R.id.b1);
bt2 = (Button) findViewById(R.id.b2);
bt3 = (Button) findViewById(R.id.b3);
lin = findViewById(R.id.lin);
//-------set up touchlisteners for buttons
lin.setOnTouchListener(this);
//
mGestureDetector = new GestureDetector(this, this);
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE)
{
Rect but1 = new Rect();
bt1.getHitRect(but1);
if (but1.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
{
bt1.setBackgroundColor(getColor(R.color.colorAccent));
return true;
}
Rect but2 = new Rect();
bt2.getHitRect(but2);
if (but2.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
{
bt2.setBackgroundColor(getColor(R.color.colorAccent));
return true;
}
Rect but3 = new Rect();
bt3.getHitRect(but3);
if (but3.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
{
bt3.setBackgroundColor(getColor(R.color.colorAccent));
return true;
}
}
return true;
}

我测试了这段代码,它工作正常。
当然,你可以做得更好,当你有 200 个按钮时,在 onCreate() 中加载矩形会比每次在触摸事件中创建它们更好

关于java - 在屏幕上移动手指时 OnTouchListener ACTION_MOVE 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62825296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com