【问题标题】:Change background color and text color of a button when pressed, should swap background color and text color if again pressed按下时更改按钮的背景颜色和文本颜色,如果再次按下应交换背景颜色和文本颜色
【发布时间】:2017-08-31 06:57:28
【问题描述】:

我想在按下按钮时更改背景颜色和文本颜色(它应该保持状态,直到再次按下按钮)。此外,如果再次按下,我需要按钮的旧状态。 请大家帮帮我

这是截图(正常状态)

一些按钮按下状态

这是我尝试过的代码

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/beco_white"
        android:layout_marginTop="@dimen/dp10"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/button_oberon_mall"
                android:layout_width="92dp"
                android:layout_height="30dp"
                android:layout_marginLeft="@dimen/dp10"
                android:minWidth="92dp">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/blue_rounded_corner_new"
                    android:text="Oberon Mall"
                    android:fontFamily="sans-serif-regular"
                    android:textAllCaps="false"
                    android:textColor="@color/beco_black"
                    android:textSize="13sp"/>

            </FrameLayout>
        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/button_centre_square_mall"
                android:layout_width="134dp"
                android:layout_height="30dp"
                android:layout_marginLeft="@dimen/dp10"
                android:layout_marginRight="@dimen/dp10"
                android:minWidth="134dp">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/blue_rounded_corner_new"
                        android:text="Centre Square Mall"
                        android:fontFamily="sans-serif-regular"
                        android:textAllCaps="false"
                        android:textColor="@color/beco_black"
                        android:textSize="13sp"/>
            </FrameLayout>
        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/button_lulu_mall"
                android:layout_width="81dp"
                android:layout_height="30dp">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/blue_rounded_corner_new"
                    android:text="Lulu Mall"
                    android:fontFamily="sans-serif-regular"
                    android:textAllCaps="false"
                    android:textColor="@color/beco_black"
                    android:textSize="13sp"/>
            </FrameLayout>
        </FrameLayout>

    </LinearLayout>

blue_round_corner_new.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="false">
<shape android:shape="rectangle">
    <!--apply button background transparent, full opacity-->
    <solid android:color="#dedfe0"/>
    <corners android:radius="15dp"/>
</shape>
</item >
<item
    android:state_pressed="true">
    <shape android:shape="rectangle">
        <!--apply button background transparent, full opacity-->
        <solid android:color="#4990d0"/>
        <corners android:radius="15dp"/>
    </shape>
</item>

在此代码中,按下时背景颜色只会改变一段时间(不保持状态)

任何人都可以帮助我。对不起我的英语

【问题讨论】:

  • 如果您已将背景设置为该颜色,则必须通过代码设置背景颜色。选择器无法做到这一点
  • “一会儿”是什么意思?该按钮不能神奇地回到旧状态。它要么在重新渲染或发生其他事情时这样做。
  • 最好使用外部库来解决这个问题检查这个链接android-arsenal.com/details/1/2143 有帮助吗请告诉我

标签: android button


【解决方案1】:

我认为最好的选择是为每个按钮实现一个 onTouchListener。

button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_DOWN:
                        break;
                }

                return false;
            }
        });

当您获得 ACTION_UP 或 ACTION_DOWN 时,您只需更改背景

【讨论】:

  • 我有 19 个按钮,所以?
  • 如果您有这么多按钮,您是否使用适配器为按钮充气?还是这些按钮在代码中是静态的?
  • @David Luque 我是初学者。我现在正在静态使用它。
  • 因此您必须将此侦听器添加到每个按钮。我建议您使用 ListView 并在适配器中仅使用一个侦听器
  • 让我试试这个!
【解决方案2】:

您最好使用外部库来解决此问题。以下库将帮助您解决问题

CheckableButton

AndroidCheckableButton

【讨论】:

    【解决方案3】:

    有两种方式可以在屏幕上显示 XML(您已经使用过)或以编程方式。 尝试将 blue_round corner.xml 放在程序调用中,如果你想长时间保留(我认为它就像你有多个屏幕并且切换屏幕保持按钮的按下状态)然后在 onResume() 函数中调用相同的保留它。

    我认为它会起作用,如果您需要更多帮助,请提供 cmets

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 2018-12-25
      • 2013-10-24
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多