android - 如何在单击时将最喜欢的星形图标设置为ON,在android中再次单击时将其设置为OFF

标签 android

我的具有报价列表的 Activity 如下。

package bank.quotes.famous;
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.CheckBox;

public class QuotesActivity extends FamousQuotesActivity  { 
     @Override 
     public void onCreate(Bundle savedInstanceState)  { 

         super.onCreate(savedInstanceState); 
         setContentView(R.layout.quoteslayout); 

         CheckBox favButton = (CheckBox)findViewById( R.id.favbutton );

我已经尝试在此处添加您的代码但没有成功。 我的 xml 如下所示,>

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="856dp"
        android:background="@android:color/white"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/TableRow03"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25" >

            <RelativeLayout
                android:id="@+id/RelativeLayout08"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/forquotesgrey" >



                <TextView
                    android:id="@+id/TextView22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="@string/albertquote"
                    android:textAppearance="?android:attr/textAppearanceMedium" />


                <TextView
                    android:id="@+id/TextView21"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:text="@string/alberteinstein"
                    android:textAppearance="?android:attr/textAppearanceMedium" />


                <CheckBox
                    android:id="@+id/favbutton"
                    style="?android:attr/starStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/TextView21"
                    android:layout_alignParentRight="true" />

            </RelativeLayout>
        </TableRow>

        <TableRow
            android:id="@+id/TableRow06"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25" >

            <RelativeLayout
                android:id="@+id/RelativeLayout10"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/forquotesgrey" >


                <TextView
                    android:id="@+id/TextView16"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="@string/stevequote"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@android:color/black" />


                <TextView
                    android:id="@+id/TextView15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:text="@string/stevejob"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@android:color/black" />

                <CheckBox
                    android:id="@+id/CheckBox01"
                    style="?android:attr/starStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true" />

            </RelativeLayout>
        </TableRow>

如果用户喜欢某个报价,它应该转到收藏夹 Activity 。如果用户喜欢,收藏按钮也应该始终发光。这就是收藏按钮的工作方式,不是吗?这是我第一次添加收藏夹按钮,所以我不太想这样做。如果你能帮助我,我将非常感激。 谢谢。

最佳答案

如果您想使用有状态的可绘制对象,您会发现为 ToggleButton 或 CheckBox 添加皮肤比为 ImageButton 更容易。

<CheckBox android:id="@+id/star" 
    style="?android:attr/starStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 

关于android - 如何在单击时将最喜欢的星形图标设置为ON,在android中再次单击时将其设置为OFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8930735/

相关文章:

android studio 3.1 警告 : The rule `-keep public class *extends java. lang.annotation.Annotation {

java - 播放响亮声音的自动音量控制

java - Android FusedLocationProviderClient问题

android - 如何在 Kotlin [Dagger-Hilt] 中创建和使用房间数据库

Android studio 多个drawable文件夹重命名

java - Android Studio 报告 "android sdk is parsed incorrectly"

java - 如何在自定义 View 中访问当前 View ?

java - 在java和android中使用final变量的替代方案?

android - 在 RecyclerView 中使用 Activity

Android Build 使用 Proguard 和 Gradle 失败