单击/聚焦时如何更改按钮的背景图像?

时间:2022-11-21 21:25:41

I want to change the background image of a button when clicked or focused.

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

This is my code:

这是我的代码:

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 ----!");
    }
});

Is this code right? Does it calls a button on its event?

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

6 个解决方案

#1


89  

you can implement in a xml file for this as follows:

你可以在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>

now save this xml file in drawable folder and name it suppos abc.xml and set it as follows

现在将此xml文件保存在drawable文件夹中,并将其命名为suppc abc.xml并将其设置如下

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

Hope it will help you. :)

希望它会对你有所帮助。 :)

#2


54  

Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.

它很容易实现。为此,您需要创建一个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>

Now set the above file in button's background.

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

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

#3


8  

Sorry this is wrong.

对不起这是错的。

For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.

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

For example: button_selector.xml (define this file inside the drawable folder)

例如: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 -->

And apply it as:

并将其应用为:

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

#4


5  

use this code create xml file in drawable folder name:button

使用此代码在可绘制文件夹名称中创建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>

and in button xml file

并在按钮xml文件中

 android:background="@drawable/button"

#5


3  

To change the button background we can follow 2 methods

要更改按钮背景,我们可以遵循2种方法

  1. In the button OnClick, just add this code:

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

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

    2.Create button_background.xml in the drawable folder.(using xml)

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

    res -> drawable -> button_background.xml

    res - > drawable - > 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>
    

    Now set the above file in button's background file.

    现在在按钮的后台文件中设置上面的文件。

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

    2nd method is better for setting the background fd button

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

#6


2  

You just need to set background and give previous.xml file in background of button in your layout file.

您只需设置背景并在布局文件中的按钮背景中提供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 Following is previous.xml file in drawable directory

和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>

#1


89  

you can implement in a xml file for this as follows:

你可以在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>

now save this xml file in drawable folder and name it suppos abc.xml and set it as follows

现在将此xml文件保存在drawable文件夹中,并将其命名为suppc abc.xml并将其设置如下

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

Hope it will help you. :)

希望它会对你有所帮助。 :)

#2


54  

Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.

它很容易实现。为此,您需要创建一个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>

Now set the above file in button's background.

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

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

#3


8  

Sorry this is wrong.

对不起这是错的。

For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.

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

For example: button_selector.xml (define this file inside the drawable folder)

例如: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 -->

And apply it as:

并将其应用为:

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

#4


5  

use this code create xml file in drawable folder name:button

使用此代码在可绘制文件夹名称中创建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>

and in button xml file

并在按钮xml文件中

 android:background="@drawable/button"

#5


3  

To change the button background we can follow 2 methods

要更改按钮背景,我们可以遵循2种方法

  1. In the button OnClick, just add this code:

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

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

    2.Create button_background.xml in the drawable folder.(using xml)

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

    res -> drawable -> button_background.xml

    res - > drawable - > 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>
    

    Now set the above file in button's background file.

    现在在按钮的后台文件中设置上面的文件。

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

    2nd method is better for setting the background fd button

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

#6


2  

You just need to set background and give previous.xml file in background of button in your layout file.

您只需设置背景并在布局文件中的按钮背景中提供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 Following is previous.xml file in drawable directory

和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>