TextSwitcher问题

cfc1243570631 cfc1243570631 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天学习了android的TextSwitcher,写了一个Demo。可是有点错误就是找不出来,想请高手帮忙解决
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
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
cfc1243570631
Android 80,490 成员 91,371 内容