gpt4 book ai didi

java - 如何在 Android Studio 中触摸多个按钮而不释放屏幕

转载 作者:行者123 更新时间:2023-12-01 19:19:51 26 4
gpt4 key购买 nike

像这样

image

1 /image/VUNz2.png

我想在不释放的情况下滑动更多按钮。此代码不起作用,它说应用程序停止工作

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
//my buttons
Button b1, b2, b3;
//my text view
TextView txt;
//my layout if you need it
LinearLayout myl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//my buttons
Button b1 = findViewById(R.id.b1);
Button b2 = findViewById(R.id.b2);
Button b3 = findViewById(R.id.b3);

//text view
TextView txt = findViewById(R.id.txt);
//my layout if you need it
final LinearLayout myl = findViewById(R.id.myl);


b1.setOnTouchListener(this);
b2.setOnTouchListener(this);
b3.setOnTouchListener(this);
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

//touch Events
switch (view.getId()) {

//b1 touch event
case R.id.b1:

txt.setText(txt.getText().toString() + b1.getText().toString());
break;

//b2 touch event
case R.id.b2:

txt.setText(txt.getText().toString() + b2.getText().toString());
break;

//b3 touch event
case R.id.b3:

txt.setText(txt.getText().toString() + b3.getText().toString());
break;
}
}

return false;

}
}

我希望在这个网站上找到答案..非常感谢

this code does not working its says app stopped working

最佳答案

当你触摸button1时,即使你移动到button2上方(不释放),button2也不会收到任何onTouchAction。相反,您可以检查 x,y 坐标来检查用户是否将手指移动到其他按钮并执行操作。

关于java - 如何在 Android Studio 中触摸多个按钮而不释放屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59372310/

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