Android - ViewFlipper 问题

发布于 2024-12-06 03:01:48 字数 4806 浏览 1 评论 0原文

我正在制作一个需要在 Activity 中使用多个视图的游戏,我决定使用 Viewflipper 来完成。

问题是。我需要在视图翻转器中有 3 个视图,最后一个视图将转移回第一个视图。

我的问题是按钮表现得很奇怪,它们要么不进入下一个视图,要么跳过第三个视图。我尝试将 vf.setDisplayedChild(R.Id.gamescreen1);在最后一次观看时,但随后整个事情就崩溃了。

感谢您提前提供的所有答案,我已经被这个问题困扰好几天了!是的,我知道我是个菜鸟:(

[源代码] 公共类 GameActivity 扩展 Activity {

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


    final ViewFlipper vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);

    //SCREEN 1

    Button btnSTART = (Button) findViewById(R.id.btnSTART);

    //SCREEN 2

    Button btnALT1 = (Button) findViewById(R.id.btnALT1);
Button btnALT2 = (Button) findViewById(R.id.btnALT1);        

    //SCREEN 3

    Button btnALT3 = (Button) findViewById(R.id.btnALT1);
Button btnALT4 = (Button) findViewById(R.id.btnALT1);

//screen 1

    btnSTART.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });

//screen 2 // Either button will go to view 3

    btnALT1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });

btnALT2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });


//screen 3 // Either button will go back to view 1

    btnALT3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();

        }        
    });

    btnALT4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });
}
}

[XML]

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

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="435dp" android:gravity="top">
        <ListView android:layout_width="fill_parent" android:id="@+id/list1" android:layout_height="184dp" android:layout_weight="0.53"></ListView>
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="bottom|center" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:id="@+id/btnSTART" android:layout_width="200dp" android:text="@string/btnstart"></Button>
    </LinearLayout>

</LinearLayout>


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

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
        <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/list2"></ListView>
    </LinearLayout>

    <LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:text="alt1" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT1"></Button>
        <Button android:text="alt2" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT2"></Button>

    </LinearLayout>

</LinearLayout>


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

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
    </LinearLayout>

    <LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:text="alt3" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT3"></Button>
        <Button android:text="alt4" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT4"></Button>
    </LinearLayout>

</LinearLayout>

I am making a game that requires multiple views within an Activity, and i decided to use Viewflipper to do.

The thing is. I need to have 3 views in in the viewflipper, and the LAST view will transfer back to the first one.

My problem is that the buttons are acting weird, and they are either not going to the next, or skipping the third view. I tried to put vf.setDisplayedChild(R.Id.gamescreen1); at the last view but then the whole thing breaks down.

Thanks for all answers in advance, i have been stuck with this problem for DAYS! and yes, i know that i am a noob :(

[SOURCECODE]
public class GameActivity extends Activity {

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


    final ViewFlipper vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);

    //SCREEN 1

    Button btnSTART = (Button) findViewById(R.id.btnSTART);

    //SCREEN 2

    Button btnALT1 = (Button) findViewById(R.id.btnALT1);
Button btnALT2 = (Button) findViewById(R.id.btnALT1);        

    //SCREEN 3

    Button btnALT3 = (Button) findViewById(R.id.btnALT1);
Button btnALT4 = (Button) findViewById(R.id.btnALT1);

//screen 1

    btnSTART.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });

//screen 2 // Either button will go to view 3

    btnALT1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });

btnALT2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });


//screen 3 // Either button will go back to view 1

    btnALT3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();

        }        
    });

    btnALT4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });
}
}

[XML]

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

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="435dp" android:gravity="top">
        <ListView android:layout_width="fill_parent" android:id="@+id/list1" android:layout_height="184dp" android:layout_weight="0.53"></ListView>
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="bottom|center" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:id="@+id/btnSTART" android:layout_width="200dp" android:text="@string/btnstart"></Button>
    </LinearLayout>

</LinearLayout>


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

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
        <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/list2"></ListView>
    </LinearLayout>

    <LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:text="alt1" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT1"></Button>
        <Button android:text="alt2" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT2"></Button>

    </LinearLayout>

</LinearLayout>


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

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
    </LinearLayout>

    <LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:text="alt3" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT3"></Button>
        <Button android:text="alt4" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT4"></Button>
    </LinearLayout>

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2024-12-13 03:01:48

试试这个

flipper.setDisplayedChild(1);
.
.to
.
flipper.setDisplayedChild(3);

if(flipper.getCurrentView() == 3)
{
    flipper.setDisplayedChild(1);
}

try this

flipper.setDisplayedChild(1);
.
.to
.
flipper.setDisplayedChild(3);

if(flipper.getCurrentView() == 3)
{
    flipper.setDisplayedChild(1);
}
因为看清所以看轻 2024-12-13 03:01:48

setDisplayChild 采用一个整数参数,该参数是要显示的子项的从零开始的索引,而不是要显示的子项的 ID。我知道有点令人困惑。

setDisplayChild takes an integer parameter that is the zero based index of the child to display NOT the id of the child to display. A bit confusing I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文