【问题标题】:How to save a Button drawable when rotating screen旋转屏幕时如何保存可绘制的按钮
【发布时间】:2017-12-04 23:05:38
【问题描述】:

我刚读到这个:Saving Android Activity state using Save Instance State

这个:Android - Open resource from @drawable String

我的情况是:我有一个按钮背景设置为按下时为绿色

button.setBackgroundResource(R.drawable.greenbutton);

如何使用 onSaveInstanceStateonRestoreInstanceState 存储这些信息?

我试过了:How to maintain a button style and click state after screen rotation? 并且工作了,但也许有比三嵌套如果条件程序更好的方法?我的意思是我必须为 4+ Button 做这个,我认为这只是一个简单的原因:)

谢谢

编辑:这是目前的代码

package com.example.android.testbutton;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.change);
    }

    Button button;
    Boolean click;

    public void changeColor(View view) {
        click = true;
        button.setBackgroundResource(R.drawable.greenbutton);
    }


    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        // Save UI state changes to the savedInstanceState.
        // This bundle will be passed to onCreate if the process is
        // killed and restarted.

        savedInstanceState.putBoolean("buttonClicked", click);
        // etc.

        super.onSaveInstanceState(savedInstanceState);
    }

//onRestoreInstanceState

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);

        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.

        Boolean firstAnswer = savedInstanceState.getBoolean("buttonClicked");
        {
            if (savedInstanceState != null) {
                if (savedInstanceState.containsKey("buttonClicked")) {
                    if (savedInstanceState.getBoolean("buttonClicked"))
                        button.setBackgroundResource(R.drawable.greenbutton);

                    //some codes to make the button becomes clicked.
                }
            }
        }
    }
}

【问题讨论】:

  • 你在哪里设置默认颜色?放更多代码
  • 添加了我正在处理的 Java 代码

标签: java android button screen-rotation


【解决方案1】:

也为您的活动设置一个变量,然后保存它并使用您的状态恢复它。

public class MyActivity extends Activity {

public static final String KEY_BUTTON_IS_GREEN = "isGreen";
boolean buttonIsGreen;

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(KEY_BUTTON_IS_GREEN, buttonIsGreen);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    buttonIsGreen = savedInstanceState.getBoolean(KEY_BUTTON_IS_GREEN, false);
    if (buttonIsGreen){
        // find the button and set it green.
    }
}

您需要添加将按钮设置为绿色的方法,如果按钮为绿色,还需要设置您的变量 = true。

【讨论】:

  • 谢谢,但这就是我到目前为止所做的,但我在问是否有其他更有效的方法来传递变量。我试过 Resources colorFirst = answerOne.getResources(); 但是,我不能调用 savedInstanceState.putResourcesDrawable colorFirst = answerOne.getBackground();int colorFirst = answerOne.getHighlightColor(); 相同
【解决方案2】:

你有几个选择:

  1. 您可以在 AndroidManifest 文件中设置特定的标志:

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>

默认情况下它不起作用,因为当您更改方向时,将再次调用 onCreate 并重绘您的视图。但是如果你设置了这些标志并且你使用不同的横向模式布局,通过添加这些参数,横向模式的布局将不会被调用,因为 onCreate 不会被第二次调用。

  1. 您可以通过以下方式节省 int 资源:

    int colorFirst = answerOne.getHighlightColor(); savedInstanceState.putInt("key", colorFirst);

【讨论】:

  • 我会试试这个,因为我认为这是一个比 if 条件更好的方法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多