android中设置按钮不同状态时按钮的效果

package cn.edu.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;

public class ButtonActivity extends Activity {
    /** Called when the activity is first created. */
	private	Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)this.findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.out.println("btn pressed");
			}
        	
        });
    }
}

 在res文件夹下,新建drawable文件夹,同时,新建ic_tab_artists.xml文件

?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/android_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/android_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/android_normal" />
</selector>
在布局文件中
<ImageButton 
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
    	android:background="@drawable/ic_tab_artists"
    />
 

相关推荐