2013-10-07 71 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(); 
    } 
}; 

我認爲這將解決您的問題。