Android開發(fā)實現(xiàn)自動切換文字TextSwitcher功能示例
本文實例講述了Android開發(fā)實現(xiàn)自動切換文字TextSwitcher功能。分享給大家供大家參考,具體如下:
介紹:
1.TextSwitcher是ViewSwicher的一個子類,繼承了ViewSwicher的所有方法
2.與ViewSwitcher的另一個子類類似,TextSwitcher也有
3.ImageSwitcher不同的是:TextSwitcher的ViewFactory方法的 makeVieW()
必須放回一個TextXiew組件.
具體效果:
放射思維:
如果將其和輪播圖(http://www.dbjr.com.cn/article/158149.htm)結(jié)合 就可以實現(xiàn)帶文字效果的輪播圖。
這里先給出布局文件:
<?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 )]); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android判斷json格式將錯誤信息提交給服務(wù)器
- android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案
- Android開發(fā)實現(xiàn)的文本折疊點擊展開功能示例
- Android開發(fā)之SeekBar基本使用及各種美觀樣式示例
- Android開發(fā)之自定義星星評分控件RatingBar用法示例
- Android開發(fā)使用ProgressBar實現(xiàn)進度條功能示例
- Android開發(fā)之計算器GridLayout布局實現(xiàn)方法示例
- Android開發(fā)之ImageSwitcher相冊功能實例分析
- Android開發(fā)之圖片壓縮實現(xiàn)方法分析
- android關(guān)于按鈕點擊效果實現(xiàn)的方法
相關(guān)文章
聊聊GridView實現(xiàn)拖拽排序及數(shù)據(jù)交互的問題
這篇文章主要介紹了聊聊GridView實現(xiàn)拖拽排序及數(shù)據(jù)交互的問題,整體實現(xiàn)思路是通過在一個容器里放置兩個dragview,DragView里面進行View的動態(tài)交換以及數(shù)據(jù)交換,具體實現(xiàn)代碼跟隨小編一起看看吧2021-11-11Android遠(yuǎn)程服務(wù)編寫和調(diào)用教程
這篇文章主要介紹了Android遠(yuǎn)程服務(wù)編寫和調(diào)用教程,本文教大家如何編寫或者調(diào)用Android的遠(yuǎn)程服務(wù),感興趣的小伙伴們可以參考一下2016-02-02Android程序開發(fā)之Fragment實現(xiàn)底部導(dǎo)航欄實例代碼
流行的應(yīng)用的導(dǎo)航一般分為兩種,一種是底部導(dǎo)航,一種是側(cè)邊欄。本文給大家介紹Fragment實現(xiàn)底部導(dǎo)航欄,對Fragment實現(xiàn)底部導(dǎo)航欄相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-03-03Android SDK命令行工具Monkey參數(shù)及使用解析
這篇文章主要介紹了Android SDK命令行工具Monkey參,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值數(shù)及使用解析,需要的朋友可以參考下2020-10-10Android編程實現(xiàn)設(shè)置TabHost當(dāng)中字體的方法
這篇文章主要介紹了Android編程實現(xiàn)設(shè)置TabHost當(dāng)中字體的方法,涉及Android針對TabHost屬性操作的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2015-12-12Android制作微信app頂部menu菜單(ActionBar)
這篇文章主要為大家詳細(xì)介紹了Android利用ActionBar制作微信app頂部menu菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02基于Android開發(fā)支持表情的實現(xiàn)詳解
本篇文章是對在Android開發(fā)中支持表情的實現(xiàn)代碼進行了介紹。需要的朋友參考下2013-05-05