当前位置:编程学习 > wap >>

TextSwitcher问题

今天学习了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
android --------------------编程问答--------------------

public class MainActivity extends Activity implements ViewFactory {

    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.activity_main);

textSwitcher = (TextSwitcher)findViewById(R.id.textSwitcher);
        textSwitcherBtn = (Button)findViewById(R.id.textSwitcherBtn);
         
        textSwitcher.setFactory(this);
        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("当前时间为:");
            }
        });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public View makeView() {
// TODO Auto-generated method stub

TextView view = new TextView(this);
view.setTextSize(15);    
view.setTextColor(Color.BLACK);   

return view;
}
}


这样就可以了,估计是这样的你setFactory那个,不能够再new一个,而要用跟你textSwitcher同一个对象,就是this哈~ --------------------编程问答-------------------- TextSwitcher的部分源码和ViewSwitcher的。你看下能不能理解!



public void setText(CharSequence text) {
        final TextView t = (TextView) getNextView();
        t.setText(text);
        showNext();
    }

 public View getNextView() {
        int which = mWhichChild == 0 ? 1 : 0;
        return getChildAt(which);
    }

private View obtainView() {
        View child = mFactory.makeView();
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp == null) {
            lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }
        addView(child, lp);
        return child;
    }

    /**
     * Sets the factory used to create the two views between which the
     * ViewSwitcher will flip. Instead of using a factory, you can call
     * {@link #addView(android.view.View, int, android.view.ViewGroup.LayoutParams)}
     * twice.
     *
     * @param factory the view factory used to generate the switcher's content
     */
    public void setFactory(ViewFactory factory) {
        mFactory = factory;
        obtainView();
        obtainView();
    }



补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,