可以采用自定义TextView的方式去实现,也可才用定时更新文字显示,思路是让TextView每隔一秒显示以一个字符串(并非每一秒多出来一个汉字),那么就简单了,可以开启一个线程,实现代码如下:
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="again" />
</RelativeLayout>
2.Activity(需要逐字显示的字)
package com.example.textjiange;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class tiaozi extends AppCompatActivity {
private TextView tv;
private String s;
private int length;
int n = 0;
private int nn;
private JiangeUtil jiangeUtil;
private View bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = ((TextView) findViewById(R.id.tv));
bt = findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n = 0;
jiangeUtil.startTv(n);
}
});
s = "这里是注水井数据";
jiangeUtil = new JiangeUtil(tv, s, 100);//调用构造方法,直接开启
}
}
3. Activity(工具类)
package com.example.textjiange;
import android.widget.TextView;
public class JiangeUtil {
private TextView tv;
private String s;
private int length;
private long time;
static int n = 0;
private int nn;
public JiangeUtil(TextView tv, String s, long time) {
this.tv = tv;//textview
this.s = s;//字符串
this.time = time;//间隔时间
this.length = s.length();
startTv(n);//开启线程
}
public void startTv(final int n) {
new Thread(
new Runnable() {
@Override
public void run() {
try {
final String stv = s.substring(0, n);//截取要填充的字符串
tv.post(new Runnable() {
@Override
public void run() {
tv.setText(stv);
}
});
Thread.sleep(time);//休息片刻
nn = n + 1;//n+1;多截取一个
if (nn <= length) {//如果还有汉子,那么继续开启线程,相当于递归的感觉
startTv(nn);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
}
}
这样就可以实现逐字跳动了,自己可以在android studio运行一下ha!