2017-10-21 115 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()。在嘗試完這些之後,工作室的android監視器/調試部分表示這些數字爲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

在點擊功能中獲取user_text的當前X和Y.

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

當向上移動時:Y = Y - delta。

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