2

我正在为 Android 制作一个应用程序,但我很难保存ImageButton单击它时的可见性。我搜索其他主题,但没有找到任何解决方案。

例如,我有一个按钮在单击时变为白色,我想在切换活动或视图时保存它的可见性:

OnClickListener oclFavourite = new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        ImageButton favWhite = (ImageButton) findViewById(R.id.favoriwhite);
        ImageButton favori = (ImageButton) findViewById(R.id.favorigrey);
        favWhite.setVisibility(View.VISIBLE); //The white button I want to save
        favourite.setVisibility(View.INVISIBLE); //The initial button
    }
};
favori.setOnClickListener(oclFavori);

我试图研究SharedPreferences函数,我成功保存TextView但我不知道如何继续ImageButtonor ImageView

4

3 回答 3

3

每当您在可见性和不可见性之间切换时,您都可以切换一个布尔标志。然后将标志保存在SharedPreferences. 像这样的东西

private boolean flag = true;

private void setvisible(){
    flag = true;
    yourView.setVisibility(View.VISIBLE); 
    // save the flag
}

private void setInvisible(){
    flag = false;
    yourView.setVisibility(View.INVISIBLE); 
    // save the flag
}

现在在 oncreate

onCreate(Bundle b){
    .............
    if(flag)
       // make it visible
    else
       // make it invisible
}
于 2013-10-07T10:59:32.573 回答
2

使用 sharedpreference 来保存使用这样的可见性isButtonVisible = false;

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean("BUTTON", false);
edit.commit();

然后使用它加载它

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.getBoolean("BUTTON",true);

并提出一些条件,例如

if(isButtonVisible==true){
    favWhite.setVisibility(View.VISIBLE);
}else{
    favWhite.setVisibility(View.INVISIBLE);
}

问我是否还有其他问题。

于 2013-10-07T11:00:51.487 回答
0

这很简单。只需一个 boolean ,根据您的要求将其切换为 true/false。您可以稍后使用SharedPreferences来获取此值,或者您也可以将布尔值设为静态(但并不总是建议使用静态变量)。也使用一个按钮本身将解决您的目的。只需在单击按钮时切换按钮的背景或可绘制对象。此外,如果您显示两个按钮使用view.GONE而不是View.Invisible使视图消失将完全删除父视图所占用的空间,而不是使其不可见只会消失视图但会保留其空间。现在来解决OnClickListener按钮在您的活动中使用 a的问题。现在boolean isButonclicked =false在您的活动的 oncreate 方法中为您的按钮充气之前,您检查

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
isButonclicked=sp.getBoolean("BUTTON",false);
 buttonSwapping(); 

现在创建一个方法来制作你想要的视图::

public void buttonSwapping(){ 
  if(isButonclicked){

  /////  findViewById() your clicked imageView  and set visibilty to Visible.All the 
         imageView set Visibilty as Gone.     

}else{
  /// findViewById() your normal imageView and set visibilty to Visible.
}
  ////Make sure that the visibilty of all the imageview should be set as gone by 
      default in the XML.   
}

现在在 oclicklistener 中更新 booelan isButonclicked 的值,一旦您单击它并将其放入共享首选项中。

OnClickListener oclFavourite = new OnClickListener() {
@Override
public void onClick(View arg0) {
    if(!isbuttonclicked){
        isbuttonclicked=true;
     }else{
         isbuttonclicked=false;
     }

         Editor edit = sp.edit();
         edit.putBoolean("BUTTON", isButonclicked );
         edit.commit();
    ///////accordingly switch your imageviews.You can have a common method and                
       ////implement  that.

        buttonSwapping();
     }
};

我认为这将解决您的问题。

于 2013-10-07T11:46:30.750 回答