首页 > 解决方案 > 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;
    }
}

结果应该是这样的:

在此处输入图像描述

标签: javaandroidbuttonontouchlistener

解决方案


我认为您应该在布局视图上设置一个触摸侦听器,并尝试获取 X 和 Y 并检测触摸点的 X 和 Y 是否在您的按钮上。

看看从其他视图拖过来时检测视图上的触摸事件


推荐阅读