Android開發(fā)實現自動切換文字TextSwitcher功能示例
本文實例講述了Android開發(fā)實現自動切換文字TextSwitcher功能。分享給大家供大家參考,具體如下:
介紹:
1.TextSwitcher是ViewSwicher的一個子類,繼承了ViewSwicher的所有方法
2.與ViewSwitcher的另一個子類類似,TextSwitcher也有
3.ImageSwitcher不同的是:TextSwitcher的ViewFactory方法的 makeVieW() 必須放回一個TextXiew組件.
具體效果:

放射思維:
如果將其和輪播圖(http://www.dhdzp.com/article/158149.htm)結合 就可以實現帶文字效果的輪播圖。
這里先給出布局文件:
<?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>
關于文字定時切換的實現:
1.首先寫一個next方法,再這個歌方法中調用父類的setText()方法 實現了文字的設定
2.再主線程中開設一個性的線程用于圖片的切換 注意:線程中不能直接改變View,所以要發(fā)送小修再Handler對象中改變布局內容(文字)
實現如下:
public class MainActivity extends Activity {
String[] string = new String[]{
"我愛高數",
"我愛概率論",
"我愛計算機網絡",
"我愛操作系統"
};
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 )]);
}
}
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
- Android判斷json格式將錯誤信息提交給服務器
- android和服務器的URLEncodedUtils亂碼編碼問題的解決方案
- Android開發(fā)實現的文本折疊點擊展開功能示例
- Android開發(fā)之SeekBar基本使用及各種美觀樣式示例
- Android開發(fā)之自定義星星評分控件RatingBar用法示例
- Android開發(fā)使用ProgressBar實現進度條功能示例
- Android開發(fā)之計算器GridLayout布局實現方法示例
- Android開發(fā)之ImageSwitcher相冊功能實例分析
- Android開發(fā)之圖片壓縮實現方法分析
- android關于按鈕點擊效果實現的方法
相關文章
Android開發(fā)中總結的Adapter工具類【附完整源碼下載】
這篇文章主要介紹了Android開發(fā)中總結的Adapter工具類,簡單說明了Adapter的功能,并結合實例形式分析了Adapter工具類的相關使用方法,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11
Android開發(fā)自定義TextView省略號樣式的方法
這篇文章主要介紹了Android開發(fā)自定義TextView省略號樣式的方法,結合實例形式分析了Android文本控件TextView相關屬性與字符串操作技巧,需要的朋友可以參考下2017-10-10
ubuntu下 AndroidStudio4.1啟動報錯問題的解決
這篇文章主要介紹了ubuntu下 AndroidStudio4.1啟動報錯問題的解決,本文給大家分享個人經驗對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Android編程之創(chuàng)建自己的內容提供器實現方法
這篇文章主要介紹了Android編程之創(chuàng)建自己的內容提供器實現方法,結合具體實例形式分析了Android創(chuàng)建內容提供器的原理、步驟與相關操作技巧,需要的朋友可以參考下2017-08-08
在Android中動態(tài)添加Panel框架的實現代碼
項目經常會有這種需求,就是想動態(tài)的在某個界面上添加一個Panel。比如,有一個按鈕,點擊后會顯示下拉菜單式的界面。這種需求,就屬于動態(tài)添加一個Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開發(fā),所以就有了以下內容2013-05-05

