方向屏幕问题

时间:2016-11-29 21:25:18

标签: android orientation

我正在开发一个简单的Android手电筒应用程序,当我旋转屏幕时,我想保存以前的状态 我有几个按钮可以改变布局的颜色,当我触摸屏幕按钮时会出现并消失。但正如我告诉我需要保持最后状态,我知道我应该这样做 的onSaveInstanceState 和onRestoreInstanceState但不幸的是我不知道如何实现代码。这是我写的代码:

// import com.uncocoder.course.app.startup4.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class FlashlightActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button Black = (Button) findViewById(R.id.Black);
        final Button White = (Button) findViewById(R.id.White);
        final Button Yellow = (Button) findViewById(R.id.Yellow);
        final Button Red = (Button) findViewById(R.id.Red);
        final Button Green = (Button) findViewById(R.id.Green);
        final LinearLayout backLayout = (LinearLayout) findViewById(R.id.backLayout);
        final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);

        Black.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                backLayout.setBackgroundColor(Color.parseColor("#000000"));
                Toast.makeText(FlashlightActivity.this, "Black Color Set", Toast.LENGTH_SHORT).show();

            }
        }); //end black

        White.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                backLayout.setBackgroundColor(Color.parseColor("#ffffff"));
                Toast.makeText(FlashlightActivity.this, "White Color Set", Toast.LENGTH_SHORT).show();

            }
        }); //end white

        Yellow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                backLayout.setBackgroundColor(Color.parseColor("#ffff00"));
                Toast.makeText(FlashlightActivity.this, "Yellow Color Set", Toast.LENGTH_SHORT).show();

            }
        }); //end yellow

        Red.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                backLayout.setBackgroundColor(Color.parseColor("#ff0000"));
                Toast.makeText(FlashlightActivity.this, "Red Color Set", Toast.LENGTH_SHORT).show();

            }
        }); //end red

        Green.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                backLayout.setBackgroundColor(Color.parseColor("#a4c639"));
                Toast.makeText(FlashlightActivity.this, "Green Color Set", Toast.LENGTH_SHORT).show();

            }
        }); //end green

        backLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (Black.getVisibility() == View.GONE) {
                    Black.setVisibility(View.VISIBLE);
                } // end if
                else {
                    Black.setVisibility(View.GONE);
                }// end else
                if (White.getVisibility() == View.GONE) {
                    White.setVisibility(View.VISIBLE);
                } // end if
                else {
                    White.setVisibility(View.GONE);
                }// end else
                if (Yellow.getVisibility() == View.GONE) {
                    Yellow.setVisibility(View.VISIBLE);
                } // end if
                else {
                    Yellow.setVisibility(View.GONE);
                }// end else

                if (Red.getVisibility() == View.GONE) {
                    Red.setVisibility(View.VISIBLE);
                } // end if
                else {
                    Red.setVisibility(View.GONE);
                }// end else

                if (Green.getVisibility() == View.GONE) {
                    Green.setVisibility(View.VISIBLE);
                } // end if
                else {
                    Green.setVisibility(View.GONE);
                }// end else

            }// end public void
        }); // end seton

    }
} 

2 个答案:

答案 0 :(得分:0)

你必须覆盖" onSaveInstanceState(Bundle outState)"进入课堂。 将数据作为包存储在此方法中,并在onCreateView方法中添加相同的数据,或者可以阻止oriantation。

为此..

清单文件

.
.
<activity android:name=".ExampleActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" />
.
.
.

https://stackoverflow.com/a/582585/7227819

答案 1 :(得分:0)

@Override
public void onSaveInstanceState(Bundle outState) {
   //put the status of the button which is enabled or not by boolean value
   outState.putBoolean("buttonstatus",true);
   super.onSaveInstanceState(outState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        boolean message = savedInstanceState.getBoolean("buttonstatus");
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
}