Код: Выделить всё
package com.exambple.vgg;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements
ViewSwitcher.ViewFactory, ViewFactory {
private TextSwitcher mSwitcher;
private int mCounter = 0;
private TextSwitcher textSwitcher;
private Button b;
String myContent[]={"A","B","C","D"};
//used to keep track of text index
int currentIndex=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textSwitcher = (TextSwitcher)findViewById(R.id.textSwitcher);
b= (Button)findViewById(R.id.btn1);
//set ViewFactory of TextSwitcher to dynamically create TextView
textSwitcher.setFactory(new ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(42);
myText.setTextColor(Color.RED);
return myText;
}
});
// Declare the in and out animations
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set the animation of textSwitcher
textSwitcher.setInAnimation(in);
textSwitcher.setOutAnimation(out);
//button click
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if(currentIndex==myContent.length)
currentIndex=0;
textSwitcher.setText(myContent[currentIndex]);
}
});
}
@Override
public View makeView() {
// TODO Auto-generated method stub
return null;
}
}