2017-02-13 20 views
0

私はをImageViewに移動したいですが、私はGoogle Developerページにあるコードを使用しても移動できません。 Androidの画面にどこにドラッグ&ドロップするとTextViewどこでもドラッグ&ドロップTextView

+4

あなたが既に試したことのコードを追加する - あなたがエラーを受け取ったTextViewを移動しようとしている場合でも、あなたの質問にコミュニティを有効にするためにそれらを含めてください。良い質問などのヒントについては、[StackOverflowのクイックツアー](http://stackoverflow.com/help)をご覧ください。 – ishmaelMakitla

+1

Googleデベロッパーページのコードは機能しています。あなたのコードを見て、別のことを説明してください。 – Entreco

+0

いいえ動作していません..エラーが含まれています –

答えて

0

XMLファイル:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linear" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    <TextView 
      android:id="@+id/tv1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="Test1" /> 


     <TextView 
      android:id="@+id/tv2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="Test2" /> 
    </RelativeLayout> 

Javaファイル

public class TestActivity extends Activity { 

     int pressed_x,pressed_y,pressed_x1,pressed_y1; 
     TextView tv1,tv2; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_test); 

      tv1 = (TextView) findViewById(R.id.tv1); 
      tv2 = (TextView) findViewById(R.id.tv2); 

      tv1.setOnTouchListener(mOnTouchListenerTv1); 
      tv2.setOnTouchListener(mOnTouchListenerTv2); 
     } 

     public final View.OnTouchListener mOnTouchListenerTv1 = new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       RelativeLayout.LayoutParams relativeLayoutParams = (RelativeLayout.LayoutParams) tv1.getLayoutParams(); 


       switch (event.getActionMasked()) { 
        case MotionEvent.ACTION_DOWN: 
         Log.d("TAG","@@@@ TV1 ACTION_UP"); 
         // Where the user started the drag 
         pressed_x = (int) event.getRawX(); 
         pressed_y = (int) event.getRawY(); 
         break; 

        case MotionEvent.ACTION_MOVE: 
         Log.d("TAG","@@@@ TV1 ACTION_UP"); 
         // Where the user's finger is during the drag 
         final int x = (int) event.getRawX(); 
         final int y = (int) event.getRawY(); 

         // Calculate change in x and change in y 
         int dx = x - pressed_x; 
         int dy = y - pressed_y; 

         // Update the margins 
         relativeLayoutParams.leftMargin += dx; 
         relativeLayoutParams.topMargin += dy; 
         tv1.setLayoutParams(relativeLayoutParams); 

         // Save where the user's finger was for the next ACTION_MOVE 
         pressed_x = x; 
         pressed_y = y; 
         break; 

        case MotionEvent.ACTION_UP: 
         Log.d("TAG","@@@@ TV1 ACTION_UP"); 

         break; 
       } 

       return true; 
      } 
     }; 
     public final View.OnTouchListener mOnTouchListenerTv2 = new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       RelativeLayout.LayoutParams relativeLayoutParams1 = (RelativeLayout.LayoutParams) tv2.getLayoutParams(); 

       switch (event.getActionMasked()) { 
        case MotionEvent.ACTION_DOWN: 
         Log.d("TAG","@@@@ TV2 ACTION_DOWN"); 
         // Where the user started the drag 
         pressed_x1 = (int) event.getRawX(); 
         pressed_y1 = (int) event.getRawY(); 
         break; 

        case MotionEvent.ACTION_MOVE: 
         Log.d("TAG","@@@@ TV2 ACTION_MOVE"); 
         // Where the user's finger is during the drag 
         final int x = (int) event.getRawX(); 
         final int y = (int) event.getRawY(); 

         // Calculate change in x and change in y 
         int dx = x - pressed_x1; 
         int dy = y - pressed_y1; 

         // Update the margins 
         relativeLayoutParams1.leftMargin += dx; 
         relativeLayoutParams1.topMargin += dy; 
         tv2.setLayoutParams(relativeLayoutParams1); 

         // Save where the user's finger was for the next ACTION_MOVE 
         pressed_x1 = x; 
         pressed_y1 = y; 
         break; 

        case MotionEvent.ACTION_UP: 
         Log.d("TAG","@@@@ TV2 ACTION_UP"); 
         break; 
       } 

       return true; 
      } 
     }; 
    } 
