android:如何在按钮点击时更改布局?

时间:2022-11-21 15:27:28

I have to following code for selecting layout on button click.

我必须按照代码选择按钮点击时的布局。

View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.DownloadView: 
                // doStuff
                setContentView(R.layout.main);
                break;
            case R.id.AppView: 
                // doStuff
                setContentView(R.layout.app);
                break;
        }
    }
};

findViewById(R.id.DownloadView).setOnClickListener(handler);
findViewById(R.id.AppView).setOnClickListener(handler);

When I click the "AppView" button, the layout changes, but when I click the "DownloadView "button, nothing happens.

当我单击“AppView”按钮时,布局会发生变化,但是当我单击“DownloadView”按钮时,没有任何反应。

This link says that I have to start a new activity.

这个链接说我必须开始一项新的活动。

But I don't know how to use the code there of intent to start new activity, will a new file be added?

但我不知道如何使用意图开始新活动的代码,是否会添加新文件?

EDIT: I have my code on the new activity:

编辑:我有关于新活动的代码:

package com.example.engagiasync;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class AppView extends Activity implements OnClickListener{


    @Override
    public void onCreate(Bundle savedInstanceState){

        setContentView(R.layout.app);

        TextView tv = (TextView) this.findViewById(R.id.thetext);
        tv.setText("App View yo!?\n");
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

but it does not work, it force closes, the logcat says:android:如何在按钮点击时更改布局?

但它没有用,它强行关闭,logcat说:

8 个解决方案

#1


19  

  Button btnDownload = (Button) findViewById(R.id.DownloadView);
  Button btnApp = (Button) findViewById(R.id.AppView);

  btnDownload.setOnClickListener(handler);
  btnApp.setOnClickListener(handler);

  View.OnClickListener handler = new View.OnClickListener(){

  public void onClick(View v) {

    if(v==btnDownload){ 
            // doStuff
            Intent intentMain = new Intent(CurrentActivity.this , 
                                           SecondActivity.class);
            CurrentActivity.this.startActivity(intentMain);
            Log.i("Content "," Main layout ");
    }

    if(v==btnApp){ 
            // doStuff
            Intent intentApp = new Intent(CurrentActivity.this, 
                                          ThirdActivity.class);

            CurrentActivity.this.startActivity(intentApp);

            Log.i("Content "," App layout ");

    }
   }
  };

Note : and then you should declare all your activities in the manifest .xml file like this :

注意:然后您应该在清单.xml文件中声明所有活动,如下所示:

<activity android:name=".SecondActivity" ></activity>
<activity android:name=".ThirdActivity" ></activity>

EDIT : update this part of Code :) :

编辑:更新这部分代码:):

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);// Add THIS LINE

    setContentView(R.layout.app);

    TextView tv = (TextView) this.findViewById(R.id.thetext);
    tv.setText("App View yo!?\n");
}

NB : check this (Broken link) Tutorial About How To Switch Between Activities.

注意:检查此(断开链接)关于如何在活动之间切换的教程。

#2


8  

I would add an android:onClick to the layout and then change the layout in the activity.

我会在布局中添加一个android:onClick,然后更改活动中的布局。

So in the layout

所以在布局中

<ImageView
(Other things like source etc.)
android:onClick="changelayout"
/>

Then in the activity add the following:

然后在活动中添加以下内容:

public void changelayout(View view){
    setContentView(R.layout.second_layout);
}

#3


2  

I think what you're trying to do should be done with multiple Activities. If you're learning Android, understanding Activities is something you're going to have to tackle. Trying to write a whole app with just one Activity will end up being a lot more difficult. Read this article to get yourself started, then you should end up with something more like this:

我认为你要做的事情应该是多项活动。如果您正在学习Android,那么理解活动是您必须要解决的问题。尝试用一个Activity编写一个完整的应用程序最终会变得更加困难。阅读这篇文章,让自己开始,然后你应该得到更像这样的东西:

