شرح TextSwitcher في الاندرويد

برنامج TextSwitcher التعليمي مع مثال في Android Studio

في الروبوت، TextSwitcher هو متخصصة ViewSwitcher التي تحتوي على الأطفال فقط من نوع TextView . يتوفر TextSwitcher في Android من الإصدار Android 1.6+.

يعد TextSwitcher مفيدًا لتحريك ملصق (أي نص) على الشاشة. إنه عنصر من عناصر عنصر واجهة الانتقال الذي يساعدنا على إضافة انتقالات على الملصقات. عندما يتم استدعاء طريقة setText (CharSequence) ، يقوم TextSwitcher ببساطة بتحريك النص الحالي وإخراج النص الجديد. على سبيل المثال ، تحتاج إلى التنقل بين المعلومات في TextView مثل التنقل عبر قائمة التواريخ باستخدام الزر الأيسر والأيمن .

TextSwitcher في Android


خطوات تنفيذ TextSwitcher :

يتم إنشاء طرق عرض جديدة في TextSwitcher باستخدام مصنع switcherid.setFactory (). وعند استخدام طريقة setText () ، فإنه يزيل أولاً العرض القديم باستخدام مجموعة رسوم متحركة باستخدام طريقة switcherid.setOutAnimation () ، ثم يضع الجديد باستخدام الرسوم المتحركة التي تم تعيينها بواسطة طريقة switcherid.setInAnimation ().

  1. احصل على مرجع TextSwitcher في الفصل باستخدام طريقة findViewById () ، أو يمكنك أيضًا إنشاء كائن ديناميكيًا.
  2. تعيين مصنع باستخدام switcherid.setFactory ()
  3. قم بتعيين رسم متحرك داخلي باستخدام switcherid.setInAnimation ()
  4. قم بتعيين رسم متحرك خارجي باستخدام switcherid.setOutAnimation ()

رمز XML الأساسي TextSwitcher :

<TextSwitcher
  android:id="@+id/simpleTextSwitcher"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

طرق مهمة لتحويل النص:

دعونا نناقش بعض الطرق المهمة في TextSwitcher والتي يمكن استدعاؤها من أجل إدارة TextSwitcher.

1. setFactory (مصنع ViewFactory): تُستخدم هذه الطريقة لإنشاء عرض جديد لـ TextSwitcher. باستخدام هذه الطريقة ، نقوم بإنشاء TextView جديدواستبدال العرض القديم بذلك.

نوضح أدناه كيفية إنشاء عرض جديد باستخدام طريقة setFactory.

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): تُستخدم هذه الطريقة لتعيين النص في TextView الذي يتم عرضه حاليًا.

أدناه قمنا بتعيين النص في TextView الذي يتم عرضه حاليًا.

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

setCurrentText في TextSwitcher Android

3. setText (نص CharSequence): تُستخدم هذه الطريقة لتعيين النص في العرض التالي والانتقال إلى العرض التالي.

أدناه قمنا بتعيين النص في العرض التالي والتبديل إلى العرض التالي.

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

setText في TextSwitcher Android

4. loadAnimation (سياق السياق ، معرف int): تُستخدم هذه الطريقة كلما احتجنا إلى تعريف كائن من فئة الرسوم المتحركة من خلال فئة AnimationUtils عن طريق استدعاء أسلوب ثابت loadAnimation.

أدناه نقوم بإنشاء كائن من فئة الرسوم المتحركة وتحميل الرسوم المتحركة باستخدام فئة 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 (الرسوم المتحركة في الرسوم المتحركة): تُستخدم هذه الطريقة لتعيين الرسوم المتحركة لمظهر الكائن على الشاشة.

أدناه نقوم بإنشاء كائن من فئة الرسوم المتحركة وتحميل الرسوم المتحركة باستخدام فئة AnimationUtils ثم تعيين الرسوم المتحركة على 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

setInAnimation في TextSwitcher Android

6. setOutAnimation (Animation outAnimation): تقوم هذه الطريقة بعكس تعيين setInAnimation ().

كلما قمنا بتعيين تسمية جديدة في TextView ، فإنه يزيل أولاً العرض القديم باستخدام مجموعة رسوم متحركة باستخدام طريقة setOutAnimation () ، ثم يضع العلامة الجديدة باستخدام الرسوم المتحركة التي تم تعيينها بواسطة طريقة setInAnimation ().

