码迷,mamicode.com
首页 > 其他好文 > 详细

点击时改变按钮图标

时间:2015-09-19 18:06:25      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

参考:http://www.mkyong.com/android/android-imagebutton-selector-example/

http://developer.android.com/guide/topics/ui/controls.html

1.在res/drawable文件夹下新建filename.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed_yellow" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_focused_orange" android:state_focused="true"/>
    <item android:drawable="@drawable/button_normal_green"/>
</selector>

正常状态:button_normal_green.png

点击时:button_pressed_yellow.png

 

2.Button配置:

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"
        android:text="Button" 
        android:background="@drawable/drawable"
        />

 

3.Activity代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "cc", Toast.LENGTH_LONG).show();
            }
        });
    }

 

点击时改变按钮图标

标签:

原文地址:http://www.cnblogs.com/mada0/p/4821767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!