没有按钮点击的TextSwitcher

时间:2015-08-21 11:34:25

标签: java android android-studio

嗨,有人可以帮助我,因为我是Android工作室开发的新手,需要帮助。我创建了一个按钮单击的文本切换器,但是我想添加一些修改,例如当用户进行联系或滑动屏幕时,它会滑动到下一个文本而不是使用单击方法上的按钮。请你指教一下吗?。

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 Fiile

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

1 个答案:

答案 0 :(得分:0)

这是Android开发者页面的链接,提供了有关如何使用ViewPager的视图,该视图应该完全符合您的要求。这是我之前在我的应用程序中完成它的方式。 ViewPager