Ich bin neu in der Entwicklung von Android, und ich konnte dieses Problem einfach nicht beheben. Wenn ich diese App erstelle, ist mein anfänglicher Text zentriert, aber wenn ich auf die Schaltfläche klicke oder lange darauf klicke, bleibt der Antworttext linksbündig. Doppeltipp-, Scroll- und Schleudergesten funktionieren auch nicht. Was kann der Grund sein?Warum ist mein Text nach links ausgerichtet?
MainActivity.java:
package com.revolise.gesturehandling;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
public class MainActivity extends AppCompatActivity implements
GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener
{
private Button button1;
private GestureDetectorCompat gestureDetect;
private TextView txt1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
txt1 = (TextView)findViewById(R.id.textView1);
this.gestureDetect = new GestureDetectorCompat(this, this);
gestureDetect.setOnDoubleTapListener(this);
button1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
txt1.setText("clicked to the button whooaoaaa");
}
}
);
button1.setOnLongClickListener(
new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
txt1.setText("long clicked to the button woww");
return false;
}
}
);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
txt1.setText("Hey, you double tapped");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
txt1.setText("Aaaand now you're swiping.");
return true;
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return true;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
txt1.setText("Scrolling weee");
return true;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
txt1.setText("Long pressed");
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
txt1.setText("Whoaa flinginggg");
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.revolise.gesturehandling.MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="160dp"
android:text="@string/initial_text"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/button1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
android:layout_marginBottom="200dp" />
</android.support.constraint.ConstraintLayout>
post Ihre XML-Layout –
in Ordnung, ich habe es hinzugefügt. –
was meinst du mit "scrolling und flinging gesten funktionieren überhaupt nicht". Was ist das genaue Problem? –