我希望它与触摸听器一起在字母上形成单词。但是当涉及到一封信时,它会反复写出该信。我如何一次性采取这个措施?
因此,如果将“ harf1”写为单词的字母,就不会再写。
这里是代码:
linearLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()){ case MotionEvent.ACTION_DOWN: if (isDownWord1(event.getX(),event.getY())) yazi.setText("A"); if (isDownWord2(event.getX(),event.getY())) yazi.setText("B"); if (isDownWord3(event.getX(),event.getY())) yazi.setText("C"); if (isDownWord4(event.getX(),event.getY())) yazi.setText("D"); break; case MotionEvent.ACTION_MOVE: if (isDownWord1(event.getX(),event.getY())) yazi.setText(yazi.getText().toString() + "A"); if (isDownWord2(event.getX(),event.getY())) yazi.setText(yazi.getText().toString() + "B"); if (isDownWord3(event.getX(),event.getY())) yazi.setText(yazi.getText().toString() + "C"); if (isDownWord4(event.getX(),event.getY())) yazi.setText(yazi.getText().toString() + "D"); break; case MotionEvent.ACTION_UP: yazi.setText(""); break; } return true; } }); } public boolean isDownWord1(float x,float y){ if(x < harf1.getRight() && x > harf1.getLeft() && y > harf1.getTop() && y < harf1.getBottom()) return true; else return false; } public boolean isDownWord2(float x,float y){ if(x < harf2.getRight() && x > harf2.getLeft() && y > harf2.getTop() && y < harf2.getBottom()) return true; else return false; } public boolean isDownWord3(float x,float y){ if(x < harf3.getRight() && x > harf3.getLeft() && y > harf3.getTop() && y < harf3.getBottom()) return true; else return false; } public boolean isDownWord4(float x,float y){ if(x < harf4.getRight() && x > harf4.getLeft() && y > harf4.getTop() && y < harf4.getBottom()) return true; else return false; }
这是XML文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:orientation="vertical"
android:background="@drawable/game_screen_bg"
tools:context=".GameLevel1">
<TextView
android:id="@+id/harf1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="93dp"
android:layout_marginLeft="93dp"
android:layout_marginTop="475dp"
android:text="A"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/harf2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="408dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:text="B"
android:textSize="50dp"
app:layout_constraintEnd_toStartOf="@+id/harf3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/harf3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="471dp"
android:layout_marginEnd="95dp"
android:layout_marginRight="95dp"
android:text="C"
android:textSize="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/harf4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="188dp"
android:layout_marginRight="188dp"
android:layout_marginBottom="114dp"
android:text="D"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/yazi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="108dp"
android:layout_marginLeft="108dp"
android:layout_marginTop="200dp"
android:text="TextView"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/xxxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#10000000"
android:orientation="horizontal" />
</androidx.constraintlayout.widget.ConstraintLayout>
我希望它与触摸听器一起在字母上形成单词。但是当涉及到一封信时,它会反复写出该信。我该如何一次性采取这项措施?因此,如果将“ harf1”写为...
在将字符添加到yazi
TextView
之前,您只需要检查yazi
的最后一个字符是否与新字符不同;因此将MotionEvent.ACTION_MOVE
的开关大小写更改为: