<TextSwitcher android:id="@+id/textSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="75dp" > <TextSwitcher android:id="@+id/textSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView 1" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView 2" /> </TextSwitcher> </LinearLayout> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="Switcher" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextSwitcher; public class MainActivity extends Activity { Button btn1; TextSwitcher ts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ts = (TextSwitcher) findViewById(R.id.textSwitcher1); btn1 = (Button) findViewById(R.id.Button01); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click ts.showNext(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextSwitcher android:id="@+id/textSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" > </TextSwitcher> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textSwitcher1" android:layout_marginRight="34dp" android:layout_marginTop="64dp" android:layout_toLeftOf="@+id/textSwitcher1" android:text="Value +" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="23dp" android:layout_toRightOf="@+id/textSwitcher1" android:text="Value -" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.Menu; 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 ViewFactory { private TextSwitcher txtSwitcher; private int mCounter = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1); txtSwitcher.setFactory(this); Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); txtSwitcher.setInAnimation(in); txtSwitcher.setOutAnimation(out); Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mCounter++; updateCounter(); } }); Button btn2 = (Button) findViewById(R.id.button2); btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mCounter--; updateCounter(); } }); updateCounter(); } private void updateCounter() { // TODO Auto-generated method stub txtSwitcher.setText(String.valueOf(mCounter)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public View makeView() { // TODO Auto-generated method stub TextView txtView = new TextView(this); txtView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); txtView.setTextSize(36); return txtView; } }
txtSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1); txtSwitcher.setFactory(this); Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); txtSwitcher.setInAnimation(in); txtSwitcher.setOutAnimation(out);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="75dp" > <TextSwitcher android:id="@+id/textSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" > </TextSwitcher> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="200dp" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="<<" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">>" /> </LinearLayout> </RelativeLayout>
arrList.add("String 1 String 1 String 1 String 1"); arrList.add("String 2 String 2 String 2 String 2"); arrList.add("String 3 String 3 String 3 String 3"); arrList.add("String 4 String 4 String 4 String 4"); arrList.add("String 5 String 5 String 5 String 5");
package com.myapp; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.Menu; 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 ViewFactory { List<String> arrList = new ArrayList<String>(); private TextSwitcher txtSwitcher; int position = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); arrList.add("String 1 String 1 String 1 String 1"); arrList.add("String 2 String 2 String 2 String 2"); arrList.add("String 3 String 3 String 3 String 3"); arrList.add("String 4 String 4 String 4 String 4"); arrList.add("String 5 String 5 String 5 String 5"); txtSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1); txtSwitcher.setFactory(this); Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); txtSwitcher.setInAnimation(in); txtSwitcher.setOutAnimation(out); txtSwitcher.setText(String.valueOf(arrList.get(position))); Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setPositionPrev(); txtSwitcher.setText(String.valueOf(arrList.get(position))); } }); Button btn2 = (Button) findViewById(R.id.button2); btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setPositionNext(); txtSwitcher.setText(String.valueOf(arrList.get(position))); } }); } public void setPositionNext() { position++; if(position > arrList.size() -1) { position = 0; } } public void setPositionPrev() { position--; if(position < 0) { position = arrList.size() - 1; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public View makeView() { // TODO Auto-generated method stub TextView txtView = new TextView(this); txtView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); txtView.setTextSize(20); return txtView; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท