Android屏幕方向改变

时间:2013-11-30 23:19:03

标签: android

我正在制作一个程序,当我按下一个按钮时,这会改变背景颜色,但是我遇到了问题,当我改变屏幕方向时,这又改变了预定义的颜色,我不知道如何解决这个问题......这是我的代码。

public class MainActivity extends Activity implements OnClickListener {

    LinearLayout myLayout;
    LinearLayout myLayout2;

    // Declare UI elements
    private Button firstButton, secondButton, thirdButton, fourthButton, fifthButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // Our only layout for this app is main.xml

        myLayout = (LinearLayout) findViewById(R.id.myLayout);
        myLayout2 = (LinearLayout) findViewById(R.id.myLayout2);

        // Initialize the UI components

        firstButton = (Button) findViewById(R.id.button1); 
        // When we creating a button and if we expect that to use for event handling we have to set the listener
        firstButton.setOnClickListener(this);
        secondButton = (Button) findViewById(R.id.button2);
        secondButton.setOnClickListener(this);
        thirdButton = (Button) findViewById(R.id.button3);
        thirdButton.setOnClickListener(this);
        fourthButton = (Button) findViewById(R.id.button4);
        fourthButton.setOnClickListener(this);
        fifthButton = (Button) findViewById(R.id.button5);
        fifthButton.setOnClickListener(this);

    }

    }    

2 个答案:

答案 0 :(得分:3)

问题是当方向改变时,活动会重新开始。有多种方法可以解决这个问题。

一种方法是在更改方向时使活动不会重新启动。以下是如何做到这一点:

android:configChanges="orientation|screenSize"添加到您<activity中的AndroidManifest.xml标记中,如下所示:

<activity
    android:name="UserIdActivity"
    android:label="@string/app_name"
    android:configChanges="orientation|screenSize" />

Rastikan's answer稍有不同,但他的做法不适用于API 12之后的任何API(sourcethis too)。我的方法是,您实际上不需要在活动类中调用onConfiguationChanged

另一种方法是使用像Rastikan这样的onSaveInstanceState(Bundle savedInstanceState)

另一种方法是让活动重新启动,然后在重新启动活动时使用onResume()方法更改背景颜色(如有必要)。像这样:

public boolean changeColor = false;

// set changeColor to be true whenever you change the background colour

public void onResume()
{
    if (changeColor) {
        // change the background color
    }
}

答案 1 :(得分:-1)

这是一个短/备用版本,假定某种视图(view1)。

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private static final int DEFAULT_COLOR = Color.WHITE;

    private int myColor;
    // Assuming a 'view' of some sort
    private View myView;

    // Declare UI elements
    private Button firstButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Our only layout for this app
                                                // is main.xml

        myView      = (View)   findViewById( R.id.view1   );
        firstButton = (Button) findViewById( R.id.button1 );

        Log.i("MainActivity", "onCreate"
                + ((null == savedInstanceState) ? "(null)"
                        : "(savedInstanceState)"));

        if (savedInstanceState != null) {
            myColor = savedInstanceState.getInt("COLOR");
        } else {
            myColor = DEFAULT_COLOR;
        }

        myView.setBackgroundColor(myColor);

        firstButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (myColor == Color.WHITE)
                    myColor = Color.BLACK;
                else if (myColor == Color.BLACK)
                    myColor = Color.WHITE;

                myView.setBackgroundColor(myColor);
            }

        });
    }

    @Override
    // this method is called before android trashes and recreates your activity
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("COLOR", myColor);
    }
}

或者你可以用廉价笨笨的方式做到:锁定屏幕。将“android:configChanges”属性添加到您的活动中。

 <activity
    android:name="UserIdActivity"
    android:label="@string/app_name"
    android:configChanges="orientation" />

实现'onConfigurationChanged'回调以无所作为

public void onConfigurationChanged ( Configuration newConfig)
{
}