Android开发实现自动切换文字TextSwitcher功能示例

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android开发实现自动切换文字TextSwitcher功能示例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了AndROId开发实现自动切换文字TextSwITcher功能。分享给大家供大家参考,具体如下:

介绍:

1.TextSwitcher是ViewSwicher的一个子类,继承了ViewSwicher的所有方法

2.与ViewSwitcher的另一个子类类似,TextSwitcher也有

3.ImageSwitcher不同的是:TextSwitcher的ViewFactory方法的 makeVieW() 必须放回一个TextXiew组件.

具体效果:

Android开发实现自动切换文字TextSwitcher功能示例

放射思维

如果将其和轮播图(https://www.jb51.net/article/158149.htm)结合 就可以实现带文字效果的轮播图。

这里先给出布局文件:

 <&#63;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,所以要发送小修再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图形与图像处理技巧总结》、《@L_512_2@》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android开发实现自动切换文字TextSwitcher功能示例全部内容,希望文章能够帮你解决Android开发实现自动切换文字TextSwitcher功能示例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。