2016-01-24 89 views
1

我正在做一個食譜應用程序,並希望創建一個最喜歡的按鈕,用戶將點擊灰色的星形按鈕,它會變成一個黃色的星形按鈕(我有一個灰色的星星圖片和黃星),所選擇的收藏夾然後將列表中的,一旦你點擊收藏夾顯示在主屏幕上鍊接最喜歡的按鈕Android

我有一個佈局下面的代碼

<ImageButton android:id="@+id/favouritebtn" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:src="@drawable/staroff" 
     android:background="#00ffffff" 
     android:onClick="onToggleStar" 
     android:clickable="true"/> 

誰能幫助我如何編寫代碼,以便當我點擊這個明星時,它會變成黃色並添加到收藏列表

UPDATE

FavouriteBtn.java

import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageButton; 

public class FavouriteBtn extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.recipe); 

    final ImageButton imgButton =(ImageButton)findViewById(R.id.favbtn); 
    imgButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      boolean isFavourite = readState(); 

      if (isFavourite) { 
       imgButton.setBackgroundResource(R.drawable.staroff); 
       isFavourite = false; 
       saveState(isFavourite); 

      } else { 
       imgButton.setBackgroundResource(R.drawable.staron); 
       isFavourite = true; 
       saveState(isFavourite); 

      } 

     } 
    }); 

} 

private void saveState(boolean isFavourite) { 
    SharedPreferences aSharedPreferences = this.getSharedPreferences(
      "Favourite", Context.MODE_PRIVATE); 
    SharedPreferences.Editor aSharedPreferencesEdit = aSharedPreferences 
      .edit(); 
    aSharedPreferencesEdit.putBoolean("State", isFavourite); 
    aSharedPreferencesEdit.commit(); 
} 

private boolean readState() { 
    SharedPreferences aSharedPreferences = this.getSharedPreferences(
      "Favourite", Context.MODE_PRIVATE); 
    return aSharedPreferences.getBoolean("State", true); 
} 

} 

recipe.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/jbsbackground2" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/iv_detail" 
    android:layout_width="fill_parent" 
    android:layout_height="150dp" 
    android:scaleType="centerCrop" 
    android:src="@drawable/barbecuedporkribs" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="10dp" 
    android:layout_below="@+id/iv_detail" 
    android:background="#3D3C3A" /> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button1" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/iv_detail" 
      android:layout_marginTop="5dp" 
      android:text="Recipe" 
      android:layout_toLeftOf="@id/favourites" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/tvName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/textView2" 
      android:layout_marginTop="2dp" 
      android:text="TextView" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textStyle="italic" /> 

     <TextView 
      android:id="@+id/tvFavourite" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Add To Favourites (click star)" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textStyle="bold" 
      android:layout_above="@+id/tvName" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" /> 


     <ImageButton 
      android:id="@+id/favbtn" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:src="@drawable/staroff" 
      android:background="#00ffffff" 
      android:onClick="onClick" 
      android:clickable="true"/> 

     <TextView 
      android:id="@+id/tvTD" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Ingredients" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textStyle="bold" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

     <TextView 
      android:id="@+id/tvIngredients" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/tvTD" 
      android:layout_marginTop="2dp" 
      android:text="TextView" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textStyle="italic" /> 

     <TextView 
      android:id="@+id/tvK" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/tvIngredients" 
      android:layout_marginTop="5dp" 
      android:text="Preparation" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/tvPreparation" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/tvK" 
      android:layout_marginTop="2dp" 
      android:text="TextView" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textStyle="italic" /> 
</LinearLayout> 
</ScrollView> 

+0

你嘗試過什麼?如果您搜索如何判斷用戶何時單擊按鈕以及如何更改圖像,則應該找到一些非常簡單的答案。 –

+0

OP,在這裏使用android評級欄示例 - http://www.mkyong.com/android/android-rating-bar-example/並將其轉換爲您的需求(在您的情況下,收藏夾) –

回答

1

使用切換按鈕在你的XML:

<ToggleButton 
    android:layout_width="180dp" 
    android:layout_height="180dp" 
    android:id="@+id/myToggleButton" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:textOn="" 
    android:textOff=""/> 

而在你的活動:

ToggleButton toggleButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    toggleButton = (ToggleButton) findViewById(R.id.myToggleButton); 
    toggleButton.setChecked(false); 
    toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.img_star_grey)); 
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) 
       toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.img_star_yellow)); 
      else 
       toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.img_star_grey)); 
     } 
    }); 
} 
+0

感謝您的回答,當我使用此代碼時,我得到關於API級別21的getDrawable錯誤當前最小值爲8,所以我添加了getResource()。在每個getDrawable的前面,但現在應用程序崩潰,你知道如何解決這個問題 – john

+0

@john是的。我更新了我的答案。 –

+0

嘿謝謝你回到我身邊,我改變了代碼,以更新你的答案,但現在應用程序崩潰時,我點擊食譜TextView打開食譜列表,我得到以下錯誤消息「java.lang.NullPointerException :嘗試在空對象引用上調用虛擬方法'void android.widget.ToggleButton.setChecked(boolean)'我會將MainActivity.java添加到我的問題中以更好地查看整個代碼 – john

1

用於更改背景按鈕 代碼也可以保存在SharedPreferences狀態按鈕

favouritebtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      boolean isFavourite = readStae(); 

      if (isFavourite) { 
       favouritebtn.setBackgroundResource(R.drawable.imgStarGrey); 
       isFavourite = false; 
       saveStae(isFavourite); 

      } else { 
       favouritebtn.setBackgroundResource(R.drawable.imgStaryellow); 
       isFavourite = true; 
       saveStae(isFavourite); 

      } 

     } 
    }); 

還添加方法

private void saveStae(boolean isFavourite) { 
    SharedPreferences aSharedPreferenes = this.getSharedPreferences(
      "Favourite", Context.MODE_PRIVATE); 
    SharedPreferences.Editor aSharedPreferenesEdit = aSharedPreferenes 
      .edit(); 
    aSharedPreferenesEdit.putBoolean("State", isFavourite); 
    aSharedPreferenesEdit.commit(); 
} 

private boolean readStae() { 
    SharedPreferences aSharedPreferenes = this.getSharedPreferences(
      "Favourite", Context.MODE_PRIVATE); 
    return aSharedPreferenes.getBoolean("State", true); 
} 
+0

嗨,當我使用您的代碼我得到以下錯誤「java.lang.IllegalStateException:無法找到方法onClick(視圖)在父或祖先上下文的android:onClick屬性定義在視圖類android.widget.ImageButton與id'favbtn'」我已經更新我的問題,並添加我的java和xml文件,如果你可以看看,並告訴我在哪裏出錯了,請 – john