eclipse Android - 如何通过单击按钮更改 xml 视图/布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13246681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:20:48  来源:igfitidea点击:

Android - How to change xml views / layout with button click?

androideclipsesdk

提问by MoschDev

I want to use a button to change xml layout / view (what exactly do you call the page shown). I have developed a app that will feature many pages about 5 or 6, I need to be able to move forward with a button to a page and go back to the previous page with a different button.

我想用一个按钮来改变 xml 布局/视图(你到底怎么称呼显示的页面)。我开发了一个应用程序,该应用程序将包含大约 5 或 6 个页面,我需要能够使用一个按钮前进到一个页面,然后使用不同的按钮返回上一页。

button 1 is the forward button

按钮 1 是前进按钮

button 2 is the backwards button

按钮 2 是向后按钮

button 3 is a reset button (brings back to the first page)

按钮 3 是重置按钮(返回第一页)

Thank in advanced!

先谢谢了!

采纳答案by Greezer

you could make activities for each view or you could implement flipviewer

您可以为每个视图制作活动,或者您可以实现翻转查看器

sample layout:

示例布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/flip_me"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Flip Me!"
/>
<ViewFlipper android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FF00FF00"
android:text="This is the first panel"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FFFF0000"
android:text="This is the second panel"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FFFFFF00"
android:text="This is the third panel"
/>
</ViewFlipper>
</LinearLayout>

sample activity:

样本活动:

public class FlipperSample extends Activity {

 ViewFlipper flipper;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setContentView(R.layout.main);

        flipper=(ViewFlipper)findViewById(R.id.details);

        Button btn=(Button)findViewById(R.id.flip_me);

        btn.setOnClickListener((View.OnClickListener) new flipMyView());

    }

 class flipMyView implements View.OnClickListener {
  public void onClick(View view) {
   flipper.showNext();
  }
 }
}

回答by Anuj Sharma

You can use ViewFlippers or you can set the layout to your activity on button click...

您可以使用 ViewFlippers 或者您可以在按钮单击时将布局设置为您的活动...

回答by Girish Nair

Use the setContentView(R.layout.newLayout)to change the layout On the buttons onClickcall the setContentViewwith different layout file like this

使用setContentView(R.layout.newLayout)改变布局 在按钮上onClick调用setContentView不同的布局文件,像这样

Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setContentView(R.layout.newLayout);
        }
    });

NOTE:Since your doing like this findViewById(R.id.button1)will create NullPointerExceptionif that view does not have that element so be carefull

注意:因为你这样做findViewById(R.id.button1)会创建NullPointerException如果该视图没有该元素所以要小心