読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - TextSwitcher

 

activity_main.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="match_parent"
android:layout_height="wrap_content"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"

android:onClick="next"
/>
</LinearLayout>
 
MainActivity.java
 
package com.example.liguofeng.textswitcher;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

String[] texts = {"JAVA", "C", "c#" , "VB"};

TextSwitcher switcher;
int index = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
switcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
// TextView生成
TextView view = new TextView(MainActivity.this);
view.setTextSize(50);
view.setTextColor(Color.MAGENTA);
view.setBackgroundColor(Color.YELLOW);
return view;
}
});

next(null); // 初期表示
}

public void next(View view){
switcher.setText(texts[index++ % texts.length]);
}
}