怎么在Android中使用TextSwitcher實現(xiàn)自動切換文字功能

怎么在Android中使用TextSwitcher實現(xiàn)自動切換文字功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)是專業(yè)的北海網(wǎng)站建設(shè)公司,北海接單;提供網(wǎng)站設(shè)計制作、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行北海網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

布局文件:

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_horizontal">
    <!--定義一個ViewSwitcher并且制定了文本切換時的動畫效果-->
    <TextSwitcher
      android:id="@+id/textSwitcher"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inAnimation="@android:anim/slide_in_left"
      android:outAnimation="@android:anim/slide_out_right"
      android:onClick="next">
    </TextSwitcher>
</RelativeLayout>

關(guān)于文字定時切換的實現(xiàn):

1.首先寫一個next方法,再這個歌方法中調(diào)用父類的setText()方法 實現(xiàn)了文字的設(shè)定

2.再主線程中開設(shè)一個性的線程用于圖片的切換 注意:線程中不能直接改變View,所以要發(fā)送小修再Handler對象中改變布局內(nèi)容(文字)

實現(xiàn)如下:

public class MainActivity extends Activity {
  String[] string = new String[]{
      "我愛高數(shù)",
      "我愛概率論",
      "我愛計算機網(wǎng)絡(luò)",
      "我愛操作系統(tǒng)"
  };
  TextSwitcher textSwitcher;
  int curStr ;
  Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      next(null);
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
    textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
      @Override
      public View makeView() {
        TextView textView = new TextView(MainActivity.this);
        textView.setTextSize(40);
        textView.setTextColor(Color.RED);
        return textView;
      }
    });
    new Thread(){
      @Override
      public void run() {
        while (true){
          Message message = handler.obtainMessage();
          message.obj = 0;
          handler.sendMessage(message);
          try {
            sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }.start();
  }
  private void next(View scource){
    textSwitcher.setText(string[curStr = ( curStr++ % string.length )]);
  }
}

看完上述內(nèi)容,你們掌握怎么在Android中使用TextSwitcher實現(xiàn)自動切換文字功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

文章標(biāo)題:怎么在Android中使用TextSwitcher實現(xiàn)自動切換文字功能
網(wǎng)頁路徑:http://bm7419.com/article46/igdieg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站收錄、關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航、微信公眾號、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化