【问题标题】:Change button background to clicked将按钮背景更改为单击
【发布时间】:2013-09-20 10:32:54
【问题描述】:

当您在 Android 中按下按钮时,它会改变颜色。我希望它保持这种状态。我的意思是我想要这样的东西:

Button btn = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             btn.setBackGroundColor(R.drawable.clicked /*clicked style*/ );
         }
     });

示例:

这是一个按下的按钮。我希望它在我停止按下它后保持这种状态。有没有这样简单的方法:

android.R.drawable.btn_default //这是默认按钮样式,我想要按下的:D

【问题讨论】:

标签: android button background


【解决方案1】:

在你的java代码中写button.setPressed(true);。它将按钮设置为按下模式。当你想设置它按下和不按下时,你可以将参数更改为 true 和 false...

【讨论】:

    【解决方案2】:

    尝试使用选择器:

    在drawable文件夹中定义selector.xml

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

    并将按钮的背景设置为android:background="@drawable/selector",然后在代码中设置按钮的点击事件btn.setpressed(true);

    【讨论】:

      【解决方案3】:

      所以你想让它开始不被按下,然后在被点击后保持按下状态? 尝试使用 OnTouchListener 代替添加 btn.setPressed(true);:

      public boolean onTouch(View v, MotionEvent event)
              {
                  if (event.getAction() == MotionEvent.ACTION_DOWN)
                  {
                      btn.setPressed(true);
                  }
                  return true;
              }
      

      【讨论】:

      • 当我在其他方法中使用 setPressed 然后按钮 onClick 时,它可以工作。但是当我在 onClick 中使用它时它不起作用。我认为它会在 onClick 事件完成时自动将 setPressed 更改为 false。
      【解决方案4】:

      查看 StateSelector 图像

      http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

      您可以为按钮/视图的不同状态设置不同的资源。这个例子设置了不同的颜色:

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

      然后就可以正常设置选择器了

      button.setBackgroundResource(R.drawable.selector);
      

      【讨论】:

        【解决方案5】:

        您可以尝试这种方式,或者在按下时将图像设置为暗色或彩色

        drawable/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:drawable="@drawable/buttondark"/>
             <item android:state_pressed="true" android:drawable="@drawable/buttondark" />
             <item android:drawable="@drawable/button" /> 
        </selector>
        

        在on-create方法里面写

        btn.setBackgroundResource(R.drawable.selector);
        

        【讨论】:

        • 这似乎是一个很好的解决方案。但在 selector.xml 中,它给出了错误:“错误:找不到与给定名称匹配的资源(在 'drawable' 处,值为 '@drawable/buttondark')”和“错误:找不到与给定名称匹配的资源(在'可绘制',值为'@drawable/button')。”
        • 只是因为资源文件夹中没有图像,所以将图像或颜色替换为 buttondark
        猜你喜欢
        • 2023-03-15
        • 1970-01-01
        • 2017-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-14
        • 2014-10-09
        相关资源
        最近更新 更多