繁体   English   中英

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

[英]how to change background image of button when clicked/focused?

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

这是我的代码:

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

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

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

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

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

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

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

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

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

对不起这是错的。

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

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

并将其应用为:

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

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

并在按钮xml文件中

 android:background="@drawable/button"

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

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

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

    2.在drawable文件夹中创建button_background.xml。(使用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> 

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

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

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

您只需设置背景并在布局文件中的按钮背景中提供previous.xml文件。

<Button
 android:id="@+id/button1"
 android:background="@drawable/previous"
 android:layout_width="200dp"
 android:layout_height="126dp"
 android:text="Hello" />

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

您还可以直接在item标记内创建形状,以防您想要在视图中添加更多细节,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#81ba73" />
            <corners android:radius="6dp" />
        </shape>
        <ripple android:color="#c62828"/>
    </item>
    <item android:state_enabled="false">
        <shape>
            <solid android:color="#788e73" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#add8a3" />
            <corners android:radius="6dp" />
        </shape>
    </item>
</selector>

请注意Android将从上到下循环浏览项目,因此,您必须将item无条件地放在列表的底部(因此它的作用类似于默认/后备)。

  1. 在drawable play_pause.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/pause" />

    <item android:state_selected="false"
          android:drawable="@drawable/play" />
    <!-- default -->
</selector>
  1. 在xml文件中添加以下代码
 <ImageView
                android:id="@+id/iv_play"
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_centerInParent="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/pause_button"
                android:gravity="center"
                android:scaleType="fitXY" />
  1. 在java文件中添加以下代码
iv_play = (ImageView) findViewById(R.id.iv_play);
iv_play.setSelected(false);

并添加此

iv_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_play.setSelected(!iv_play.isSelected());
                if (iv_play.isSelected()) {
                    ((GifDrawable) gif_1.getDrawable()).start();
                    ((GifDrawable) gif_2.getDrawable()).start();
                } else {
                    iv_play.setSelected(false);
                    ((GifDrawable) gif_1.getDrawable()).stop();
                    ((GifDrawable) gif_2.getDrawable()).stop();
                }
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM