Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

TextSwitcher - Android Widgets Example

TextSwitcher - Android Widgets สำหรับ TextSwitcher เป็น Widget ใช้สำหรับ กำหนดรูปแบบการเคลื่อนไหวหรือเปลี่ยนแปลงข้อความต่าง ๆ (TextView) ที่อยู่ใน Form เช่น การสลับเปลี่ยนข้อความแบบมี Effect หรือกำหนดคุณสมบัติระหว่างการเปลี่ยนแปลง

TextSwitcher - Android Widgets

XML Syntax
    <TextSwitcher
        android:id="@+id/textSwitcher1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

Example 1 การใช้ TextSwitcher เพื่อเปลี่ยนแปลงข้อความ TextView ระหว่าง 2 ตัว

TextSwitcher - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<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>


ไฟล์ XML Layout ของ TextSwitcher

TextSwitcher - Android Widgets

จากรูปจะเห็นว่ามีการใช้ TextSwitcher ควบคุมการเปลี่ยนแปลงของ TextView ระหว่าง 2 ตัว

MainActivity.java (Java Code)
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;
    }
	
}


Screenshot

TextSwitcher - Android Widgets

คลิกที่ปุ่ม Switcher เพื่อเปลี่ยนแปลงระหว่าง TextView ทั้ง 2 ตัว




Example 2 การใช้ TextSwitcher กับ Animation เพื่อกำหนด Effect ในระหว่างการเปลี่ยนแปลงข้อความ และการประยุกต์ใช้กับรูปแบบอื่น ๆ

TextSwitcher - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<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>


MainActivity.java (Java Code)
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;
	}

    
}


Code ที่เป็น Java ทั้งหมด

        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);


จาก Code จะเห็นว่ามีการกำหนด Effect ที่เป็น Animation ให้กับ TextSwitcher

Screenshot

TextSwitcher - Android Widgets

ตัวอย่างการทำงานของ TextSwitcher กับ Animation โดยในตัวอย่างเมื่อมีการคลิก Button ที่เป็น บวก หรือ ลบ ข้อความจะมีการเปลี่ยนแปลง และในขั้นตอนการเปลี่ยนแปลงก็จะแสดง Effect ที่ได้กำหนดขึ้น




Example 3 การใช้ TextSwitcher กับรูปแบบอื่น ๆ โดยตัวอย่างนี้จะดึงข้อความมาจาก ArrayList และแสดงชุดของข้อความตามตำแหน่งที่เลือกไว้ สามารถกด Back หรือ Next ได้

TextSwitcher - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<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="&lt;&lt;" />
		
		    <Button
		        android:id="@+id/button2"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="&gt;&gt;" />
    </LinearLayout>		    

</RelativeLayout>


XML Layout ที่ได้ออกแบบไว้

        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");


ชุดของข้อความที่อยู่ใน ArrayList เพื่อจะกำหนดเงื่อนไขในการแสดงผล

MainActivity.java (Java Code)
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;
	}

    
}


Screenshot

TextSwitcher - Android Widgets

แสดงข้อความ

TextSwitcher - Android Widgets

แสดงชุดของข้อความที่มาจาก ArrayList






   
Share

Property & Method (Others Related)

Android Transitions Widgets
ImageSwitcher - Android Widgets Example
AdapterViewFlipper - Android Widgets Example
StackView - Android Widgets Example
ViewAnimator - Android Widgets Example
ViewFlipper - Android Widgets Example
ViewSwitcher - Android Widgets Example

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-01 16:23:27 / 2012-07-15 11:53:08
  Download : No files
 Sponsored Links / Related

 
Android Custom Adapter
Rating :

 
Android People Contact List, Name, Phone No, Photo Picture, Email and Address
Rating :

 
Android Rating (Vote) and ListView Part 1
Rating :

 
Android Rating (Vote) and ListView Part 2 (Member Login and Average Rating)
Rating :

 
Android PhoneGap (jQuery Mobile) Create Convert App from Website(URL)
Rating :

 
Android Capture Image and Camera Capture Screenshot (android.view.SurfaceView)
Rating :

 
Android Pull Down to Refresh And Release to Refresh or Update (Part 1)
Rating :

 
Android Pull Down to Refresh And Release to Update (Part 2 , PHP & MySQL)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่