Android實(shí)現(xiàn)文字逐字顯示出來
先上Android實(shí)現(xiàn)文字逐字顯示出來效果圖,供大家參考,具體內(nèi)容如下

可以采用自定義TextView的方式去實(shí)現(xiàn),也可才用定時(shí)更新文字顯示,思路是讓TextView每隔一秒顯示以一個字符串(并非每一秒多出來一個漢字),那么就簡單了,可以開啟一個線程,那么線程主要方法如下:
public static 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) {//如果還有漢字,那么繼續(xù)開啟線程,相當(dāng)于遞歸的感覺
startTv(nn);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
}
完整代碼如下:
1.Activity
public class TiaoZiActivity extends Activity {
private TextView textView;
private String s = "天生我才必有用,千金散盡還福來--李白\n你挑著但,我騎著馬--唐僧\n年后打藍(lán)思科技卡死了減肥的 kjdsfkjsjkdsfj kjdflskjklfjsljdflsjkldfjsljdflsjdfkl";;
private TiaoZiUtil tiaoziUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiaozi);
textView = ((TextView) findViewById(R.id.tv_text));
tiaoziUtil = new TiaoZiUtil(textView, s, 100);//調(diào)用構(gòu)造方法,直接開啟
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
2.工具類
public class TiaoZiUtil {
private static TextView tv;
private static String s;
private static int length;
private static long time;
static int n = 0;
private static int nn;
public TiaoZiUtil(TextView tv, String s, long time) {
this.tv = tv;//textview
this.s = s;//字符串
this.time = time;//間隔時(shí)間
this.length = s.length();
startTv(n);//開啟線程
}
public static 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) {//如果還有漢字,那么繼續(xù)開啟線程,相當(dāng)于遞歸的感覺
startTv(nn);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
}
}
3.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="200dp" />
<TextView
android:id="@+id/mytext"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android webview獲取html代碼和根據(jù)id獲取value實(shí)例
這篇文章主要介紹了android webview獲取html代碼和根據(jù)id獲取value實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
android實(shí)現(xiàn)通知欄下載更新app示例
這篇文章主要介紹了android實(shí)現(xiàn)通知欄下載更新app示例,需要的朋友可以參考下2014-03-03
Android 動態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的實(shí)例代碼
在上次android開發(fā)的項(xiàng)目中遇到個這樣的需求,要動態(tài)改變seekbar進(jìn)度條顏色與滑塊顏色的需求,實(shí)現(xiàn)代碼也算比較簡單,對實(shí)現(xiàn)過程感興趣的朋友可以通過本文學(xué)習(xí)下2016-11-11
Android抓取CSDN首頁極客頭條內(nèi)容完整實(shí)例
這篇文章主要介紹了Android抓取CSDN首頁極客頭條內(nèi)容完整實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
新版Android Studio3.6找不到R.java怎么處理
這篇文章主要介紹了新版Android Studio3.6找不到R.java怎么處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android 使用自定義RecyclerView控件實(shí)現(xiàn)Gallery效果
這篇文章主要介紹了Android 使用自定義RecyclerView 實(shí)現(xiàn)Gallery效果,本文給大家簡單介紹了RecyclerView的基本用法,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-10-10
Android開發(fā)之串口編程原理和實(shí)現(xiàn)方式
提到串口編程,就不得不提到JNI,不得不提到JavaAPI中的文件描述符類:FileDescriptor;下面我分別對JNI、FileDescriptor以及串口的一些知識點(diǎn)和實(shí)現(xiàn)的源碼進(jìn)行分析說明,感興趣的朋友可以了解下2013-01-01
Android自定義view實(shí)現(xiàn)半圓環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)半圓環(huán)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android MPAndroidChart開源庫圖表之折線圖的實(shí)例代碼
這篇文章主要介紹了Android MPAndroidChart開源庫圖表之折線圖的實(shí)例代碼,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

