2017-10-21 8 views
0

私がしようとしているのは、画像に表示されている矢印ボタンのいずれかをクリックすると、TextViewのuser_textがそれぞれの方向に少々移動します。何らかの理由で、ボタンをクリックしたときに以下のJavaコードを使用すると、テキストが画面の絶対的な端に移動します。私はなぜこれが起こっていたのか分からなかったので、私はuser_text.getX() and user_text.getY()を使ってX座標とY座標を得ることにしましたが、何もしませんでした。ちょっとした研究の末、user_text.getLeft() and user_text.getTop() and/or user_text.getWidth()を使っていたはずです。それらを試した後、スタジオのアンドロイドモニタ/デバッグセクションでは、それらの数字は0であると言いました。私はなぜこれが起こったのか分かりませんが、誰かが私が何をすべきか、それが動くボタンをクリックすると、それは非常に高く評価されるでしょう。ボタンを使用してTextViewを移動するAndroid

ありがとう

P.S.あなたがいるID-EDが何であるかを知っているので、私は、画像の上に書いたようuser_text

reference image

私の関連するXMLコード:

<TextView 
    android:id="@+id/user_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:textColor="@android:color/black" 
    android:textSize="18sp" 
    android:elevation="20dp" 
    android:paddingBottom="120dp" /> 
    . <--- that is supposed to represent that there's other non-relevant code in between 
    . 
    . 

<RelativeLayout 
    android:id="@+id/move_Group" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="5dp" 
    android:layout_centerInParent="true" 
    android:visibility="visible" 
    android:gravity="center"> 

    <Button 
     android:id="@+id/move_left" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:background="@mipmap/arrow" 
     android:rotation="-90" 
     android:layout_marginRight="20dp" 
     android:onClick="moveLeft"/> 

    <Button 
     android:id="@+id/move_up" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/move_left" 
     android:background="@mipmap/arrow" 
     android:layout_marginRight="20dp" 
     android:onClick="moveUp"/> 

    <Button 
     android:id="@+id/move_down" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/move_up" 
     android:background="@mipmap/arrow" 
     android:rotation="180" 
     android:layout_marginRight="20dp" 
     android:onClick="moveDown"/> 

    <Button 
     android:id="@+id/move_right" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/move_down" 
     android:background="@mipmap/arrow" 
     android:rotation="90" 
     android:layout_marginRight="20dp" 
     android:onClick="moveRight"/> 

    <TextView 
     android:id="@+id/move" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="MOVE" 
     android:textSize="20sp" 
     android:layout_toRightOf="@id/move_right" 
     android:textColor="@android:color/black"/> 

</RelativeLayout> 

私の関連するJavaコード:

TextView user_text = (TextView) findViewById(R.id.user_text); 
. 
. 
. 
public void moveLeft(View view) 
{ 
    user_text.setX(userX + 10); 
} 

public void moveUp(View view) 
{ 
    user_text.setY(userY + 10); 
} 

public void moveDown(View view) 
{ 
    user_text.setY(userY - 10); 
} 

public void moveRight(View view) 
{ 
    user_text.setX(userX - 10); 
} 

答えて

2

クリック機能で現在のXとYのuser_textを取得します。

public void moveLeft(View view) 
{ 
     user_text.setX(user_text.getX() - 10); 
} 

移動した場合:Y = Y - デルタ。

public void moveUp(View view) 
{ 
     user_text.setY(user_text.getY() - 10); 
} 

下降:Y = Y +デルタを

public void moveDown(View view) 
{ 
    user_text.setY(user_text.getY() + 10); 
} 

これはMainActivity.java例です。私はそれが動作参照してください

public class MainActivity extends AppCompatActivity { 

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

     user_text = (TextView) findViewById(R.id.user_text); 

    } 

    public void moveLeft(View view) 
    { 
     user_text.setX(user_text.getX() - 10); 
    } 

    public void moveUp(View view) 
    { 
     user_text.setY(user_text.getY() - 10); 

    } 

    public void moveDown(View view) 
    { 
     user_text.setY(user_text.getY() + 10); 

    } 

    public void moveRight(View view) 
    { 
     user_text.setX(user_text.getX() + 10); 
    } 
} 
+0

いくつかの説明を残してください – tan

関連する問題