我正在開發一個簡單的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
}
}
看看這個帖子http://stackoverflow.com/questions/6525698/how-to-use- onsavedinstancestate - 例如,請 –