TextSwitcher问题
今天学习了android的TextSwitcher,写了一个Demo。可是有点错误就是找不出来,想请高手帮忙解决
XML文件
java文件
问题在于textSwitcher.setText("当前时间为:"); ”当前时间为:“显示不出来,但是Button可以显示
还有意外的发现一个小问题,百思不得其解
就是当我把XML文件里的Button放在上面,将TextSwitcher放在下面的时候居然有异常
11-04 14:15:36.569: ERROR/AndroidRuntime(516): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cfc.widget/com.cfc.widget.TextSwitcherActivity}: java.lang.ClassCastException: android.widget.Button
XML文件
<?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" >
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/textSwitcherBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示当前时间"/>
</LinearLayout>
java文件
package com.cfc.widget;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
public class TextSwitcherActivity extends Activity
{
private static final String TAG = "TextSwitcherActivity";
private TextSwitcher textSwitcher = null;
private Button textSwitcherBtn = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.text_switcher_activity);
textSwitcher = (TextSwitcher)findViewById(R.id.textSwitcher);
textSwitcherBtn = (Button)findViewById(R.id.textSwitcherBtn);
textSwitcher.setFactory(new MyViewFactory());
textSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
textSwitcherBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
textSwitcher.setText("当前时间为:");
}
});
}
private class MyViewFactory implements ViewFactory
{
@Override
public View makeView()
{
Log.i(TAG, "makeView");
TextView text = new TextView(TextSwitcherActivity.this);
text.setTextColor(0x00FFFF);
text.setTextSize(20);
text.setLayoutParams(new TextSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return text;
}
}
}
问题在于textSwitcher.setText("当前时间为:"); ”当前时间为:“显示不出来,但是Button可以显示
还有意外的发现一个小问题,百思不得其解
就是当我把XML文件里的Button放在上面,将TextSwitcher放在下面的时候居然有异常
11-04 14:15:36.569: ERROR/AndroidRuntime(516): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cfc.widget/com.cfc.widget.TextSwitcherActivity}: java.lang.ClassCastException: android.widget.Button
...全文
相关推荐
textswitcher的官方demo
这样做的好处是可以为每个文本切换创建独立的`TextView`,避免了对单个`TextView`进行反复修改导致的性能问题。
TextSwitcher垂直滚动文字广告demo
- 可以通过XML定义动画资源文件,然后在代码中加载并应用到TextSwitcher上。5. **注意事项**: - 要注意处理好线程同步问题,避免在主线程中进行耗时操作,影响UI的流畅性。
Android开发实现自动切换文字TextSwitcher功能示例
由于线程安全问题,不能直接在新线程中修改UI,所以我们需要使用Handler来在主线程中执行切换操作。
Android 上下滚动TextSwitcher实例详解
此外,Handler和Thread的使用需要谨慎,避免内存泄漏和其他潜在问题。对于更复杂的滚动效果,可以考虑使用ViewPager配合Adapter实现,或者使用第三方库如Android Flip等。
Android-仿照Google在卡片上滚动显示最新评论的TextSwitcher
在实际项目中,你可能还需要考虑线程安全、错误处理、用户体验优化等问题,比如添加加载动画、错误提示等。此外,为了更好地适应不同的设备和屏幕尺寸,可能需要对布局和文本进行适当的调整。


