使用动态生成的Textview创建Android TextSwitcher

使用动态生成的Textview创建Android TextSwitcher,android,text,textview,gallery,switch-statement,Android,Text,Textview,Gallery,Switch Statement,我打算用TextView创建一个类似TextSwitcher的图库,如下所示: 见图片 当我单击所选的文本视图时,将启动一个活动 我已经阅读了Android API演示和许多其他帖子,但我仍然无法创建类似的内容。APIdemo没有告诉我如何使用TextView和TextSwitcher 我相信它很有用,但我不知道如何使用它,如何链接到我的活动onCreate方法,并添加我动态生成的textview 与XML内容一起工作的解决方案是什么?我在API演示中得到了以下示例 从下面的路径 C:\Prog

我打算用
TextView
创建一个类似
TextSwitcher
的图库,如下所示:

见图片

当我单击所选的
文本视图
时,将启动一个活动

我已经阅读了Android API演示和许多其他帖子,但我仍然无法创建类似的内容。APIdemo没有告诉我如何使用
TextView
TextSwitcher

我相信它很有用,但我不知道如何使用它,如何链接到我的活动
onCreate
方法,并添加我动态生成的
textview


与XML内容一起工作的解决方案是什么?

我在API演示中得到了以下示例 从下面的路径

C:\Program Files\Android\android-sdk-windows\samples\android-10\ApiDemos\res\layout

TextSwitcher1.java

public class TextSwitcher1 extends Activity implements
   ViewSwitcher.ViewFactory, View.OnClickListener {

   private TextSwitcher mSwitcher;

   private int mCounter = 0;

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

       setContentView(R.layout.text_switcher_1);

       mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
       mSwitcher.setFactory(this);

       Animation in = AnimationUtils.loadAnimation(this,
               android.R.anim.fade_in);
       Animation out = AnimationUtils.loadAnimation(this,
               android.R.anim.fade_out);
       mSwitcher.setInAnimation(in);
       mSwitcher.setOutAnimation(out);

       Button nextButton = (Button) findViewById(R.id.next);
       nextButton.setOnClickListener(this);

       updateCounter();
   }

   public void onClick(View v) {
       mCounter++;
       updateCounter();
   }

   private void updateCounter() {
       mSwitcher.setText(String.valueOf(mCounter));
   }

   public View makeView() {
       TextView t = new TextView(this);
       t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
       t.setTextSize(36);
       return t;
   }
}

TextSwitcher_1.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

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

    <Button android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_switcher_1_next_text" />

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

</LinearLayout>

我无法创建带有动态文本视图的文本切换器


相反,我的替代解决方案是使用ImageView。这样,我就可以创建一个带有水平滚动的图库,如链接中所示。

是的,我已经读过了,但这并不是我想要做的。我有3个动态生成的TextView,如图所示,我需要使用TextSwitcher在3个TextView之间切换。示例显示的只是用于共享的counter.hi@sunil thnks的占位符。这对我真的很有帮助。但是我想在这个文本框上获得点击事件,其中包含当时查看的文本。你能指导我如何做到这一点吗。因为文本视图是动态添加的,所以我对如何在单击文本视图时获取其文本感到困惑。两个链接都已断开。链接似乎已断开。
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

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

    <Button android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_switcher_1_next_text" />

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

</LinearLayout>