从一种布局视图更改为另一种布局视图并返回?

发布于 2024-12-05 10:05:52 字数 1152 浏览 0 评论 0 原文

想要制作一个以 main 布局开始的 Android 应用程序,当您按下此布局中的按钮(称为 stateButton)时,布局将更改为 main2 布局包含另一个按钮(称为 boton2),当您按下此按钮时,您将返回到第一个主按钮。

我想在同一个活动中执行此操作,而不创建或启动另一个活动。

这里我向您展示部分代码:

public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
    private Button stateButton;
    private Button boton2;

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.main); 
        this.stateButton = (Button) this.findViewById(R.id.boton);
        this.boton2 = (Button) this.findViewById(R.id.boton2);      
        stateButton.setOnClickListener(this);
        boton2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v==stateButton) {
            setContentView(R.layout.main2);             
        }
        else if(v==boton2) {
            setContentView(R.layout.main);
        }
    }
}

主电源只有一些图像、文本视图和按钮。

但我有一些麻烦。难道就不能这么简单吗?或者我错过了什么或者出了什么问题?

want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.

I want to do this in the same activity without creating or starting another one.

Here I show you part of the code:

public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
    private Button stateButton;
    private Button boton2;

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.main); 
        this.stateButton = (Button) this.findViewById(R.id.boton);
        this.boton2 = (Button) this.findViewById(R.id.boton2);      
        stateButton.setOnClickListener(this);
        boton2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v==stateButton) {
            setContentView(R.layout.main2);             
        }
        else if(v==boton2) {
            setContentView(R.layout.main);
        }
    }
}

The mains only have some images, text views and the buttons.

But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?

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

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

发布评论

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

评论(4

似梦非梦 2024-12-12 10:05:52

当您使用 findViewById 时,您实际上是在尝试在 setContentView 指定的布局内查找视图。因此,当您尝试检查按钮时,一次又一次地使用 setContentView 可能会带来问题。

我不会使用 setContentView,而是将屏幕的 2 个布局添加为视图翻转器的子布局,该视图翻转器一次只显示一个子视图。并且您可以指定要显示哪个子项的索引。使用视图翻转器的好处是,如果在视图之间切换时需要动画,您可以轻松地为视图指定“进入”和“退出”动画。这是一个比一次又一次调用 setContentView 更干净的方法。

When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.

Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.

浅浅淡淡 2024-12-12 10:05:52

FrameLayout 很好地处理了这个问题...将其与 构造一起使用来加载多个其他布局,然后您可以使用以下方法在它们之间来回切换各个布局上的 setvisibility(View.VISIBLE);setVisibility(View.INVISIBLE);

例如:

主要 XML 包括两个其他布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <include android:id="@+id/buildinvoice_step1_layout" layout="@layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
    <include android:id="@+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="@layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>

在布局之间切换的代码:

findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);

您还需要在活动启动时(或在 XML 中)设置各个布局的可见性,否则您将看到它们两个 - 一个在另一个之上。

The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.

For example:

Main XML including two other layouts:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <include android:id="@+id/buildinvoice_step1_layout" layout="@layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
    <include android:id="@+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="@layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>

Code to switch between layouts:

findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);

You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.

雾里花 2024-12-12 10:05:52

您的 boton2 按钮将为 NULL,因为该按钮的定义位于 main2.xml 中。
您能够找到的唯一视图是在 main.xml 中定义的视图。

Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.

梦途 2024-12-12 10:05:52

谢谢!!!所有信息对于理解很多事情都很有用,正如 C0deAttack 评论的那样,我在使用 main2 上的按钮时遇到了麻烦。我所做的是将 View.VISIBLE 和 View.GONE 设置为我想要在每个布局中使用的 TextViews 和 Button。非常感谢。

Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.

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