Android如何在按钮单击中更改默认背景颜色?

时间:2014-05-02 04:32:57

标签: android button colors

我需要在点击后在按钮上显示不同的颜色,因为用户需要知道按钮是 点击。 我不明白怎么做? 给我一个建议。 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);             
     }
 });

BUt这不起作用

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

4 个答案:

答案 0 :(得分: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>

ACTIVITY 代码

 @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));

    }

答案 1 :(得分:0)

根据所有状态创建选择器drawable。

以下是示例示例。

<强> 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);

您也可以使用Images而不是Shape。

答案 2 :(得分:0)

尝试这样,

<强> main.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);

        }
    });
    }
}

答案 3 :(得分:0)

您可以在onclicklistener,button.setBackgroundColor(Color.WHITE);

按钮中使用此行