2017-07-09 12 views
0

私はAndroidの開発に新しく、この問題を解決できませんでした。私はこのアプリケーションを構築するとき、私の最初のテキストは中心に置かれますが、ボタンをクリックするか、それを長くクリックすると、応答テキストは一列に並んでいます。また、ダブルタップ、スクロール、ジェスチャーの操作はまったく機能しません。理由は何でしょうか?テキストが左に揃っているのはなぜですか?

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

あなたのXMLレイアウト –

+0

を投稿しました。 –

+0

「スクロールして逃げるジェスチャーがまったく機能しない」とはどういう意味ですか?正確な問題は何ですか? –

答えて

0

(間違ったテキストが整列)

あなたはcenter_horizo​​ntalにmatch_parentと重力としてあなたのTextView幅を設定する必要があります。だからあなたのコードは次のようなものになるはずです:

<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

ありがとう!完了しました。もう1つもチェックできますか? –

+0

@AhmetEmreBoyacıはい、あなたは何をしたいですか(あなたはスクロール画面をしたいですか?)何が間違っていますか(例外/問題が起こるかどうか) –

+0

OK、問題は、 (GestureDetector.OnGestureListenerとGestureDetector.OnDoubleTapListenerのメソッドが特定のものである)、TextViewはスクロールイベントをキャッチするときには「スクロールする」、Flingをキャッチするときには「Flinging」のように言うべきですイベントなど –

関連する問題