【问题标题】:how to change background image of button when clicked/focused?单击/聚焦时如何更改按钮的背景图像?
【发布时间】:2011-12-09 20:18:21
【问题描述】:

我想在单击或聚焦时更改按钮的背景图像。

这是我的代码:

Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Button tiny = (Button)findViewById(R.id.tiny);
        tiny.setBackgroundResource(R.drawable.a9p_09_11_00754);

        TextView txt = (TextView)findViewById(R.id.txt);
        txt.setText("!---- On click ----!");
    }
});

这段代码对吗?它会在其事件中调用按钮吗?

【问题讨论】:

    标签: android button


    【解决方案1】:

    您可以在 xml 文件中实现如下:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/your_imagename_while_focused"/>
    <item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" />
    <item android:drawable="@drawable/image_name_while_notpressed" />  //means normal
    </selector>
    

    现在将这个 xml 文件保存在 drawable 文件夹中并将其命名为 suppos abc.xml 并将其设置如下

     Button tiny = (Button)findViewById(R.id.tiny);
     tiny.setBackgroundResource(R.drawable.abc);
    

    希望对您有所帮助。 :)

    【讨论】:

    • 这不适用于 API 22 或 API 16。我尝试通过 xml 和编程方式实现。另一方面,@Chirag Raval 的解决方案有效。
    • @JacobR,您说您尝试了 xml 和编程方式? Chirag Raval 的实现只是 xml 之一,那么它是如何不起作用的???我的也是程序化的,它会起作用,你一定做错了什么。
    • xml: android:background="@drawable/selector_drawable" programatically: v.setBackgroundResource(R.drawable.selector_drawable) 尝试你的答案,和@Chirag Raval 的答案唯一的区别是选择器文件中的内容。我知道很久以前,我使用了您的实现并且它有效,而今天重用该代码没有,所以它可能与 API 有关。
    • android 还会从 drawable-xhdpi、drawable-xxhdpi 等中选择合适大小的图像吗?还是我们必须在每个可绘制对象中放置一个选择器?
    • 如何在我的 java 类中以编程方式在没有 XML 文件的情况下做到这一点?
    【解决方案2】:

    它很容易实现。为此,您需要创建一个 xml 文件(选择器文件)并将其放在 res 中的 drawable 文件夹中。之后在布局文件的按钮背景中设置 xml 文件。

    button_background_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="false" android:drawable="@drawable/your_hover_image" />
        <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/your_hover_image" />
        <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/your_hover_image"/>
        <item android:drawable="@drawable/your_simple_image" />
    </selector>
    

    现在将上述文件设置在按钮的背景中。

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textColor="@color/grey_text"
        android:background="@drawable/button_background_selector"/>
    

    【讨论】:

    • 对于任何尝试这种方法但仍在苦苦挣扎的人,请确保 &lt;item android:drawable="@drawable/your_simple_image" /&gt; 是您的 selector 中的最后一个元素。这是因为系统将循环遍历每个元素并选择第一个匹配的元素。因此,如果这是第一个元素,它将匹配任何状态(按下、聚焦或正常),因此每次都会被选中 - 其他两个可绘制对象(按下和聚焦的)永远不会显示。
    • 这应该是公认的答案,它真的很有魅力!
    【解决方案3】:

    对不起,这是错误的。

    要根据特定事件(焦点、按下、正常)更改背景颜色/图像,您需要定义一个按钮选择器文件并将其实现为按钮的背景。

    例如: button_selector.xml(在drawable文件夹中定义这个文件)

    <?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:state_pressed="true"
               android:color="#000000" /> <!-- pressed -->
         <item android:state_focused="true"
               android:color="#000000" /> <!-- focused -->
         <item android:color="#FFFFFF" /> <!-- default -->
     </selector>
    
        <!-- IF you want image instead of color then write 
    android:drawable="@drawable/your_image" inside the <item> tag -->
    

    并将其应用为:

     <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:drawable="@drawable/button_selector.xml" />
    

    【讨论】:

    • 当我发布我不知道的答案时,他发布了它。
    • @Frankenstein 只需检查我发布的时间。如果你当时看不到,我是第一个回复的。
    • @Frankenstein 非常感兴趣。让我们在 chat.stackoverflow.com 中进行这种交流
    • 我在休闲聊天室或 Android 或 Android 讨论室等
    • 这个问题要么过时,要么错误。即使你使用颜色,你也必须使用
    【解决方案4】:

    使用此代码 在drawable文件夹名称中创建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/buutton_pressed" />
      <item 
         android:drawable="@drawable/button_image" />
    </selector>
    

    在按钮xml文件中

     android:background="@drawable/button"
    

    【讨论】:

      【解决方案5】:

      要改变按钮背景,我们可以通过两种方法

      1. 在按钮 OnClick 中,只需添加以下代码:

         public void onClick(View v) {
             if(v == buttonName) {
                buttonName.setBackgroundDrawable
                 (getResources().getDrawable(R.drawable.imageName_selected));
              }
        
               }
        

        2.在drawable文件夹中创建button_background.xml。(使用xml)

        res -> 可绘制 -> button_background.xml

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

        现在将上述文件设置在按钮的背景文件中。

             <Button
                   android:layout_width="fill_parent" 
                   android:layout_height="wrap_content"
                   android:background="@drawable/button_background"/>
        
                                  (or)
        
                 Button tiny = (Button)findViewById(R.id.tiny);
                       tiny.setBackgroundResource(R.drawable.abc);
        

        第二种方法更适合设置背景fd按钮

      【讨论】:

      • 谢谢@Ann,这个问题之前已经回答过了。
      【解决方案6】:

      你只需要在布局文件的按钮背景中设置背景并给出previous.xml文件。

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

      and done.Edit 下面是drawable目录下的previous.xml文件

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

      <item android:drawable="@drawable/onclick" android:state_selected="true"></item>
      <item android:drawable="@drawable/onclick" android:state_pressed="true"></item>
      <item android:drawable="@drawable/normal"></item>
      

      【讨论】:

      • 这个问题之前已经回答过了。
      【解决方案7】:

      您也可以直接在item 标签内创建形状,以防您想在视图中添加更多细节,如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_pressed="true">
              <shape>
                  <solid android:color="#81ba73" />
                  <corners android:radius="6dp" />
              </shape>
              <ripple android:color="#c62828"/>
          </item>
          <item android:state_enabled="false">
              <shape>
                  <solid android:color="#788e73" />
                  <corners android:radius="6dp" />
              </shape>
          </item>
          <item>
              <shape>
                  <solid android:color="#add8a3" />
                  <corners android:radius="6dp" />
              </shape>
          </item>
      </selector>
      

      请注意,Android 会从上到下循环遍历项目,因此,您必须将 item 无条件地放在列表底部(因此它的作用类似于默认/后备)。

      【讨论】:

        【解决方案8】:
        1. 在可绘制的 play_pause.xml 中创建一个文件
        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        
            <item android:state_selected="true"
                  android:drawable="@drawable/pause" />
        
            <item android:state_selected="false"
                  android:drawable="@drawable/play" />
            <!-- default -->
        </selector>
        
        1. 在 xml 文件中添加以下代码
         <ImageView
                        android:id="@+id/iv_play"
                        android:layout_width="@dimen/_50sdp"
                        android:layout_height="@dimen/_50sdp"
                        android:layout_centerInParent="true"
                        android:layout_centerHorizontal="true"
                        android:background="@drawable/pause_button"
                        android:gravity="center"
                        android:scaleType="fitXY" />
        
        1. 在 java 文件中添加以下代码
        iv_play = (ImageView) findViewById(R.id.iv_play);
        iv_play.setSelected(false);
        

        并添加这个

        iv_play.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        iv_play.setSelected(!iv_play.isSelected());
                        if (iv_play.isSelected()) {
                            ((GifDrawable) gif_1.getDrawable()).start();
                            ((GifDrawable) gif_2.getDrawable()).start();
                        } else {
                            iv_play.setSelected(false);
                            ((GifDrawable) gif_1.getDrawable()).stop();
                            ((GifDrawable) gif_2.getDrawable()).stop();
                        }
                    }
                });
        

        【讨论】:

          猜你喜欢
          • 2016-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-07
          • 2013-04-25
          • 1970-01-01
          • 2011-12-14
          • 2011-07-21
          相关资源
          最近更新 更多