أدناه نقوم بإنشاء كائن من فئة الرسوم المتحركة وتحميل الرسوم المتحركة باستخدام فئة AnimationUtils ثم تعيين الرسوم المتحركة على 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

setOutAnimation في TextSwitcher Android


سمات  وخصائص TextSwitcher :

الآن دعنا نناقش بعض السمات الشائعة لـ TextSwitcher التي تساعدنا على تكوينها في تخطيطنا ( xml ).

1. المعرف id :  السمة id تستخدم لتعريف TextSwitcher بشكل فريد.

أدناه قمنا بتعيين معرف 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 -->

2. المساحة المتروكة padding : تُستخدم هذه السمة لتعيين المساحة المتروكة من الجانب الأيسر أو الأيمن أو العلوي أو السفلي لمبدل النص.

  • padding إلى اليمين: تُستخدم هذه السمة لتعيين المساحة المتروكة من الجانب الأيمن من TextSwitcher .
  • paddingLeft: تُستخدم هذه السمة لتعيين المساحة المتروكة من الجانب الأيسر من TextSwitcher .
  • paddingTop: تُستخدم هذه السمة لتعيين المساحة المتروكة من الجانب العلوي من TextSwitcher .
  • paddingBottom: تُستخدم هذه السمة لتعيين المساحة المتروكة من الجانب السفلي من TextSwitcher .
  • الحشوة: تُستخدم هذه السمة لتعيين المساحة المتروكة من جميع جوانب TextSwitcher .

أدناه قمنا بتعيين حشوة 10dp من جميع جوانب 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-->

الحشو في الخلفية في TextSwitcher Android

3. الخلفية background : تُستخدم هذه السمة لتعيين خلفية TextSwitcher. يمكننا ضبط لون أو رسم في خلفية TextSwitcher.

في كود الحشو ، استخدمنا أيضًا الخلفية وبعض السمات المشتركة الأخرى.

إعداد الخلفية في TextSwitcher في فئة Java:

TextSwitcher simpleTextSwitcher = (TextSwitcher) findViewById(R.id.simpleTextSwitcher); // get the reference of TextSwitcher.
  simpleTextSwitcher.setBackgroundColor(Color.BLACK); // set black color in the background of TextSwitcher.

TextSwitcher Example In Android Studio:

Below is the example of TextSwitcher in which we display a TextSwitcher by using its different attributes. In this we use one Button for changing the label of TextSwitcher. Firstly we create a string array and create dynamic TextView using setFactory method and then load and set slide in left and slide in right animation and finally set the text in the TextSwitcher on button click using setText method. Whenever a user click on next button TextSwitcher switch between the labels and the current label will go out and next label will come in with specified animation.

Below you can download complete Android Studio code, see final output and step by step explanation of the example:

Download Code ?

TextSwitcher Example In Android Studio

Step 1: Create a new project and name it TextSwitcherExample

Step 2: Open res -> layout ->activity_main.xml (or) main.xml and add following code :

In this step we open an xml file and add the code for displaying a Button and a TextSwitcher by using its different attributes.

<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" />

  </LinearLayout>

الخطوة 3:  افتح src -> package -> MainActivity.java

في هذه الخطوة نفتح MainActivity ونضيف الكود لبدء TextSwitcher و Button. هنا أولاً نقوم بإنشاء مصفوفة سلسلة وإنشاء TextView ديناميكي باستخدام طريقة setFactory ثم نقوم بتحميل وتعيين الشريحة في اليسار والشريحة في الرسوم المتحركة اليمنى وأخيراً نضبط النص في TextSwitcher عند النقر فوق الزر باستخدام طريقة setText. عندما ينقر المستخدم على الزر التالي TextSwitcher ، قم بالتبديل بين التسميات وستخرج التسمية الحالية وستأتي التسمية التالية مع الرسوم المتحركة المحددة.

package com.example.ip_d.textswitcherexample;

  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.Gravity;
  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 AppCompatActivity {
  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
  }
  });

  }

  }

المخرجات :

الآن قم بتشغيل التطبيق وسترى TextSwitcher قيد التشغيل. ما عليك سوى النقر فوق الزر "التالي" وسيبدأ النص في التبديل بين واحد وآخر.