如何设置按钮高亮显示状态时pressed直到我preSS另一个呢? [英] How set button highlighted state when pressed till I press another one?

查看:191
本文介绍了如何设置按钮高亮显示状态时pressed直到我preSS另一个呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有这样的UI要求 -

Hi I have UI Requirement like this-

有四个按钮:流行 AZ 附近类别

它们中的任何一个可以是一次pssed $ P $。假设如果流行按钮为pressed,那么按钮的其余部分将看起来像没有pressed。

Any one of them can be pressed at a time. Suppose if Popular button is pressed, then the rest of the buttons will seem like not pressed.

现在我希望它保持下去,直到我preSS剩下的三个按钮之一。
一旦这三个中的任何一个是pressed特定的按钮,应该强调和按钮的其余部分应该是正常的。

Now I want it to stay like this until I press one of the remaining three buttons. Once any one of the three is pressed that particular button should be highlighted and rest of the buttons should be normal.

推荐答案

有关,你应该使用单选按钮,你需要有一个选择器来定义不同的可绘制不同的状态。这里是你可以有选择。将其命名为 button_selector 并把它放到绘制文件夹。你需要为你的按钮两个不同的可绘制为正常 pressed 状态。将其命名为按钮 button_selected

For that you should use Radio Buttons and you need to have a selector to define different drawables for different states. Here is how you can have selector. name it as button_selector and put it to the drawable folder. And you need to have two different drawables for your buttons as normal and pressed states. Name it as button and button_selected.

<?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/button_selected"></item>

    <item android:state_checked="true" android:drawable="@drawable/button_selected"></item>
    <item android:drawable="@drawable/button"></item>
</selector>

对于单选按钮;

As for the Radio Button ;

<RadioGroup
    android:id="@+id/radio_group_card_selector"
    android:layout_width="wrap_content"
    android:layout_height="28dp"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio_group_highlights"
        android:layout_width="69dp"
        android:layout_height="28dp"
        android:layout_gravity="left"
        android:background="@drawable/button_selector"
        android:button="@null"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/Highlights"
        android:textColor="#21596c"
        android:textSize="10sp"
        android:textStyle="normal" >
    </RadioButton>

    <RadioButton
        android:id="@+id/radio_group_products"
        android:layout_width="69dp"
        android:layout_height="28dp"
        android:layout_gravity="left"
        android:background="@drawable/button_selector"
        android:button="@null"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/All"
        android:textColor="#21596c"
        android:textSize="10sp"
        android:textStyle="normal" >
    </RadioButton>

    <RadioButton
        android:id="@+id/radio_group_mycards"
        android:layout_width="69dp"
        android:layout_height="28dp"
        android:background="@drawable/button_selector"
        android:button="@null"
        android:checked="true"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/My_Cards"
        android:textColor="#21596c"
        android:textSize="10sp"
        android:textStyle="normal" >
    </RadioButton>
</RadioGroup>

因此,在这样你就不需要处理在运行时,code。

编辑:如果您希望只使用颜色来代替绘项目,那么你可以使用下面的code为了更好地理解它是如何工作

EDIT : If you want to use only colors instead of drawables, then you can use the code below for better understanding how it works.

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

<item android:state_pressed="true"> <!-- pressed -->
    <shape>
        <gradient
            android:startColor="#058CF5"
            android:centerColor="#0273ED"
            android:centerY="0.75"
            android:endColor="#015DE6"
            android:angle="270" />
    </shape>    
</item>

<item android:state_checked="true"> <!-- focused -->
    <shape>
        <gradient
            android:startColor="#058CF5"
            android:centerColor="#0273ED"
            android:centerY="0.75"
            android:endColor="#015DE6"
            android:angle="270" />
    </shape>    
</item>

<item>      <!-- default -->
    <shape>
        <solid android:color="#E6E6E6" />
    </shape>
</item>

这篇关于如何设置按钮高亮显示状态时pressed直到我preSS另一个呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