2013-10-07 145 views
2

我正在为Android设备制作应用程序,并且点击时难以保存ImageButton的可见性。我搜索其他主题,但我还没有找到任何解决方案。保存图片按钮可见性[Android]

例如,我有一个按钮被点击时,它变成白色了,我要保存它的知名度,当我切换活动或观点:我想学习SharedPreferences功能

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); 

,我成功的保存TextView,但我不知道如何处理ImageButtonImageView

+0

保存其可见性?你究竟意味着什么,你可以详细说明 – Goofy

+0

,因为你必须使用java的单例方法。 –

+0

为什么你使用两个按钮? – Swetank

回答

2

无论何时在可见性和不可见性之间切换,您都可以切换布尔标志。然后将标志保存在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 
} 
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); 
} 

问我,如果您有任何其他问题。

0

这很简单。只要有一个布尔值,根据您的要求将其切换为真/假。你可以使用SharedPreferences以后得到这个值,或者你也可以使用布尔静态(但是并不是所有的时候都使用静态变量)。另外使用一个按钮本身将解决你的目的。只需在点击按钮时切换背景或可绘制按钮。另外,如果您显示两个按钮,则使用view.GONE而不是View.Invisible,因为视图消失将完全删除父视图中占用的空间,而不是使其无法看到,只会消失视图,但将保留其空间。现在来的问题在按钮的OnClickListener在活动考核的onCreate方法夸大你的按钮之前,请使用您activity.Now一个boolean isButonclicked =false您检查

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一旦你点击它,并把它放在共享PREF更新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(); 
    } 
}; 

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