点击事件更改按钮背景颜色?

时间:2022-11-20 14:03:03

When I declare a button on an activity and set the android:onClick attribute, the activity automatically changes the background color of the button when pressed.

当我在活动上声明一个按钮并设置android:onClick属性时,活动会在按下时自动更改按钮的背景颜色。

Now I've got a button on a fragment. Since I can't use android:onClick here, I'm doing it with a onClickListener instead. The button works, however it doesn't change it's style anymore.

现在我在一个片段上有一个按钮。由于我无法使用android:onClick here,我正在使用onClickListener。按钮有效,但它不再改变它的风格。

Do I have to do this manually? How?

我必须手动执行此操作吗?怎么样?

2 个解决方案

#1


0  

We need an XML file, drawable selector. I named this file with res/drawable/bg_button.xml:

我们需要一个XML文件,drawable选择器。我用res / drawable / bg_button.xml命名了这个文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:drawable="@drawable/bg_button_pressed"/>
    <item android:state_pressed="true" android:drawable="@drawable/bg_button_pressed" />
    <item android:drawable="@drawable/bg_button_normal" />  <!-- normal state -->
</selector>

For res/drawable/bg_button_pressed.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="color when pressed" />
</shape>

Create a new XML file, name it res/drawable/bg_button_normal.xml, and copy above code. Then, replace android:color with normal color, i.e. when button on normal state.

创建一个新的XML文件,将其命名为res / drawable / bg_button_normal.xml,然后复制上面的代码。然后,用正常颜色替换android:color,即当按钮处于正常状态时。

And finally:

<Button
    android:id="@+id/..."
    android:text="@string/..."
    android:background="@drawable/bg_button"/>

If you didn't have res/drawable folder, create a new one.

如果您没有res / drawable文件夹,请创建一个新文件夹。

#2


0  

Try this,

  1. Reference your button

    参考你的按钮

    yourButton = (Button) <fragment name>.findViewById(R.id.<button id>);
    yourButton.setOnClickListener(this);
    
  2. And within your click event ,

    在您的点击事件中,

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    
    case R.id.yourButton:
        yourButton.setBackgroundColor(getActivity().getResources().getColor(<colour code>));
        break;
    
    default:
        break;
    }
    }
    

#1


0  

We need an XML file, drawable selector. I named this file with res/drawable/bg_button.xml:

我们需要一个XML文件,drawable选择器。我用res / drawable / bg_button.xml命名了这个文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:drawable="@drawable/bg_button_pressed"/>
    <item android:state_pressed="true" android:drawable="@drawable/bg_button_pressed" />
    <item android:drawable="@drawable/bg_button_normal" />  <!-- normal state -->
</selector>

For res/drawable/bg_button_pressed.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="color when pressed" />
</shape>

Create a new XML file, name it res/drawable/bg_button_normal.xml, and copy above code. Then, replace android:color with normal color, i.e. when button on normal state.

创建一个新的XML文件,将其命名为res / drawable / bg_button_normal.xml,然后复制上面的代码。然后,用正常颜色替换android:color,即当按钮处于正常状态时。

And finally:

<Button
    android:id="@+id/..."
    android:text="@string/..."
    android:background="@drawable/bg_button"/>

If you didn't have res/drawable folder, create a new one.

如果您没有res / drawable文件夹,请创建一个新文件夹。

#2


0  

Try this,

  1. Reference your button

    参考你的按钮

    yourButton = (Button) <fragment name>.findViewById(R.id.<button id>);
    yourButton.setOnClickListener(this);
    
  2. And within your click event ,

    在您的点击事件中,

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    
    case R.id.yourButton:
        yourButton.setBackgroundColor(getActivity().getResources().getColor(<colour code>));
        break;
    
    default:
        break;
    }
    }