首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不单击按钮的TextSwitcher

不单击按钮的TextSwitcher
EN

Stack Overflow用户
提问于 2015-08-21 19:34:25
回答 1查看 492关注 0票数 2

嗨,谁能帮助我,因为我是新的android工作室开发,需要帮助。请你给点建议好吗?

Java文件

代码语言:javascript
复制
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]);
            }
        });

    }
}

代码语言:javascript
复制
  <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>
EN

回答 1

Stack Overflow用户

发布于 2015-08-21 21:28:57

这里有一个指向安卓开发人员页面的链接,它提供了一个关于如何使用ViewPager的概述,它应该能做你正在寻找的事情。这就是我之前在应用程序中使用的方法。ViewPager

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32139269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档