【原创】AndroidImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
首先看看网上的2种方法:
【以下为引用网络,来源:android/thread-7931-1-1.html】
使用Button时为了让用户有“按下”的效果,有两种实现方式:
1.在代码里面。
view plaincopy to clipboardprint?
imageButton.setOnTouchListener(new OnTouchListener(){     
                        @Override   
                        public boolean onTouch(View v, MotionEvent event) {     
                                Action() == MotionEvent.ACTION_DOWN){     
                                        //更改为按下时的背景图片     
                                        v.setBackgroundResource(R.drawable.pressed);     
                                }else Action() == MotionEvent.ACTION_UP){     
                                        //改为抬起时的图片     
                                        v.setBackgroundResource(leased);     
                                }     
                                return false;     
                        }     
float up                });   
imageButton.setOnTouchListener(new OnTouchListener(){ 
                        @Override 
                        public boolean onTouch(View v, MotionEvent event) { 
                                Action() == MotionEvent.ACTION_DOWN){ 
                                        //更改为按下时的背景图片 
                                        v.setBackgroundResource(R.drawable.pressed); 
                                }else Action() == MotionEvent.ACTION_UP){ 
                                        //改为抬起时的图片 
                                        v.setBackgroundResource(leased); 
                                } 
                                return false; 
                        }   
                }); 
imageButton.setOnTouchListener(new OnTouchListener(){ 
                        @Override 
                        public boolean onTouch(View v, MotionEvent event) { 
                                Action() == MotionEvent.ACTION_DOWN){ 
                                        //更改为按下时的背景图片 
                                        v.setBackgroundResource(R.drawable.pressed); 
                                }else Action() == MotionEvent.ACTION_UP){ 
                                        //改为抬起时的图片 
                                        v.setBackgroundResource(leased); 
                                } 
                                return false; 
                        } 
                }); 
imageButton.setOnTouchListener(new OnTouchListener(){
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Action() == MotionEvent.ACTION_DOWN){
                                        //更改为按下时的背景图片
                                        v.setBackgroundResource(R.drawable.pressed);
                                }else Action() == MotionEvent.ACTION_UP){
                                        //改为抬起时的图片
                                        v.setBackgroundResource(leased);
                                }
                                return false;
                        }
                });
 
2.用XML文件实现。
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>   
<selector xmlns:android="schemas.android/apk/res/android">   
    <item          android:state_pressed="false"  android:drawable="@drawable/button_add" />   
    <item          android:state_pressed="true"  android:drawable="@drawable/button_add_pressed" />   
    <item          android:state_focused="true"    android:drawable="@drawable/button_add_pressed" />   
<item          android:drawable="@drawable/button_add" />   
</selector>   
 
<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="schemas.android/apk/res/android"> 
<item          android:state_pressed="false"  android:drawable="@drawable/button_add" /> 
 
    <item            android:state_pressed="true"  android:drawable="@drawable/button_add_pressed" />