点击时改变按钮图标

参考: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();
            }
        });
    }

 

posted @ 2015-09-19 16:55  pepelu  阅读(660)  评论(0编辑  收藏  举报