java - android在触摸和移动时获取按钮文本
问题描述
我有一个带有 4 个按钮和文本视图的布局,并会通过选择字母来创建一个单词来制作一个小游戏。
我的问题是关于如何通过一次触摸获得多个按钮值并在按钮上移动?我尝试了这段代码,但每次触摸都只得到一个值,移动时没有得到其他按钮的任何值
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="102dp"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="A" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"
android:text="T" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="90dp"
android:text="C" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="82dp"
android:text="F" />
</RelativeLayout>
活动
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
Button btn1, btn2, btn3, btn4;
TextView txtresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
txtresult = (TextView) findViewById(R.id.txtresult);
btn1.setOnTouchListener(this);
btn2.setOnTouchListener(this);
btn3.setOnTouchListener(this);
btn4.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
switch (view.getId()) {
case R.id.btn1:
txtresult.setText(txtresult.getText().toString() + btn1.getText().toString());
break;
case R.id.btn2:
txtresult.setText(txtresult.getText().toString() + btn2.getText().toString());
break;
case R.id.btn3:
txtresult.setText(txtresult.getText().toString() + btn3.getText().toString());
break;
case R.id.btn4:
txtresult.setText(txtresult.getText().toString() + btn4.getText().toString());
break;
}
}
return false;
}
}
结果应该是这样的:
解决方案
我认为您应该在布局视图上设置一个触摸侦听器,并尝试获取 X 和 Y 并检测触摸点的 X 和 Y 是否在您的按钮上。
推荐阅读
- javascript - React Native 中的机读区/护照阅读
- javascript - 在上下文中从 javascript 更新 Blazor InputText 组件值
- php - 检测数组是否包含另一个数组
- coq - 矛盾陈述的使用
- java - 当两个文件都在同一个文件夹中时,为什么 relativize 会产生“../file_name”?
- apache - .htaccess 内部路径重写的最佳实践?
- css - 伪元素在 chrome 手机上消失,在桌面上运行良好
- c - 如何将文本文件与单词列表进行比较,无论哪个单词不在列表中,我都必须在第三个文件中写下(在 C 中)
- flutter - 针对 Google Ads 的严格安全 Cookie 政策
- java - 如何从控制器中的 Principal 获取数据?