源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android实现文字逐字显示出来

  • 时间:2021-07-01 17:23 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现文字逐字显示出来
先上Android实现文字逐字显示出来效果图,供大家参考,具体内容如下 [img]http://files.jb51.net/file_images/article/201705/2017525151245087.gif?2017425151259[/img] 可以采用自定义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();


}

完整代码如下: [b]1.Activity [/b]
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();

  }
}

[b]2.工具类[/b]
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();


  }


}

[b]3.布局文件 [/b]
<?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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部