【问题标题】:Android how to Change Default background color in Button click?Android如何更改按钮单击中的默认背景颜色?
【发布时间】:2014-05-02 04:32:56
【问题描述】:

点击后我需要在按钮上显示不同的颜色,因为用户需要知道按钮是 点击。 我不明白该怎么做? 给我建议。 button click shoud be programatically,无需为此创建XML

//XML file saved at res/drawable/button_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true"
      android:color="#ffff0000"/> <!-- pressed -->
   <item android:state_focused="true"
      android:color="#ff0000ff"/> <!-- focused -->
   <item android:color="#ff000000"/> <!-- default -->
 </selector>

在 JAVA 中

我是这样创作的

 Button Settings_Button = new Button(this)  ;        
 Settings_Button.setClickable(true);
 //Settings_Button.setBackgroundResource(R.drawable.selector);
 Settings_Button.setOnClickListener(new View.OnClickListener() {
 public void onClick(View view) {                    
   // Intent newIntent = new Intent(activity.getBaseContext(), ProfileSettingActivity.class);
   // activity.startActivity(newIntent);             
     }
 });

但是这不起作用

EDIT :如何以编程方式在单击按钮时更改背景颜色。

【问题讨论】:

  • 先告诉我你要把ColorImage 设置成你的Button 什么?
  • 取消注释设置按钮选择器资源的行会发生什么?
  • 使用该活动时强制关闭@TheDude
  • 你为什么不做 setBackgroundResource(R.drawable.button_bg);

标签: android button colors


【解决方案1】:

试试这个方法,希望对你有帮助...

button_selector

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

    <item android:drawable="@color/button_pressed" android:state_pressed="true"></item>
    <item android:drawable="@color/button_pressed" android:state_focused="true"></item>
    <item android:drawable="@color/button_default" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
    <item android:drawable="@color/button_pressed" android:state_enabled="false"></item>

</selector>

颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="button_pressed">#ffff0000</color>
    <color name="button_default">#ff0000ff</color>
</resources>

活动代码

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = new Button(this);
        btn.setBackgroundResource(R.drawable.button_selector);
        btn.setText("Custom Button");
        addContentView(btn,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    }

【讨论】:

    【解决方案2】:

    根据所有状态创建可绘制的选择器。

    以下是示例。

    btn_green_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:state_focused="false"
            android:state_selected="false"
            android:state_pressed="false"
            android:drawable="@drawable/btn_green_unselected" />
        <item
            android:state_focused="true"
            android:state_selected="false"
            android:state_pressed="false"
            android:drawable="@drawable/btn_green_selected" />
        <!-- Focused states -->
        <item
            android:state_focused="true"
            android:state_selected="true"
            android:state_pressed="false"
            android:drawable="@drawable/btn_green_selected" />
        <!-- Pressed state -->
        <item
            android:state_pressed="true"
            android:drawable="@drawable/btn_green_selected" />
    
    </selector>
    

    btn_green_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle">
    
        <solid android:color="#00B2B2"/>
    
    </shape>
    

    btn_green_unselected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle">
    
        <solid android:color="#8ED3CD"/>
    
    </shape>
    

    现在在编码中应用了 btn_green_selector.xml

    Settings_Button.setBackgroundResource(R.drawable.btn_green_selector);
    

    您也可以使用图像代替形状。

    【讨论】:

      【解决方案3】:

      像这样尝试,

      ma​​in.xml

         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/myLayout"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" >
      
              <Button
                  android:id="@+id/button1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentTop="true"
                  android:layout_marginLeft="64dp"
                  android:layout_marginTop="71dp"
                  android:text="changeColor" />
      
          </LinearLayout>
      

      ChangeBackgroundActivity.java

      public class ChangeBackgroundActivity extends Activity {
      /** Called when the activity is first created. */
          Button blueButton;
          LinearLayout myLO;
          @Override
         public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              myLO=(LinearLayout)findViewById(R.id.myLayout);
              blueButton=(Button)findViewById(R.id.button1);
              blueButton.setOnClickListener(new OnClickListener() {
      
                  public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  myLO.setBackgroundColor(Color.BLUE);
      
              }
          });
          }
      }
      

      【讨论】:

        【解决方案4】:

        您可以在按钮 onclicklistener 中使用此行,button.setBackgroundColor(Color.WHITE);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-09
          • 2017-08-02
          • 1970-01-01
          相关资源
          最近更新 更多