+0

TextViewを単独で使用しても動作しません。 tv2に触れてみると、tv1は動いていますが、tv2を動かそうとしています)。 –

+0

更新されたコードを追加 – user2025187

+0

以下に私のコードを追加しました –

0

XMLコード:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linear" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
    <TextView 
    android:id="@+id/tv1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:text="Test1" /> 
    <TextView 
    android:id="@+id/tv2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:layout_centerInParent="true" 
    android:text="Test2" /> 
</RelativeLayout> 

Javaコード:

public class MainActivity extends Activity { 

int pressed_x,pressed_y,pressed_x1,pressed_y1; 
TextView tv1,tv2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tv1 = (TextView) findViewById(R.id.tv1); 
    tv2 = (TextView) findViewById(R.id.tv2); 

    tv1.setOnTouchListener(mOnTouchListenerTv1); 
    tv2.setOnTouchListener(mOnTouchListenerTv2); 
} 

public final View.OnTouchListener mOnTouchListenerTv1 = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     RelativeLayout.LayoutParams relativeLayoutParams = (RelativeLayout.LayoutParams) tv1.getLayoutParams(); 


     switch (event.getActionMasked()) { 
      case MotionEvent.ACTION_DOWN: 
       Log.d("TAG","@@@@ TV1 ACTION_UP"); 
       // Where the user started the drag 
       pressed_x = (int) event.getRawX(); 
       pressed_y = (int) event.getRawY(); 
       break; 

      case MotionEvent.ACTION_MOVE: 
       Log.d("TAG","@@@@ TV1 ACTION_UP"); 
       // Where the user's finger is during the drag 
       final int x = (int) event.getRawX(); 
       final int y = (int) event.getRawY(); 

       // Calculate change in x and change in y 
       int dx = x - pressed_x; 
       int dy = y - pressed_y; 

       // Update the margins 
       relativeLayoutParams.leftMargin += dx; 
       relativeLayoutParams.topMargin += dy; 
       tv1.setLayoutParams(relativeLayoutParams); 

       // Save where the user's finger was for the next ACTION_MOVE 
       pressed_x = x; 
       pressed_y = y; 
       break; 

      case MotionEvent.ACTION_UP: 
       Log.d("TAG","@@@@ TV1 ACTION_UP"); 

       break; 
     } 

     return true; 
    } 
}; 
public final View.OnTouchListener mOnTouchListenerTv2 = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     RelativeLayout.LayoutParams relativeLayoutParams1 = (RelativeLayout.LayoutParams) tv2.getLayoutParams(); 

     switch (event.getActionMasked()) { 
      case MotionEvent.ACTION_DOWN: 
       Log.d("TAG","@@@@ TV2 ACTION_DOWN"); 
       // Where the user started the drag 
       pressed_x1 = (int) event.getRawX(); 
       pressed_y1 = (int) event.getRawY(); 
       break; 

      case MotionEvent.ACTION_MOVE: 
       Log.d("TAG","@@@@ TV2 ACTION_MOVE"); 
       // Where the user's finger is during the drag 
       final int x = (int) event.getRawX(); 
       final int y = (int) event.getRawY(); 

       // Calculate change in x and change in y 
       int dx = x - pressed_x1; 
       int dy = y - pressed_y1; 

       // Update the margins 
       relativeLayoutParams1.leftMargin += dx; 
       relativeLayoutParams1.topMargin += dy; 
       tv2.setLayoutParams(relativeLayoutParams1); 

       // Save where the user's finger was for the next ACTION_MOVE 
       pressed_x1 = x; 
       pressed_y1 = y; 
       break; 

      case MotionEvent.ACTION_UP: 
       Log.d("TAG","@@@@ TV2 ACTION_UP"); 
       break; 
     } 

     return true; 
    } 
}; 

}

関連する問題