简体   繁体   English

Android和setContentView?

[英]Android and setContentView?

I have one activity, off this I have two classes. 我有一项活动,因此我有两节课。

When my app starts I setContentView to a layout that is in one of my classes. 当我的应用程序启动时,我将ContentView设置为我的一个类中的布局。

Now inside this layout there is a button which needs to setContentView to the layout that is in my other class. 现在,在此布局中有一个按钮,需要将ContentView设置为其他类中的布局。

How can I change the content view? 如何更改内容视图? How can my subclass send a message back to my main class to tell it to change the setContentView? 我的子类如何将消息发送回我的主类,以告诉它更改setContentView?

"off this I have two classes" are they subclasses to your activity, inner classes? “我有两个类”,它们是您活动的子类,内部类吗? Your problem is not really apparent to me. 您的问题对我来说并不是很明显。

The way I would do that is have two XML layouts and two activities: layout1 and layout2 , Activity1 and Activity2 . 我这样做的方法是有两个XML布局和两个活动: layout1layout2Activity1Activity2

You call setContent(R.layout.layout1) normally in your onCreate method in Activity1 , and you add the following code in your button onClick method: 您通常在Activity1onCreate方法中调用setContent(R.layout.layout1) ,并在按钮的onClick方法中添加以下代码:

finish(); // to close current activity
startActivity(this, Activity2.class);

You should create another Activity which uses the second layout, and make that button launch that activity, like this: 您应该创建另一个Activity使用第二布局,使该按钮启动该活动,如下所示:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
        startActivity(intent);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM