下面列出了怎么用android.widget.TextSwitcher的API类实例代码及写法,或者点击链接到github查看源代码。
public void initViews() {
barWavesView = ((BarWavesView) activity.findViewById(R.id.play_bar_waves));
tvPlayProgress = (TextView) activity.findViewById(R.id.play_progress);
tvDuration = (TextView) activity.findViewById(R.id.play_duration);
sbSongProgress = (DiscreteSeekBar) activity.findViewById(R.id.play_seekBar);
tsSongName = (TextSwitcher) activity.findViewById(R.id.play_ts_song_name);
tsSongArts = (TextSwitcher) activity.findViewById(R.id.play_ts_song_arts);
btPre = (SkipView) activity.findViewById(R.id.play_pre_song);
btNext = (SkipView) activity.findViewById(R.id.play_next_song);
btPlay = (PlayView) activity.findViewById(R.id.play_song);
btPre.setOnClickListener(this);
btNext.setOnClickListener(this);
btPlay.setOnClickListener(this);
}
private void initializeTextSwitcher(@NonNull final Context context) {
selectedTextView = new TextSwitcher(getContext());
selectedTextView.setFactory(this);
selectedTextView.setAnimateFirstView(false);
selectedTextView.setBackgroundResource(mSelectedTextBackground);
if (mTextSwitcherInAnimation != 0) {
selectedTextView.setInAnimation(context, mTextSwitcherInAnimation);
}
if (mTextSwitcherOutAnimation != 0) {
selectedTextView.setOutAnimation(context, mTextSwitcherOutAnimation);
}
LayoutParams params = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
params.gravity = Gravity.CENTER;
selectedTextView.setLayoutParams(params);
selectedTextView.setLayerType(LAYER_TYPE_HARDWARE, null);
addView(selectedTextView);
}
@Override
public void setText(CharSequence text, boolean animate) {
View view = getView();
if (view == null) {
return;
}
TextSwitcher textSwitcher = (TextSwitcher) view;
if (animate) {
textSwitcher.setText(text);
} else {
TextView curr = (TextView) textSwitcher.getCurrentView();
curr.setText(text);
}
//
// waiting for the first layout of SWITCHER to be finished,
// so that we can adjust its size according to its content.
//
// 100 ms
//
schedule(MSG_TEXT_VIEW_ADJUST_HEIGHT, 100);
}
private void updateAnimation() {
View view = getView();
if (view == null) {
return;
}
ViewSwitcher viewSwitcher = (ViewSwitcher) view;
if (viewSwitcher.getInAnimation() != mInAnimation || mInAnimation == null) {
if (mInAnimation == null) {
mInAnimation = AnimationFactory.pushDownIn();
}
if (viewSwitcher instanceof TextSwitcher) {
mInAnimation.setAnimationListener(mTextViewInAnimationListener);
}
mInAnimation.setDuration(mInDuration);
viewSwitcher.setInAnimation(mInAnimation);
}
if (viewSwitcher.getOutAnimation() != mOutAnimation || mOutAnimation == null) {
if (mOutAnimation == null) {
mOutAnimation = AnimationFactory.pushDownOut();
}
mOutAnimation.setDuration(mOutDuration);
viewSwitcher.setOutAnimation(mOutAnimation);
}
}
private void adjustTextViewHeight() {
View view = getView();
if (view == null) {
return;
}
TextSwitcher textSwitcher = (TextSwitcher) view;
TextView curr = (TextView) textSwitcher.getCurrentView();
TextView next = (TextView) textSwitcher.getNextView();
int currH = curr.getLineCount() * curr.getLineHeight();
int nextH = next.getLineCount() * next.getLineHeight();
if (currH != nextH) {
curr.setHeight(currH);
next.setHeight(currH);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_switcher_1);
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
Button nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(this);
updateCounter();
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
startText = (TextView) mActivity.findViewById(R.id.start);
endText = (TextView) mActivity.findViewById(R.id.end);
nextTime = (TextSwitcher) mActivity.findViewById(R.id.next_time);
nextRoute = (TextView) mActivity.findViewById(R.id.next_route);
nextBus = (TextView) mActivity.findViewById(R.id.next_bus);
callSafeRideButton = (TextView) mActivity.findViewById(R.id.safe_ride_button);
drawer = (MultipleOrientationSlidingDrawer) mActivity.findViewById(R.id.sliding_drawer);
startTextCorrect = mActivity.getString(R.string.start);
safeRideTextCorrect = mActivity.getString(R.string.call_safe_ride);
endTextCorrect = mActivity.getString(R.string.end);
decorView = mActivity.getWindow().getDecorView();
}
CellFeedViewHolder(View view) {
super(view);
ivFeedCenter = (ImageView) view.findViewById(R.id.ivFeedCenter);
ivFeedBottom = (ImageView) view.findViewById(R.id.ivFeedBottom);
btnComments = (ImageButton) view.findViewById(R.id.btnComments);
btnLike = (ImageButton) view.findViewById(R.id.btnLike);
btnMore = (ImageButton) view.findViewById(R.id.btnMore);
tsLikesCounter = (TextSwitcher) view.findViewById(R.id.tsLikesCounter);
ivUserProfile = (ImageView) view.findViewById(R.id.ivUserProfile);
vImageRoot = (FrameLayout) view.findViewById(R.id.vImageRoot);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSendText = (EditText) findViewById(R.id.main_text_input);
mRecvText = (TextSwitcher) findViewById(R.id.main_text_recv);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSendText = (EditText) findViewById(R.id.main_text_input);
mRecvText = (TextSwitcher) findViewById(R.id.main_text_recv);
}
public CardViewHolder(CardView itemView) {
super(itemView);
chartView = (ChartView) itemView.findViewById(R.id.chart_view);
sensorSelectionArea = itemView.findViewById(R.id.sensor_selection_area);
sensorTabLayout = (TabLayout) itemView.findViewById(R.id.sensor_selector_tab_layout);
sensorSettingsGear = (ImageButton) itemView.findViewById(R.id.settings_gear);
sensorTabHolder = (ViewGroup) itemView.findViewById(R.id.sensor_selection_tab_holder);
graphStatsList = (StatsList) itemView.findViewById(R.id.stats_drawer);
header = (SensorCardHeader) itemView.findViewById(R.id.sensor_card_header);
headerText = (TextView) itemView.findViewById(R.id.sensor_card_header_title);
toggleButton = (ToggleArrow) itemView.findViewById(R.id.btn_sensor_card_toggle);
toggleButtonSpacer = itemView.findViewById(R.id.sensor_card_toggle_spacer);
menuButton = (ImageButton) itemView.findViewById(R.id.btn_sensor_card_overflow_menu);
graphViewGroup = (ViewGroup) itemView.findViewById(R.id.graph_view_content_group);
meterSensorIconContainer =
(RelativeLayout) itemView.findViewById(R.id.card_meter_sensor_icon_container);
meterLiveData = (SingleLineResizableTextView) itemView.findViewById(R.id.live_sensor_value);
statusViewGroup = (ViewGroup) itemView.findViewById(R.id.status_view_content_group);
statusProgressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar);
statusMessage = (TextView) itemView.findViewById(R.id.status_message);
statusRetryButton = (Button) itemView.findViewById(R.id.status_retry_button);
triggerSection = (RelativeLayout) itemView.findViewById(R.id.sensor_card_trigger_section);
triggerIcon = (ViewSwitcher) itemView.findViewById(R.id.trigger_icon_view_switcher);
triggerLevelDrawableButton = (ImageButton) itemView.findViewById(R.id.sensor_trigger_icon);
triggerTextSwitcher = (TextSwitcher) itemView.findViewById(R.id.trigger_text_switcher);
triggerFiredBackground =
(TriggerBackgroundView) itemView.findViewById(R.id.sensor_trigger_fired_background);
triggerFiredText = (TextView) itemView.findViewById(R.id.trigger_fired_text);
infoButton = (ImageButton) itemView.findViewById(R.id.btn_info);
WindowManager windowManager =
(WindowManager) itemView.getContext().getSystemService(Context.WINDOW_SERVICE);
screenOrientation = windowManager.getDefaultDisplay().getRotation();
}
@Override protected void onFinishInflate() {
super.onFinishInflate();
modifierTitle = (TextView) findViewById(R.id.bike_image_modifier_title);
textSwitcher = (TextSwitcher) findViewById(R.id.text_switcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override public View makeView() {
return LayoutInflater.from(getContext()).inflate(R.layout.modifier_value_textview, null);
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView textView = new TextView(MainActivity.this);
textView.setTextSize(18);
textView.setGravity(Gravity.CENTER);
return textView;
}
});
mTextSwitcher.setInAnimation(this, android.R.anim.fade_in);
mTextSwitcher.setOutAnimation(this, android.R.anim.fade_out);
mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
mImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
return imageView;
}
});
mImageSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
mImageSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);
onSwitch(null);
}
@Override
public void setTextSize(int size) {
View view = getView();
if (view == null) {
return;
}
TextSwitcher textSwitcher = (TextSwitcher) view;
for (int i = 0; i < 2; i++) {
TextView titleView = (TextView) textSwitcher.getChildAt(i);
titleView.setTextSize(size);
}
}
@Override
public void setTextColor(int color) {
View view = getView();
if (view == null) {
return;
}
TextSwitcher textSwitcher = (TextSwitcher) view;
for (int i = 0; i < 2; i++) {
TextView titleView = (TextView) textSwitcher.getChildAt(i);
titleView.setTextColor(color);
}
}
private void findViews(){
container = (FrameLayout) findViewById(R.id.container);
containerScrollView = (ScrollView) findViewById(R.id.containerScrollView);
innerContainer = (LinearLayout) findViewById(R.id.innerContainer);
titleSwitcher = (TextSwitcher) findViewById(R.id.titleSwitcher);
errorSwitcher = (TextSwitcher) findViewById(R.id.errorSwitcher);
detailsSwitcher = (TextSwitcher) findViewById(R.id.detailsSwitcher);
textField = (CardView) findViewById(R.id.textField);
inputSwitcher = (ViewAnimator) findViewById(R.id.inputSwitcher);
nextButton = (ImageButton) findViewById(R.id.nextButton);
progress = (ProgressBar) findViewById(R.id.progress);
stepText = (TextView) findViewById(R.id.stepText);
setProgressDrawable();
}
@SuppressLint("PrivateResource")
@Override
protected void onCreate(Bundle savedInstanceState) {
switch (Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("theme", "0"))) {
case ActivityMain.THEME_LIGHT : setTheme(R.style.AppThemeLight); break;
case ActivityMain.THEME_DARK : setTheme(R.style.AppThemeDark); break;
default: setTheme(R.style.AppThemeLight); break;
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mReplaceFragmentStrategy = new PreferenceScreenNavigationStrategy.ReplaceFragment(this, R.anim.abc_fade_in, R.anim.abc_fade_out, R.anim.abc_fade_in, R.anim.abc_fade_out);
if (savedInstanceState == null) {
mSettingsFragment = FragmentSettings.newInstance(null);
getSupportFragmentManager().beginTransaction().add(R.id.content, mSettingsFragment, "Settings").commit();
} else {
mSettingsFragment = (FragmentSettings) getSupportFragmentManager().findFragmentByTag("Settings");
}
getSupportFragmentManager().addOnBackStackChangedListener(this);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
// Cross-fading title setup.
mTitle = getTitle();
mTitleSwitcher = new TextSwitcher(mToolbar.getContext());
mTitleSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView tv = new AppCompatTextView(mToolbar.getContext());
TextViewCompat.setTextAppearance(tv, R.style.TextAppearance_AppCompat_Widget_ActionBar_Title);
return tv;
}
});
mTitleSwitcher.setCurrentText(mTitle);
if (ab != null) {
ab.setCustomView(mTitleSwitcher);
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
// Add to hierarchy before accessing layout params.
// int margin = Util.dpToPxOffset(this, 16);
// ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mTitleSwitcher.getLayoutParams();
// lp.leftMargin = margin;
// lp.rightMargin = margin;
mTitleSwitcher.setInAnimation(this, R.anim.abc_fade_in);
mTitleSwitcher.setOutAnimation(this, R.anim.abc_fade_out);
android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
((IslamicLibraryApplication) getApplication()).refreshLocale(this, false);
super.onCreate(savedInstanceState);
// Enable if you use AppCompat 24.1.x.
// Fixes.updateLayoutInflaterFactory(getLayoutInflater());
setContentView(R.layout.activity_settings);
mReplaceFragmentStrategy = new PreferenceScreenNavigationStrategy
.ReplaceFragment(this,
R.anim.abc_fade_in,
R.anim.abc_fade_out,
R.anim.abc_fade_in,
R.anim.abc_fade_out);
if (savedInstanceState == null) {
mSettingsFragment = SettingsFragment.newInstance(null);
getSupportFragmentManager().beginTransaction().add(R.id.content, mSettingsFragment, "Settings").commit();
} else {
mSettingsFragment = (SettingsFragment) getSupportFragmentManager().findFragmentByTag("Settings");
}
getSupportFragmentManager().addOnBackStackChangedListener(this);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBar ab = getSupportActionBar();
// Cross-fading title setup.
mTitle = getTitle();
mTitleSwitcher = new TextSwitcher(mToolbar.getContext());
mTitleSwitcher.setFactory(() -> {
TextView tv = new AppCompatTextView(mToolbar.getContext());
TextViewCompat.setTextAppearance(tv, R.style.TextAppearance_AppCompat_Widget_ActionBar_Title);
return tv;
});
mTitleSwitcher.setCurrentText(mTitle);
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setCustomView(mTitleSwitcher);
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
// Add to hierarchy before accessing layout params.
int margin = Util.dpToPxOffset(this, 16);
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mTitleSwitcher.getLayoutParams();
lp.leftMargin = margin;
lp.rightMargin = margin;
mTitleSwitcher.setInAnimation(this, R.anim.abc_fade_in);
mTitleSwitcher.setOutAnimation(this, R.anim.abc_fade_out);
}
private void init(Context context, AttributeSet attrs) {
mDefaultTextSize = getResources().getDimensionPixelSize(R.dimen.text_default_size);
mIsLoadingShowing = false;
LayoutInflater.from(getContext()).inflate(R.layout.view_loading_button, this, true);
mProgressBar = (ProgressBar) findViewById(R.id.pb_progress);
mTextSwitcher = (TextSwitcher) findViewById(R.id.pb_text);
if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.LoadingButton,
0, 0);
try {
float textSize = a.getDimensionPixelSize(R.styleable.LoadingButton_pbTextSize, mDefaultTextSize);
setTextSize(textSize);
String text = a.getString(R.styleable.LoadingButton_pbText);
setText(text);
mLoadingMessage = a.getString(R.styleable.LoadingButton_pbLoadingText);
if (mLoadingMessage == null) {
mLoadingMessage = getContext().getString(R.string.default_loading);
}
int progressColor = a.getColor(R.styleable.LoadingButton_pbProgressColor, DEFAULT_COLOR);
setProgressColor(progressColor);
int textColor = a.getColor(R.styleable.LoadingButton_pbTextColor, DEFAULT_COLOR);
setTextColor(textColor);
} finally {
a.recycle();
}
} else {
int white = getResources().getColor(DEFAULT_COLOR);
mLoadingMessage = getContext().getString(R.string.default_loading);
setProgressColor(white);
setTextColor(white);
setTextSize(mDefaultTextSize);
}
setupTextSwitcher();
}
public static BaseDSL.ViewClassResult textSwitcher() {
return BaseDSL.v(TextSwitcher.class);
}
public static Void textSwitcher(Anvil.Renderable r) {
return BaseDSL.v(TextSwitcher.class, r);
}
public static BaseDSL.ViewClassResult textSwitcher() {
return BaseDSL.v(TextSwitcher.class);
}
public static Void textSwitcher(Anvil.Renderable r) {
return BaseDSL.v(TextSwitcher.class, r);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
// Get the TextSwitcher view from the layout
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
// BEGIN_INCLUDE(setup)
// Set the factory used to create TextViews to switch between.
mSwitcher.setFactory(mFactory);
/*
* Set the in and out animations. Using the fade_in/out animations
* provided by the framework.
*/
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
// END_INCLUDE(setup)
/*
* Setup the 'next' button. The counter is incremented when clicked and
* the new value is displayed in the TextSwitcher. The change of text is
* automatically animated using the in/out animations set above.
*/
Button nextButton = (Button) findViewById(R.id.button);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCounter++;
// BEGIN_INCLUDE(settext)
mSwitcher.setText(String.valueOf(mCounter));
// END_INCLUDE(settext)
}
});
// Set the initial text without an animation
mSwitcher.setCurrentText(String.valueOf(mCounter));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linkup);
Stopwatch sw = new Stopwatch();
sw.start();
Display mDisplay = getWindowManager().getDefaultDisplay();
Point size = new Point();
mDisplay.getSize(size);
curLevelCfg = levelCfgs.get(getIntent().getIntExtra("levelIndex", 0));
holder.tvLevel = (TextView) findViewById(R.id.tvLevel);
holder.tvRecord = (TextView) findViewById(R.id.maxScore);
holder.pbTime = (ProgressBar) findViewById(R.id.pbTime);
holder.tsScore = (TextSwitcher) findViewById(R.id.scoreText);
holder.flBackground = (FrameLayout) findViewById(R.id.rootFrame);
holder.startCoin = (ImageView) findViewById(R.id.startCoin);
holder.endCoin = (ImageView) findViewById(R.id.endCoin);
holder.tvCombo = (TextView) findViewById(R.id.tvCombo);
holder.tvAnimMsg = (TextView) findViewById(R.id.tvAnimMsg);
holder.screenWidth = size.x;
holder.screenHeight = size.y;
holder.screenCenter = new Point((int) (size.x * 0.5), (int) (size.y * 0.5));
holder.tools = findViewById(R.id.tools);
holder.btnPrompt = (Button) findViewById(R.id.prompt);
holder.btnRefresh = (Button) findViewById(R.id.refresh);
holder.btnAddTime = (Button) findViewById(R.id.addTime);
holder.tsScore.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView tv = new TextView(GameActivity.this);
tv.setTextSize(30);
tv.setTextColor(0xffff6347);
tv.setGravity(Gravity.CENTER);
return tv;
}
});
if (curLevelCfg.getLevelMode() == GameMode.Level || curLevelCfg.getLevelMode() == GameMode.ScoreTask) {
holder.tsScore.setInAnimation(this, R.anim.slide_in_up);
holder.tsScore.setOutAnimation(this, R.anim.slide_out_up);
}
pathView = new PathView(this);
holder.flBackground.addView(pathView, -1, -1);
cardsView = (CardsView) findViewById(R.id.cardsView);
failDialog = new FailDialog(this);
successDialog = new SuccessDialog(this);
timeDialog = new TimeDialog(this);
taskDialog = new TaskDialog(this);
sw.stop();
Log.e("game load1", String.valueOf(sw.getElapsedTime()));
sw.start();
start();
sw.stop();
Log.e("game load2", String.valueOf(sw.getElapsedTime()));
}