如何保存结果?

时间:2013-03-21 08:36:12

标签: android button android-activity imageview

我是Android编程的新手。

我有一个小问题。当我单击ImageView时,我使ImageView不可见并将Button设置为可见。我的问题是你如何保存这个?

例如,我单击ImageView,按钮显示,ImageView消失。然后我退出应用程序并返回相同的活动,我希望该按钮保留在那里。

我该怎么做?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class Flightplan extends Activity {

Button a;
ImageView b; 
private boolean isVisible;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.flightplan);

b = (ImageView) findViewById(R.id.plus);
View v1 = findViewById(R.id.test); 
v1.setVisibility(View.GONE);
b.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

            View v2 = findViewById(R.id.test); 
            v2.setVisibility(View.VISIBLE);

            View v3 = findViewById(R.id.plus);
            v3.setVisibility(View.GONE); 
    }
});

} 
}

谢谢!

2 个答案:

答案 0 :(得分:1)

将状态保存到共享首选项中,然后在onCreate中检查该首选项。 this question的答案非常清楚如何使用共享偏好设置,但您可以使用store and get a Boolean代替Long。

答案 1 :(得分:0)

试试这个:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.content.SharedPreferences;

public class Flightplan extends Activity {

  Button a;
  ImageView b;
  private boolean isVisible;
  SharedPreferences prefs = null;

  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.flightplan);
prefs = this.getSharedPreferences(
    "com.your.app", Context.MODE_PRIVATE);

boolean imageVisible = prefs.getBoolean("imageVisible", false); 

b = (ImageView) findViewById(R.id.plus);

View v1 = findViewById(R.id.test);
if (!imageVisible){
  v1.setVisibility(View.GONE);
  v3.setVisibility(View.VISIBLE);
}else{
  v1.setVisibility(View.VISIBLE);
  v3.setVisibility(View.GONE);
}

b.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub

    View v2 = findViewById(R.id.test);
    v2.setVisibility(View.VISIBLE);

    View v3 = findViewById(R.id.plus);
    v3.setVisibility(View.GONE);

    prefs.edit().putBoolean("imageVisible", true).commit();
  }
});

v1.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub

    View v2 = findViewById(R.id.test);
    v2.setVisibility(View.GONE);

    View v3 = findViewById(R.id.plus);
    v3.setVisibility(View.VISIBLE);

    prefs.edit().putBoolean("imageVisible", false).commit();
  }
});


 }

 }

我用文本编辑器写了这个。可能是有一些编译器错误......