0

我是android编程的菜鸟。

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

例如,我单击 ImageView,Button 出现,而​​ 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); 
    }
});

} 
}

谢谢!

4

2 回答 2

1

将状态保存到共享首选项中,然后在onCreate中检查该首选项。关于如何使用 Shared Preferences,这个问题的答案非常清楚,但是您可以存储并获取 Boolean来代替 Long 。

于 2013-03-21T08:40:40.533 回答
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();
  }
});


 }

 }

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

于 2013-03-21T08:54:05.613 回答