Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 旋转屏幕时如何保存可拖动的按钮_Java_Android_Button_Screen Rotation - Fatal编程技术网

Java 旋转屏幕时如何保存可拖动的按钮

Java 旋转屏幕时如何保存可拖动的按钮,java,android,button,screen-rotation,Java,Android,Button,Screen Rotation,我刚刚读到: 这: 我的情况是:我有一个按钮背景设置时,按下它是绿色的 button.setBackgroundResource(R.drawable.greenbutton); 如何使用onSaveInstanceState和onRestoreInstanceState存储此信息 我试过:并且成功了,但也许有什么比三个嵌套的if条件过程更好的方法?我的意思是,我必须为4+按钮执行此操作,我认为这需要做很多工作,原因很简单:) 多谢各位 编辑:这是目前为止的代码 package com.exa

我刚刚读到:

这:

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

button.setBackgroundResource(R.drawable.greenbutton);
如何使用onSaveInstanceStateonRestoreInstanceState存储此信息

我试过:并且成功了,但也许有什么比三个嵌套的if条件过程更好的方法?我的意思是,我必须为4+按钮执行此操作,我认为这需要做很多工作,原因很简单:)

多谢各位

编辑:这是目前为止的代码

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.
                }
            }
        }
    }
}

在您的活动上也设置一个变量,然后保存该变量并将其与您的状态一起还原

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。

您有几个选项:

  • 您可以在AndroidManifest文件中设置特定标志:
  • 默认情况下,它不起作用,因为当您更改方向时,将再次调用onCreate,并重新绘制视图。但是,如果您设置了这些标志,并且正在为横向模式使用不同的布局,则通过添加这些参数,横向模式的布局将不会被调用,因为onCreate不会被第二次调用

  • 您可以通过以下方式保存int资源:

    int colorFirst=answerOne.getHighlightColor();
    savedInstanceState.putInt(“键”,colorFirst)


  • 在哪里设置默认颜色?添加更多代码添加了我正在处理的Java代码Ank you,但这就是我到目前为止所做的,但我问是否有其他更有效的方法来传递变量。我尝试了Resources colorFirst=answerOne.getResources();但是,我不能调用savedInstanceState.putResources。与Drawable colorFirst=answerOne.getBackground()相同;int colorFirst=answerOne.getHighlightColor();我会试试这个,因为我认为这是一个更好的方法,然后做一个如果条件!