[英]Moving Textview Using Buttons Android
What i'm trying to do is when you click any of the arrow buttons shown in the image, the TextView user_text will move a small amount in the respective direction. 我想做的是,当您单击图像中显示的任何箭头按钮时,TextView user_text将在各自的方向上移动少量。 For some reason using the Java code below when I click on the buttons, the text moves all the way to the absolute edge of the screen.
由于某些原因,当我单击按钮时,使用下面的Java代码,文本将一直移动到屏幕的绝对边缘。 I wasn't sure why this was happening so I decided to try and use
user_text.getX() and user_text.getY()
to get their X and Y coordinates but that didn't do anything. 我不确定为什么会这样,所以我决定尝试使用
user_text.getX() and user_text.getY()
来获取它们的X和Y坐标,但是那什么也没做。 After a little research, I found that I should've been using user_text.getLeft() and user_text.getTop() and/or user_text.getWidth()
. 经过一些研究,我发现我应该一直使用
user_text.getLeft() and user_text.getTop() and/or user_text.getWidth()
。 After trying those, the android monitor/debug section of the studio said that those numbers were 0. I'm not sure why this happened but if someone could tell me what I should be doing instead or how to get it so that every time you click the button it moves, that would be greatly appreciated. 在尝试了这些之后,工作室的android monitor / debug部分说这些数字为0。我不确定为什么会发生这种情况,但是如果有人可以告诉我我应该怎么做或者如何获得它,以便每次您单击它移动的按钮,将不胜感激。
Thanks 谢谢
PS I wrote on the image so you would know what is being ID-ed as user_text
PS我写在图像上,所以您会知道正在以
user_text
身份进行user_text
My relevant XML Code: 我相关的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>
My relevant Java Code: 我相关的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);
}
Get current X and Y of user_text
in click function. 在click函数中获取
user_text
当前X和Y。
public void moveLeft(View view)
{
user_text.setX(user_text.getX() - 10);
}
when move up: Y = Y - delta. 当向上移动时:Y = Y-增量。
public void moveUp(View view)
{
user_text.setY(user_text.getY() - 10);
}
when move down: Y = Y + delta 向下移动时:Y = Y +增量
public void moveDown(View view)
{
user_text.setY(user_text.getY() + 10);
}
This is example in MainActivity.java. 这是MainActivity.java中的示例。 I see it works
我看到了
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.