View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.DownloadView: 
                // doStuff
                startActivity(new Intent(ThisActivity.this, DownloadActivity.class));
                break;
            case R.id.AppView: 
                // doStuff
                startActivity(new Intent(ThisActivity.this, AppActivity.class));
                break;
        }
    }
};

findViewById(R.id.DownloadView).setOnClickListener(handler);
findViewById(R.id.AppView).setOnClickListener(handler);

#4


2  

You wanted to change the layout at runtime on button click. But that is not possible and as it has been rightly stated above, you need to restart the activity. You will come across a similar problem when u plan on changing the theme based on user's selection but it will not reflect in runtime. You will have to restart the activity.

您希望在按钮单击时在运行时更改布局。但这是不可能的,正如上面已正确说明的那样,您需要重新启动活动。当您计划根据用户的选择更改主题时,您会遇到类似的问题,但它不会在运行时反映出来。您必须重新启动活动。

#5


1  

I know I'm coming to this late, but what the heck.

我知道我迟到了,但到底是什么。

I've got almost the exact same code as Kris, using just one Activity but with 2 different layouts/views, and I want to switch between the layouts at will.

我有几乎与Kris完全相同的代码,只使用一个Activity但有两个不同的布局/视图,我想随意切换布局。

As a test, I added 2 menu options, each one switches the view:

作为测试,我添加了2个菜单选项,每个选项都切换视图:

public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.item1:
                setContentView(R.layout.main);
                return true;
            case R.id.item2:
                setContentView(R.layout.alternate);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Note, I've got one Activity class. This works perfectly. So I have no idea why people are suggesting using different Activities / Intents. Maybe someone can explain why my code works and Kris's didn't.

注意,我有一个Activity类。这非常有效。所以我不知道为什么人们建议使用不同的活动/意图。也许有人可以解释为什么我的代码有效,而Kris没有。

#6


1  

The logcat shows the error, you should call super.onCreate(savedInstanceState) :

logcat显示错误,你应该调用super.onCreate(savedInstanceState):

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //... your code
}

#7


0  

First I would suggest putting a Log in each case of your switch to be sure that your code is being called.

首先,我建议在您的交换机的每种情况下放置一个日志,以确保您的代码被调用。

Then I would check that the layouts are actually different.

然后我会检查布局实际上是不同的。

#8


0  

It is very simple, just do this:

这很简单,只需这样做:

t4.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            launchQuiz2();          // TODO Auto-generated method stub

        }

        private void launchQuiz2() {
            Intent i = new Intent(MainActivity.this, Quiz2.class);
            startActivity(i);
            // TODO Auto-generated method stub

        }

    });

#1


19  

  Button btnDownload = (Button) findViewById(R.id.DownloadView);
  Button btnApp = (Button) findViewById(R.id.AppView);

  btnDownload.setOnClickListener(handler);
  btnApp.setOnClickListener(handler);

  View.OnClickListener handler = new View.OnClickListener(){

  public void onClick(View v) {

    if(v==btnDownload){ 
            // doStuff
            Intent intentMain = new Intent(CurrentActivity.this , 
                                           SecondActivity.class);
            CurrentActivity.this.startActivity(intentMain);
            Log.i("Content "," Main layout ");
    }

    if(v==btnApp){ 
            // doStuff
            Intent intentApp = new Intent(CurrentActivity.this, 
                                          ThirdActivity.class);

            CurrentActivity.this.startActivity(intentApp);

            Log.i("Content "," App layout ");

    }
   }
  };

Note : and then you should declare all your activities in the manifest .xml file like this :

注意:然后您应该在清单.xml文件中声明所有活动,如下所示:

<activity android:name=".SecondActivity" ></activity>
<activity android:name=".ThirdActivity" ></activity>

EDIT : update this part of Code :) :

