2017-07-09 6 views
0

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> 
+0

post Ihre XML-Layout –

+0

in Ordnung, ich habe es hinzugefügt. –

+0

was meinst du mit "scrolling und flinging gesten funktionieren überhaupt nicht". Was ist das genaue Problem? –

Antwort

0

Für erstes Problem (falscher Text ausgerichtet)

Sie sollten Ihre Textview Breite wie match_parent und Schwerkraft center_horizontal gesetzt. So sollte Ihr Code in etwa so aussehen:

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="160dp" 
     android:text="@string/initial_text" 
     android:textSize="30sp" 
     android:layout_gravity="center_horizontal" 
     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" /> 
+0

Vielen Dank! Erledigt. Kannst du auch die zweite überprüfen? –

+0

@ AhmetEmreBoyacı ja, erklären Sie besser bitte, was Sie tun möchten (Sie wollen einen Bildlauf Bildschirm?) Und was ist falsch (welche Ausnahme/Problem passieren) –

+0

ok, die Sache ist, ich versuche nur zu implementieren scrollen, schleudern und tippen Sie doppelt auf eine Art von Gesten (die Methoden in GestureDetector.OnGestureListener und GestureDetector.OnDoubleTapListener, um genau zu sein) und die TextView sollte etwas wie "Scrolling" sagen, wenn es ein Scroll-Ereignis abfängt, und "Schleudern", wenn es ein Schleudern fängt Veranstaltung usw. –

Verwandte Themen