【问题标题】:android - Custom Button (using selector file) not workingandroid - 自定义按钮(使用选择器文件)不起作用
【发布时间】:2021-03-24 15:42:48
【问题描述】:

我正在尝试创建一个“添加到收藏夹”按钮。

问题是除非我一直按下按钮,否则drawable不会改变。一旦我释放按钮,它就会返回到原来的drawable。

我遵循了本教程:https://www.youtube.com/watch?v=Nn4-Vn7qk9k,但得到了不同的结果。

我创建了一个 res/drawable/custom_fav_button.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:drawable="@drawable/ic_baseline_favorite_24"/>

    <item
        android:drawable="@drawable/ic_baseline_favorite_border_24"/>

</selector>

我正在如下所示的活动中使用它。

<Button
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/custom_fav_button"/>

提前致谢!

【问题讨论】:

    标签: java android android-studio button custom-button


    【解决方案1】:

    正如您在视频中看到的那样,您的代码运行良好,可以按照您说的去做。仅在按下时更改。如果您想在点击后更改它,您应该添加您的可绘制 xml

    drawable_button_selector.xml

     <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/drawable_button_selected" android:state_selected="true" />
        <item android:drawable="@drawable/drawable_button_unselected" android:state_selected="false" />
        <item android:drawable="@drawable/drawable_button_unselected" />
    </selector>
    

    drawable_button_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        
        <!-- color of the selected button -->
        <solid
            android:color="@color/purple_200"/>
    
    </shape
    

    drawable_button_unselected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
    
        <!-- unselected button background -->
        <solid
            android:color="@color/gray_dove_three" />
    
        <stroke
            android:color="@color/gray_martini"
            android:width="2dp"/>
    
    
    </shape>
    

    在你的屏幕布局中,你有

     <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click Me"
            android:clickable="true"
            android:background="@drawable/drawable_button_selector"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    

    当然,之后您必须根据您的要求更改按钮状态。例如,只需在点击时切换按钮状态

     private fun initLayout() {
            button.setOnClickListener {
                it.isSelected = !it.isSelected
                Log.d("Click Me", "Button isSelected" + it.isSelected)
                Toast.makeText(this, "Button Clicked and isSelected = " + it.isSelected, Toast.LENGTH_SHORT).show()
    
            }
    

    【讨论】:

    • 这只会在按钮启用时起作用。之后,该按钮将无法使用,因为它未启用。我希望它每次单击时都会更改图片。类似于 Facebook 或 Instagram 上的“赞”按钮。
    • @Maryam Wael 请检查我更新的答案。如果您尝试该代码,您会发现它工作得很好,并且可以实现您想要的。颜色是随机的,我添加了一条 toast 消息以查看它是否确实有效。
    • 完美运行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 2017-07-09
    • 2011-11-07
    • 1970-01-01
    • 2014-12-29
    • 2013-02-26
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多