java - 没有按钮单击的TextSwitcher

标签 java android android-studio

嗨,有人可以帮我,因为我是android studio开发的新手,需要帮助。我已经创建了一个带有按钮单击的textswitcher,但是我想添加一些修改,例如当用户建立联系或在屏幕上滑动时,它会滑动到下一个文本,而不是使用click on按钮。请您指教?

Java文件

public class NationalMediaMuseum extends Activity
{
    private TextSwitcher mSwitcher;
    Button btnNext;

    // Array of String to Show In TextSwitcher
    String textToShow[]={"Main HeadLine","Hello World ","Hello Eeveryone","New Articles","Business News","What IS New"};
    int messageCount=textToShow.length;
    // to keep current Index of text
    int currentIndex=-1;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_national_media_museum);

        // get The references
        btnNext=(Button)findViewById(R.id.buttonNext);
        mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);

        // Set the ViewFactory of the TextSwitcher that will create TextView object when asked
        mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

            public View makeView() {
                // TODO Auto-generated method stub
                // create new textView and set the properties like clolr, size etc
                TextView myText = new TextView(NationalMediaMuseum.this);
                myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                myText.setTextSize(36);
                myText.setTextColor(Color.BLUE);
                return myText;
            }
        });

        // Declare the in and out animations and initialize them
        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 of textSwitcher
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);

        // ClickListener for NEXT button
        // When clicked on Button TextSwitcher will switch between texts
        // The current Text will go OUT and next text 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 reset it
                if (currentIndex == messageCount)
                    currentIndex = 0;
                mSwitcher.setText(textToShow[currentIndex]);
            }
        });

    }
}

XML文件
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:background="#0d47a1" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/imageView8"
            android:src="@drawable/logo"
            android:layout_weight="0.32"
            android:contentDescription="@string/logonew" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/logo1"
            android:textColor="#ffffff"
            android:id="@+id/textView12"
            android:layout_gravity="center"
            android:textSize="30sp"
            android:layout_marginLeft="25dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/imageView9"
            android:src="@drawable/logo"
            android:layout_weight="1"
            android:contentDescription="@string/logo2" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="801dp"
        android:background="#64b5f6"
        android:id="@+id/relativeLayout3">

        <TextView
            android:layout_width="200dp"
            android:textColor="#ffffffff"
            android:layout_height="40dp"
            android:id="@+id/textwelcome"
            android:textStyle="bold"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="40dp"
            android:id="@+id/uname"
            android:layout_alignParentTop="true"
            android:textColor="#ffffffff"
            android:layout_toRightOf="@+id/textwelcome"
            android:textStyle="italic"
            android:layout_toStartOf="@+id/logout"
            android:layout_toLeftOf="@+id/logout" />


        <Button
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="LOGOUT"
            android:id="@+id/logout"
            android:textColor="#ffffffff"
            android:background="#0d47a1"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Museum And Theatres Category"
            android:textColor="#ffffff"
            android:id="@+id/textView16"
            android:layout_gravity="center"
            android:textSize="20dp"
            android:gravity="center|center_horizontal"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textwelcome"
            android:layout_alignRight="@+id/logout"
            android:layout_alignEnd="@+id/logout" />

        <Button
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="NEXT"
            android:id="@+id/nxtbtn"
            android:textColor="#ffffffff"
            android:background="#0d47a1"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="BACK"
            android:id="@+id/bkebtn"
            android:textColor="#ffffffff"
            android:background="#0d47a1"
            android:layout_alignTop="@+id/nxtbtn"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="HOME"
            android:id="@+id/hmebtn"
            android:textColor="#ffffffff"
            android:background="#0d47a1"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <TextSwitcher
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textSwitcher"
            android:layout_below="@+id/textView16"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="NEXT"
            android:id="@+id/buttonNext"
            android:textColor="#ffffffff"
            android:background="#0d47a1"
            android:layout_below="@+id/textSwitcher"
            android:layout_alignLeft="@+id/hmebtn"
            android:layout_alignStart="@+id/hmebtn"
            android:layout_marginTop="61dp" />

    </RelativeLayout>
</LinearLayout>

最佳答案

这是一个指向android开发人员页面的链接,该页面提供了有关如何使用ViewPager的详细信息,该ViewPager应该可以完全满足您的要求。这是我以前在应用程序中完成的方法。 ViewPager

关于java - 没有按钮单击的TextSwitcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32139269/

相关文章:

java - Firestore - 将@Exclude 注释放在哪里?

java - android studio 中的 Android URLConnection.setRequestProperty() 似乎没有做任何事情

android - 为什么 XML 元素在我的 android studio 模拟器上不可见?

android-studio - 如何在 Android Studio 中导入谷歌字体?

java - 如何解决 Android Studio 中的 Phenotype API 错误?

java - 无法使用 JAXB 解析响应

java - jsf2 + <托管属性> <属性名称>

java - @Controller 无法在 Spring boot 多模块 Maven 项目中的(模块 Web)子包中工作

Java 数组列表帮助

android - 有没有办法从下一次应用程序/进程崩溃测试继续进行 Espresso 测试?