编辑:更新这部分代码:):

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);// Add THIS LINE

    setContentView(R.layout.app);

    TextView tv = (TextView) this.findViewById(R.id.thetext);
    tv.setText("App View yo!?\n");
}

NB : check this (Broken link) Tutorial About How To Switch Between Activities.

注意:检查此(断开链接)关于如何在活动之间切换的教程。

#2


8  

I would add an android:onClick to the layout and then change the layout in the activity.

我会在布局中添加一个android:onClick,然后更改活动中的布局。

So in the layout

所以在布局中

<ImageView
(Other things like source etc.)
android:onClick="changelayout"
/>

Then in the activity add the following:

然后在活动中添加以下内容:

public void changelayout(View view){
    setContentView(R.layout.second_layout);
}

#3


2  

I think what you're trying to do should be done with multiple Activities. If you're learning Android, understanding Activities is something you're going to have to tackle. Trying to write a whole app with just one Activity will end up being a lot more difficult. Read this article to get yourself started, then you should end up with something more like this:

我认为你要做的事情应该是多项活动。如果您正在学习Android,那么理解活动是您必须要解决的问题。尝试用一个Activity编写一个完整的应用程序最终会变得更加困难。阅读这篇文章,让自己开始,然后你应该得到更像这样的东西:

View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.DownloadView: 
                // doStuff
                startActivity(new Intent(ThisActivity.this, DownloadActivity.class));
                break;
            case R.id.AppView: 
                // doStuff
                startActivity(new Intent(ThisActivity.this, AppActivity.class));
                break;
        }
    }
};

findViewById(R.id.DownloadView).setOnClickListener(handler);
findViewById(R.id.AppView).setOnClickListener(handler);

#4


2  

You wanted to change the layout at runtime on button click. But that is not possible and as it has been rightly stated above, you need to restart the activity. You will come across a similar problem when u plan on changing the theme based on user's selection but it will not reflect in runtime. You will have to restart the activity.

您希望在按钮单击时在运行时更改布局。但这是不可能的,正如上面已正确说明的那样,您需要重新启动活动。当您计划根据用户的选择更改主题时,您会遇到类似的问题,但它不会在运行时反映出来。您必须重新启动活动。

#5


1  

I know I'm coming to this late, but what the heck.

我知道我迟到了,但到底是什么。

I've got almost the exact same code as Kris, using just one Activity but with 2 different layouts/views, and I want to switch between the layouts at will.

我有几乎与Kris完全相同的代码,只使用一个Activity但有两个不同的布局/视图,我想随意切换布局。

As a test, I added 2 menu options, each one switches the view:

作为测试,我添加了2个菜单选项,每个选项都切换视图:

public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.item1:
                setContentView(R.layout.main);
                return true;
            case R.id.item2:
                setContentView(R.layout.alternate);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Note, I've got one Activity class. This works perfectly. So I have no idea why people are suggesting using different Activities / Intents. Maybe someone can explain why my code works and Kris's didn't.

注意,我有一个Activity类。这非常有效。所以我不知道为什么人们建议使用不同的活动/意图。也许有人可以解释为什么我的代码有效,而Kris没有。

#6


1  

The logcat shows the error, you should call super.onCreate(savedInstanceState) :

logcat显示错误,你应该调用super.onCreate(savedInstanceState):

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //... your code
}

#7


0  

First I would suggest putting a Log in each case of your switch to be sure that your code is being called.

首先,我建议在您的交换机的每种情况下放置一个日志,以确保您的代码被调用。

Then I would check that the layouts are actually different.

然后我会检查布局实际上是不同的。

#8


0  

It is very simple, just do this:

这很简单,只需这样做:

t4.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            launchQuiz2();          // TODO Auto-generated method stub

        }

        private void launchQuiz2() {
            Intent i = new Intent(MainActivity.this, Quiz2.class);
            startActivity(i);
            // TODO Auto-generated method stub

        }

    });