final String strTextSwitcher[] = {"Text Switcher 1", "Text Switcher 2", "Text Switcher 3", "Text Switcher 4", "Text Switcher 5"};
final int currentIndex = -1;
final TextSwitcher textSwitcher = new TextSwitcher(MainActivity.this);
textSwitcher.setLayoutParams(new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
linear1.addView(textSwitcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
TextView t = new TextView(MainActivity.this);
t.setGravity(Gravity.TOP | Gravity.CENTER);
t.setTextSize(36);
return t;
}
});
textSwitcher.setCurrentText("click Button");
//button prev
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (currentIndex>0)
currentIndex = currentIndex-1;
textSwitcher.setText(strTextSwitcher[currentIndex]);
}
});
//button next
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (currentIndex<strTextSwitcher.length-1)
currentIndex = currentIndex+1;
textSwitcher.setText(strTextSwitcher[currentIndex]);
}
});
Komentar