@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);
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(); }
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哈~
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();
}