收藏夹按钮Android

时间:2016-01-24 19:22:16

标签: java android favorites

我正在制作一个食谱应用程序,并希望创建一个最喜欢的按钮,用户将点击灰色的星形按钮,它将变为一个黄色的星形按钮(我有一个灰色的星星图片和一个黄色星号),一旦您点击主屏幕上的收藏夹链接,所选的收藏夹将显示在列表中

我在布局中有以下代码

 <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"/>

任何人都可以帮我解释如何编写代码,这样当我点击星标时它会变成黄色并添加到收藏列表中

更新

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>

2 个答案:

答案 0 :(得分:3)

使用代码更改背景按钮 您也可以在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);
}

答案 1 :(得分:2)

在XML中使用ToggleButton:

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