如何使用具有三种布局的视图翻转器? [英] How to use view flipper with three layouts?

查看:16
本文介绍了如何使用具有三种布局的视图翻转器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将 ViewFlipper 用于具有两种不同布局的主要活动.我想使用第三种布局,但我只能找到 showNext()showPrevious() 命令.有人可以告诉我如何使用 ViewFlipper 实现第三种布局吗?

I am currently using ViewFlipper for my main activity with two different layouts. I want to use a third layout, but I can only find the showNext() and showPrevious() commands. Can someone show me how to implement a third layout using ViewFlipper?

推荐答案

为您制作了一个示例,展示了如何在 ViewFlipper 中显示不同的视图.

Made an example for you that shows howto display different views in a ViewFlipper.

示例的布局由以下部分组成.有三个单选按钮.ViewFlipper 位于单选按钮下方.这个鳍状肢包含三个不同的简单视图,带有不同的文本.

The layout of the example is made up of the following parts. There are three radio buttons. A ViewFlipper is placed below the radio buttons. This flipper holds three different simple views with different texts.

然后将单选按钮连接到 java 代码中的侦听器,该侦听器将根据当前选择的单选按钮更改 ViewFlipper 显示的视图.

The radio buttons are then hooked up to a listener in the java code that will change the view displayed by the ViewFlipper depending on which radio button that currently is chosen.

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <RadioGroup android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio0" android:layout_width="wrap_content"
            android:text="Show View 1" android:checked="true"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio1" android:layout_width="wrap_content"
            android:text="Show view 2"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio2" android:layout_width="wrap_content"
            android:text="Show View 3"></RadioButton>
    </RadioGroup>

    <ViewFlipper android:id="@+id/ViewFlipper01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <!--adding views to ViewFlipper-->
        <TextView android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First view is now displayed"></TextView>
        <TextView android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second view is now displayed"></TextView>
        <TextView android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Third view is now displayed"></TextView>
    </ViewFlipper>

</LinearLayout>

JAVA

package com.test.threeviews;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.ViewFlipper;

public class ThreeViewsinaFlipperActivity extends Activity {

    RadioButton RB0;
    RadioButton RB1;
    RadioButton RB2;
    ViewFlipper VF;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /*
         * Find the views declared in main.xml.
         */
        RB0 = (RadioButton) findViewById(R.id.radio0);
        RB1 = (RadioButton) findViewById(R.id.radio1);
        RB2 = (RadioButton) findViewById(R.id.radio2);
        VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);

        /*
         * Set a listener that will listen for clicks on the radio buttons and
         * perform suitable actions.
         */
        RB0.setOnClickListener(radio_listener);
        RB1.setOnClickListener(radio_listener);
        RB2.setOnClickListener(radio_listener);
    }

    /*
     * Define a OnClickListener that will change which view that is displayed by
     * the ViewFlipper
     */
    private OnClickListener radio_listener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.radio0:
                VF.setDisplayedChild(0);
                break;
            case R.id.radio1:
                VF.setDisplayedChild(1);
                break;
            case R.id.radio2:
                VF.setDisplayedChild(2);
                break;
            }
        }
    };
}

这篇关于如何使用具有三种布局的视图翻转器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