温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用中怎么将文字逐字显示

发布时间:2020-12-05 16:32:52 来源:亿速云 阅读:113 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关Android应用中怎么将文字逐字显示,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

可以采用自定义TextView的方式去实现,也可才用定时更新文字显示,思路是让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) {//如果还有汉字,那么继续开启线程,相当于递归的感觉
              startTv(nn);
            }

          } catch (InterruptedException e) {
            e.printStackTrace();
          }


        }
      }

  ).start();


}

完整代码如下:

1.Activity

public class TiaoZiActivity extends Activity {

  private TextView textView;
 
  private String s = "天生我才必有用,千金散盡還福來--李白\n你挑著但,我騎著馬--唐僧\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);//调用构造方法,直接开启

  }

  @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;//间隔时间
    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) {//如果还有汉字,那么继续开启线程,相当于递归的感觉
                startTv(nn);
              }

            } catch (InterruptedException e) {
              e.printStackTrace();
            }


          }
        }

    ).start();


  }


}

3.布局文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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>

看完上述内容,你们对Android应用中怎么将文字逐字显示有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI