TextSwitcher trong Android
TextSwitcher thường sử dụng để làm hiệu ứng chuỗi trên màn hình, TextSwitcher đơn giản làm mờ dần chuỗi văn bản hiện tại và hiện dần chuỗi văn bản mới
TextSwitcher code trong XML:
<TextSwitcher android:id="@+id/simpleTextSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content"/>
Các bước tạo TextSwitcher
Bước 1: Khởi tạo TextSwitcherbằng phương thức findViewById(), hoặc bạn có thể tạo động .
Bước 2: Thiết lập view để hiển thị ảnh bằng phương thức switcherid.setFactory()
Bước 3: Thiết lập ảnh động dạng đưa vào dần dùng phương thức switcherid.setInAnimation()
Bước 4: Thiết lập ảnh động dạng làm mờ dần dùng phương thức switcherid.setOutAnimation()
Các phương thức quan trọng của TextSwitcher
Sau đây là các phương thức quan trọng thường được sử dụng để quản lý TextSwitcher
1. setFactory(ViewFactory factory): Phương thức này dùng tạo mới một View cho TextSwitcher. Bằng cách dùng phương thức này chúng ta tạo mới một View mới và thay thế View cũ.
Ví dụ sau sử dụng phương thức setFactory để tạo View mới
TextSwitcher simpleTextSwitcher=(TextSwitcher)findViewById(R.id. simpleTextSwitcher); // get reference of TextSwitcher // Set the ViewFactory of the TextSwitcher that will create TextView object when asked simpleTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() { public View makeView() { // TODO Auto-generated method stub // create a TextView TextView t = new TextView(MainActivity.this); // set the gravity of text to top and center horizontal t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); // set displayed text size t.setTextSize(36); return t; } });
2. setCurrentText(CharSequence text): Phương thức này được sử dụng để thiết lập chuỗi văn bản hiện tại trên TextSwitcher.
Ví dụ sau chúng ta thiết lập chuỗi văn bản hiện tại trên TextSwitcher
TextSwitcher simpleTextSwitcher = (TextSwitcher) findViewById(R.id.simpleTextSwitcher); // get the reference of TextSwitcher simpleTextSwitcher.setCurrentText("current Text"); // set current text in TextView that is currently showing
3. setText(CharSequence text): Phương thức này thường sử dụng để thiết lập chuỗi văn bản trên view kế tiếp và chuyển sang view kế tiếp.
Ví dụ sau thiết lập chuỗi văn bản trên view kế tiếp và chuyển đến view kế tiếp
TextSwitcher simpleTextSwitcher = (TextSwitcher) findViewById(R.id.simpleTextSwitcher); // get the reference of TextSwitcher simpleTextSwitcher.setText("text after switching"); // set current text on the next view and switch to next view
4. loadAnimation(Context context, int id): Phương thức này được sử dụng khi chúng ta cần tạo ra một đối tượng chuỗi văn bản động.
Ví dụ sau chúng ta tạo ra một đối tượng lớp Animation và nạp một chuỗi văn bản động bằng cách sử dụng lớp AnimationUtils
// load an animation by using AnimationUtils class Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
5. setInAnimation(Animation inAnimation): Phương thức này thường được sử dụng để thiết lập một chuỗi văn bản động xuất hiện ở giữa màn hình.
Ví dụ sau chúng ta tạo ra một đối tượng lớp Animation và nạp một chuỗi văn bản động bằng cách sử dụng lớp AnimationUtils, sau đó thiết lập Animation lên TextSwitcher
TextSwitcher simpleTextSwitcher=(TextSwitcher)findViewById(R.id. simpleTextSwitcher); // initiate a TextSwitcher Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); // load an animation simpleTextSwitcher.setInAnimation(in); // set in Animation for TextSwitcher
6. setOutAnimation(Animation outAnimation): Phương thức đối ngược với phương thức setInAnimation().
Ví dụ sau chúng ta tạo ra một đối tượng lớp Animation và nạp một chuỗi văn bản động bằng cách sử dụng lớp AnimationUtils, sau đó thiết lập chuỗi văn bản mờ dần lên TextSwitcher.
TextSwitcher simpleTextSwitcher=(TextSwitcher)findViewById(R.id. simpleTextSwitcher); // get reference of TextSwitcher Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); // load an animation simpleTextSwitcher.setOutAnimation(out); // set out Animation for TextSwitcher
Một số thuộc tính thường dùng của TextSwitcher
1. android:id: Là thuộc tính duy nhất của TextSwitcher.
<TextSwitcher android:id="@+id/simpleTextSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" /><!-- id of TextSwitcher that is used to uniquely identify it -->
Dựa vào Id ta sẽ lấy được control theo đúng Id này, xem code bên dưới để biết cách lấy control theo Id:
TextSwitcher simpleTextSwitcher=(TextSwitcher)findViewById(R.id. simpleTextSwitcher); // get reference of TextSwitcher
2. android:padding: Thuộc tính này xác định khoảng cách từ đường viền của TextSwitcher với nội dung nó chứa: left, right, top or bottom. Cũng ví dụ trên bây giờ chúng ta xác định padding=20dp từ mọi phía cho TextSwitcher.
<TextSwitcher android:id="@+id/simpleTextSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="50dp" android:background="#028B2F" android:padding="20dp"/><!--Setting background and padding in TextSwitcher-->
3. android:background: Thuộc tính này thiết lập màu nền TextSwitcher
Code sau chúng ta thiết lập màu nền, màu đỏ cho TextSwitcher
extSwitcher simpleTextSwitcher = (TextSwitcher) findViewById(R.id.simpleTextSwitcher); // get the reference of TextSwitcher. simpleTextSwitcher.setBackgroundColor(Color.RED); // set black color in the background of TextSwitcher.
Ví dụ: Trong ví dụ này chúng ta sẽ làm ứng dụng gồm có một TextSwitcher, một Button.Thiết lập sự kiện cho Button, khi người sử dụng click vào Button sẽ chuyển qua chuỗi văn bản khác. Tiến hành tạo project, vào thư mục res /layout -> activity_main.xml thiết kế giao diện sau:
Bước 1: Tạo một project tên là TextSwitcher: File->New->Android Application Project điền các thông tin ->Next ->Finish
Bước 2: Mở res -> layout -> xml (hoặc) activity_main.xml và thêm code, chúng ta sẽ tạo các đối tượng TextSwitcher và Button trong Linear Layout.
<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/simpleTextSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="50dp" /> <Button android:id="@+id/buttonNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:layout_gravity="center" android:background="#005" android:textColor="#fff" android:textStyle="bold" android:text="NEXT" />
Bước 3: Mở app -> src ->MainActivity.java
Trong bước này chúng ta khởi tạo ImageSwitcher và Button. Chúng ta cũng khai báo một mảng id để chứa các chuỗi văn bản, đồng thơi tạo một ImageView động bằng phương thức setFactory và sau đó, load và thiết lập các thanh trượt bên trái và bên phải cùa chuỗi văn bản động và cuối cùng thiết chuỗi văn bản vào trong TextSwitcher bằng phương thức setImageResource. Khi người sử dụng clcik vào button "NEXT" TextSwitcher sẽ chuyển từ chuỗi hiện tại sang chuỗi kế tiếp.
package com.example.textswitcher; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; 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; public class MainActivity extends Activity { private TextSwitcher simpleTextSwitcher; Button btnNext; // Array of String to Show In TextSwitcher String strings[] = {"Text Switcher 1", "Text Switcher 2", "Text Switcher 3", "Text Switcher 4", "Text Switcher 5"}; int messageCount = strings.length; // to keep current Index of textID array int currentIndex = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get The references if Button and TextSwitcher btnNext = (Button) findViewById(R.id.buttonNext); simpleTextSwitcher = (TextSwitcher) findViewById(R.id.simpleTextSwitcher); // Set the ViewFactory of the TextSwitcher that will create TextView object when asked simpleTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() { public View makeView() { // TODO Auto-generated method stub // create a TextView TextView t = new TextView(MainActivity.this); // set the gravity of text to top and center horizontal t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); // set displayed text size t.setTextSize(36); return t; } }); // Declare in and out animations and load them using AnimationUtils class Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); // set the animation type to TextSwitcher simpleTextSwitcher.setInAnimation(in); simpleTextSwitcher.setOutAnimation(out); //text appear on start simpleTextSwitcher.setCurrentText("click on next button to switch text"); // ClickListener for NEXT button // When clicked on Button TextSwitcher will switch between labels // The current label will go out and next label will come in with specified animation btnNext.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub currentIndex++; // If index reaches maximum then reset it if (currentIndex == messageCount) currentIndex = 0; simpleTextSwitcher.setText(strings[currentIndex]); // set Text in TextSwitcher } }); } } }
Ứng dụng này được phát triển bởi adt bundle, android 4.2 sử dụng minimum sdk 11 and target sdk 21.
Kết quả khi chạy ứng dụng và kết quả khi click vào "NEXT":
Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com