Button 有按下效果 - 续

上次讲过 如何使得Button有按下的效果 但是最后也提过 如果要求几个Button都有这种效果 分别为每一个Button定义一个 *.xml 是不方便的 今天就演示一下简便一点的做法

 

 

[代码 步骤]

1. 准备这2个ImageButton 所需的 *.ico 如:

写道
play.ico : ImageButton-play 的静态(未按下) 效果
play_down.ico : ImageButton-play 的按下 效果


pause.ico : ImageButton-pause的静态(未按下) 效果
pause_down.ico : ImageButton-pause的按下 效果

 

 

 

2. 构建有2个ImageButton 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageButton  
	android:id="@+id/play"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />
<ImageButton  
	android:id="@+id/pause"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

 

 

 

3. 分别为View 定义名称 便于引用

public void initialView(){
    	play = (ImageButton) findViewById(R.id.play);
    	pause = (ImageButton) findViewById(R.id.pause);
    }

 

 

4. 为ImageButton play,pause 指定默认背景

public void specifyBackground(){
    	play.setBackgroundResource(R.drawable.play);
    	pause.setBackgroundResource(R.drawable.pause);
    }

 

 

5. 为ImageButton play,pause 注册 onTouch() 事件 并根据其ID 与 状态 指定具体的背景图

@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch(v.getId()){
				case R.id.play:
					if(event.getAction() == MotionEvent.ACTION_DOWN){
						v.setBackgroundResource(R.drawable.play_down);
					}
					else if(event.getAction() == MotionEvent.ACTION_UP){
						v.setBackgroundResource(R.drawable.play);
					}
					break;
				
				case R.id.pause:
					if(event.getAction() == MotionEvent.ACTION_DOWN){
						v.setBackgroundResource(R.drawable.pause_down);
					}
					else if(event.getAction() == MotionEvent.ACTION_UP){
						v.setBackgroundResource(R.drawable.pause);
					}
					break;
					
				
				}
				
				return false;
			}
        	
        };
        
        play.setOnTouchListener(listener);
        pause.setOnTouchListener(listener);

 

 

 

6. 所有程序为:

public class ButtonStyle3Usage extends Activity {
	ImageButton play,pause;
	
	OnTouchListener listener;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initialView();

        specifyBackground();
        
        listener = new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch(v.getId()){
				case R.id.play:
					if(event.getAction() == MotionEvent.ACTION_DOWN){
						v.setBackgroundResource(R.drawable.play_down);
					}
					else if(event.getAction() == MotionEvent.ACTION_UP){
						v.setBackgroundResource(R.drawable.play);
					}
					break;
				
				case R.id.pause:
					if(event.getAction() == MotionEvent.ACTION_DOWN){
						v.setBackgroundResource(R.drawable.pause_down);
					}
					else if(event.getAction() == MotionEvent.ACTION_UP){
						v.setBackgroundResource(R.drawable.pause);
					}
					break;
					
				
				}
				
				return false;
			}
        	
        };
        
        play.setOnTouchListener(listener);
        pause.setOnTouchListener(listener);
    }
    
    
    public void initialView(){
    	play = (ImageButton) findViewById(R.id.play);
    	pause = (ImageButton) findViewById(R.id.pause);
    }
    
    public void specifyBackground(){
    	play.setBackgroundResource(R.drawable.play);
    	pause.setBackgroundResource(R.drawable.pause);
    }
}

 

 

7. emulator 运行截图: 静态

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值