【问题标题】:Android screen orientation changeAndroid屏幕方向更改
【发布时间】:2013-11-30 23:19:02
【问题描述】:

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

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);

    }

    }    

【问题讨论】:

  • 你在哪里改变颜色?
  • 我有一个开关... case R.id.button1: myLayout.setBackgroundColor(Color.WHITE);
  • 当我按下按钮时在屏幕上
  • 我的意思是代码在哪里?在 onCreate() 或其他地方设置的 onClickListener 中?

标签: android


【解决方案1】:

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

一种方法是使活动不会在方向改变时重新启动。这样做的方法如下:

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

另一种方法是使用onSaveInstanceState(Bundle savedInstanceState),就像Rastikan 那样。

另一种方法是让活动自行重新启动,然后在活动重新启动时使用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
    }
}

【讨论】:

  • 我正在添加您所说但不起作用的行...
  • @user2958480 将该行放在您的 AndroidManifest.xml 中。抱歉,我最初忘记提及了。
  • 还是不行...
  • @user2958480 “不工作”是什么意思?每当你旋转它时它会改变颜色吗?用onSavedInstanceStateonResume() 以及其他任何东西取出所有其他东西。此外,请确保您将代码行放入正确的活动中。您的代码中有多个活动吗?
  • 我已经添加了清单的一部分......我的代码看起来和我发布的一样......
【解决方案2】:

这是一个简短/替代版本,假设有某种视图 (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)
{
}

【讨论】:

  • “或者你可以用廉价愚蠢的方式来做”。这不是廉价或愚蠢的方式。这可以防止活动被破坏,你的第一个方法没有。两者各有利弊。
  • 你说得对,迈克,我的英语不太好。无论如何,我经常不得不以这种方式实现一些活动(实际上添加了转换条件 'orientation|keyboardHidden|keyboard|touchscreen|screenLayout|screenSize|uiMode'),因为我们不希望活动的 BT 连接被中断......至少直到有人有一两个小时将蓝牙代码更新为服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 2014-02-08
相关资源
最近更新 更多