单击时更改切换按钮的颜色并存储其单击状态

时间:2018-02-04 07:20:43

标签: android button click

在我的应用程序中读取moviesdb。我可以添加任何一个作为收藏夹,也可以不喜欢它们。这是我的问题。我希望按钮在点击时改变颜色,直到我稍后回来并且不喜欢电影。这是我想要改变的代码的一部分。基本上我说如果isCheckedtrue,我说他会把这部电影添加到数据库中作为最爱。如果我再次点击它我会删除它。

b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if(isChecked) {

            ContentValues contentValues = new ContentValues();
            contentValues.put(FavouriteContract.FavouriteEntry.COLUMN_MOVIE_ID, mMovieId);
            contentValues.put(FavouriteContract.FavouriteEntry.COLUMN_MOVIE_POSTER_PATH, mImage);
            contentValues.put(FavouriteContract.FavouriteEntry.COLUMN_MOVIE_TITLE, mTitle);
            contentValues.put(FavouriteContract.FavouriteEntry.COLUMN_MOVIE_OVERVIEW, mOverview);
            contentValues.put(FavouriteContract.FavouriteEntry.COLUMN_MOVIE_RELEASE_DATE, mDate);
            contentValues.put(FavouriteContract.FavouriteEntry.COLUMN_MOVIE_VOTE_AVERAGE, mRating);

            uri = getActivity().getContentResolver().insert(FavouriteContract.FavouriteEntry.CONTENT_URI, contentValues);

            Toast.makeText(getActivity(),"Movie:  " + mTitle + " was added", Toast.LENGTH_SHORT).show();


        }else{

            Uri deleteUri = FavouriteContract.FavouriteEntry.buildMovieUriWithId(mMovieId);

            getActivity().getContentResolver().delete(
                          deleteUri,
                          null,
                          null);

            Toast.makeText(getActivity(),"Movie deleted from favourites ", Toast.LENGTH_SHORT).show();

        }

    }

});

有什么想法吗?

谢谢,

西奥。

更新

我尝试了以前的努力并且几乎修复了它

我创建了一个可绘制的xml文件,

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/ic_star_black"/>
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/ic_star_black" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/ic_star_black"/>
<item android:drawable="@drawable/ic_star_border" />

我把它导入了我的ToggleButton。

<ToggleButton

            android:id="@+id/add_favourite"
            android:layout_width="46dp"
            android:layout_height="46dp"
            android:layout_gravity="right"
            android:layout_marginRight="45dp"
            android:background="@drawable/button_background_selector"
            android:textSize="20sp"
            android:backgroundTint="@color/colorPrimaryDark"
             />

2 个答案:

答案 0 :(得分:0)

要更改切换按钮颜色,您可以使用自定义背景。创建一个drawable。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@color/red"  />
    <item android:state_checked="true" android:drawable="@color/green"  />
</selector>

现在把它放在你的切换上。

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New ToggleButton"
    android:id="@+id/toggleButton"
    android:background="@drawable/bg_toggle"/>

现在要保持切换状态,你可以使用SharedPreference。

答案 1 :(得分:0)

要让ToggleButton作为开/关星工作,你必须这样做:

<ToggleButton
        android:id="@+id/starButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/some_background_you_want"
        android:button="@drawable/star_selector" <!--place your star selector here-->
        android:textOff="@null" <!--Get rid of the text-->
        android:textOn="@null"  <!--Get rid of the text--> />
相关问题