2017-10-21 156 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