【问题标题】:android button selector安卓按钮选择器
【发布时间】:2012-12-11 00:37:06
【问题描述】:

这是一个按钮选择器,正常时显示为红色,按下时显示为灰色。

我想问一下如何进一步直接修改代码,以便在按下时文本大小和颜色也可以改变?非常感谢!

<item android:state_pressed="true" >         
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="@color/grey"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>    
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="#FF6699"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>
</item>

【问题讨论】:

    标签: android selector


    【解决方案1】:

    您只需在布局文件中设置selectorbutton

    <Button
         android:id="@+id/button1"
         android:background="@drawable/selector_xml_name"
         android:layout_width="200dp"
         android:layout_height="126dp"
         android:text="Hello" />
    

    完成了。

    编辑

    以下是button_effect.xml 目录下的button_effect.xml 文件

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

    在这里,您可以看到有 3 个可绘制对象,您只需要将这个 button_effect 样式放置到您的 button 中,就像我在上面写的那样。您只需将selector_xml_name 替换为button_effect

    【讨论】:

    • 能否请您详细说明如何设置按下和非按下状态? (带有上述所有组件,例如圆形颜色,笔划等)。非常感谢!
    • 实现选择器的最佳方法是使用 xml 参考这个 blazin.in/2016/03/how-to-use-selectors-for-botton.html 我按照这个实现及其工作
    • 嗨,我知道你写这篇文章已经有一段时间了,但也许你会知道我错过了什么。我使用了您的代码,并且我的按钮始终为绿色,然后对于按下 =true 并选择 =true 它正在将可绘制更改为灰色,但是在打开其他活动按钮之前的秒数具有默认的 android 样式。知道我错过了什么状态吗?
    【解决方案2】:

    您无法使用state list drawable 更改文本大小。要更改文本颜色和文本大小,请执行以下操作:

    文字颜色

    要更改文本颜色,您可以创建color state list resource。它将是位于res/color/ 目录中的单独资源。在布局 xml 中,您必须将其设置为 android:textColor 属性的值。然后颜色选择器将包含如下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="@color/text_pressed" />
        <item android:color="@color/text_normal" />
    </selector>
    

    文字大小

    你不能简单地用资源来改变文本的大小。没有“尺寸选择器”。您必须在代码中执行此操作。并且没有直接的解决方案。

    可能最简单的解决方案可能是利用View.onTouchListener() 并相应地处理向上和向下事件。使用这样的东西:

    view.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // change text size to the "pressed value"
                    return true;
                case MotionEvent.ACTION_UP:
                    // change text size to the "normal value"
                    return true;
                default:
                    return false;
                }
            }
    });
    

    另一种解决方案可能是扩展视图并覆盖setPressed(Boolean) 方法。当按下状态发生变化时,该方法在内部被调用。然后在方法调用中相应地改变文本的大小(不要忘记调用super)。

    【讨论】:

      【解决方案3】:

      在drawable文件夹中创建custom_selector.xml

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:drawable="@drawable/unselected" android:state_pressed="true" />
         <item android:drawable="@drawable/selected" />
      </selector>
      

      在可绘制文件夹中创建 selected.xml 形状

      <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
         <solid android:color="@color/selected"/>
         <padding />
         <stroke android:color="#000" android:width="1dp"/>
         <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
      </shape>
      

      在可绘制文件夹中创建 unselected.xml 形状

      <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
         <solid android:color="@color/unselected"/>
         <padding />
         <stroke android:color="#000" android:width="1dp"/>
         <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
      </shape>
      

      在 values 文件夹的 color.xml 中为选中/未选中状态添加以下颜色

      <color name="selected">#a8cf45</color>
      <color name="unselected">#ff8cae3b</color>
      

      您可以通过 here

      查看完整的解决方案

      【讨论】:

        【解决方案4】:

        实现选择器的最佳方式是使用 xml,而不是使用编程方式,因为使用 xml 更容易实现。

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

        有关我使用此链接实现的更多信息 http://www.blazin.in/2016/03/how-to-use-selectors-for-botton.html

        【讨论】:

          【解决方案5】:

          在布局 .xml 文件中

          <Button
           android:id="@+id/button1"
           android:background="@drawable/btn_selector"
           android:layout_width="100dp"
           android:layout_height="50dp"
           android:text="press" />
          

          btn_selector.xml

          <?xml version="1.0" encoding="utf-8"?>
          

           <item android:drawable="@drawable/btn_bg_selected" android:state_selected="true"></item>
          <item android:drawable="@drawable/btn_bg_pressed" android:state_pressed="true"></item>
          <item android:drawable="@drawable/btn_bg_normal"></item>
          

          【讨论】:

            【解决方案6】:

            您可以使用此代码:

            <Button
                android:id="@+id/img_sublist_carat"
                android:layout_width="70dp"
                android:layout_height="68dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="625dp"
                android:contentDescription=""
                android:background="@drawable/img_sublist_carat_selector"
                android:visibility="visible" />
            

            (选择器文件) img_sublist_carat_selector.xml:

            <?xml version="1.0" encoding="UTF-8"?>
            <selector xmlns:android="http://schemas.android.com/apk/res/android">
             <item android:state_focused="true" 
                   android:state_pressed="true"        
                   android:drawable="@drawable/img_sublist_carat_highlight" />
             <item android:state_pressed="true" 
                   android:drawable="@drawable/img_sublist_carat_highlight" />
             <item android:drawable="@drawable/img_sublist_carat_normal" />
            </selector>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-12-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-06
              • 1970-01-01
              • 1970-01-01
              • 2020-03-05
              相关资源
              最近更新 更多