【发布时间】:2020-02-03 21:00:08
【问题描述】:
基本上我有一个 TextView 需要从右侧移动到另一侧(我正在为不同的项目测试一些东西)。相反,一切都还在。什么都没有执行。
要指出的另一件事:如果我删除了 if 语句,这在我的情况下是必要的,那么一切正常。我认为条件是错误的,所以我放了一个 else 语句。同样的事情发生了——什么都没有。我测试了条件是否为假——它是真的。所以 if 语句中的代码应该已经被执行了。
一切对我来说似乎都是合乎逻辑的,但我可能是错的。你可以帮帮我吗?我能改变什么?你对我的问题有不同的方法吗?谢谢!
P.S:问题在于没有为 TextView 设置动画。问题是时间延迟。
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#000000"
android:text="0"
android:textColor="#000000" />
</RelativeLayout>
Java:
package com.nicksoft.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
int ind = -1;
private Handler h = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r.run();
}
private Runnable r = new Runnable() {
@Override
public void run() {
if(findViewById(R.id.textView).getX()>0) {
findViewById(R.id.textView).setTranslationX(ind);
ind--;
h.postDelayed(r,5);
}
}
};
}
【问题讨论】:
-
正如您在 XML 文件中看到的那样,我在右侧有一个 TextView,它需要每 5ms 向左移动 1px 直到它到达屏幕的左侧。