Android Button 的单击事件的三种响应方法

转自http://blog.csdn.net/hopease/article/details/7293244

Android(SDK 1.5) Button 的单击事件的三种响应方法( 个人比较推荐第三种):

 

第一种:个人认为写法不好,不容易看懂。

  1. package leo.zheng.ButtonClick;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. import android.graphics.Color;  
  7. import android.widget.Button;  
  8.   
  9. import android.widget.TextView;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12.   
  13. import android.util.Log;  
  14.   
  15. public class ButtonClick extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.           
  22.         final TextView Text = (TextView)findViewById(R.id.ColorView);  
  23.         final Button redButton = (Button)findViewById(R.id.Button01);  
  24.         final Button greenButton = (Button)findViewById(R.id.Button02);  
  25.           
  26.         redButton.setOnClickListener(  
  27.             new OnClickListener()  
  28.             {  
  29.                 public void onClick(View v)  
  30.                 {  
  31.                     Text.setBackgroundColor(Color.RED);  
  32.                     Log.v("Button click""red");  
  33.                 }  
  34.             }  
  35.         );  
  36.           
  37.         greenButton.setOnClickListener(  
  38.             new OnClickListener()  
  39.             {  
  40.                 public void onClick(View v)  
  41.                 {  
  42.                     Text.setBackgroundColor(Color.GREEN);  
  43.                     Log.v("Button click","green");  
  44.                 }  
  45.             }  
  46.         );  
  47.     }  
  48. }  
package leo.zheng.ButtonClick;

import android.app.Activity;
import android.os.Bundle;

import android.graphics.Color;
import android.widget.Button;

import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

import android.util.Log;

public class ButtonClick extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final TextView Text = (TextView)findViewById(R.id.ColorView);
        final Button redButton = (Button)findViewById(R.id.Button01);
        final Button greenButton = (Button)findViewById(R.id.Button02);
        
        redButton.setOnClickListener(
        	new OnClickListener()
        	{
        		public void onClick(View v)
        		{
        			Text.setBackgroundColor(Color.RED);
        			Log.v("Button click", "red");
        		}
        	}
        );
        
        greenButton.setOnClickListener(
        	new OnClickListener()
        	{
        		public void onClick(View v)
        		{
        			Text.setBackgroundColor(Color.GREEN);
        			Log.v("Button click","green");
        		}
        	}
        );
    }
}


 

第二种:Activity implements OnClickListener

  1. package leo.zheng.ButtonClick2;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8.   
  9. import android.util.Log;  
  10.   
  11. import android.widget.TextView;  
  12. import android.widget.Button;  
  13.   
  14. public class ButtonClick2 extends Activity implements OnClickListener {  
  15.     /** Called when the activity is first created. */  
  16.     private TextView textChange;  
  17.     private Button button1;  
  18.     private Button button2;  
  19.       
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.           
  25.         textChange = (TextView)findViewById(R.id.TextView01);  
  26.         button1 = (Button)findViewById(R.id.Button01);  
  27.         button1.setOnClickListener(this);  
  28.         button2 = (Button)findViewById(R.id.Button02);  
  29.         button2.setOnClickListener(this);  
  30.     }  
  31.       
  32.     public void onClick(View v)  
  33.     {  
  34.         Log.v("Click""Button id = " + v.getId());  
  35.         switch(v.getId())  
  36.         {  
  37.         case R.id.Button01:  
  38.             textChange.setText("Ok");  
  39.             break;  
  40.         case R.id.Button02:  
  41.             textChange.setText("Cancal");  
  42.             break;  
  43.         default:  
  44.             break;  
  45.         }  
  46.     }  
  47. }  
package leo.zheng.ButtonClick2;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;

import android.util.Log;

import android.widget.TextView;
import android.widget.Button;

public class ButtonClick2 extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	private TextView textChange;
	private Button button1;
	private Button button2;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textChange = (TextView)findViewById(R.id.TextView01);
    	button1 = (Button)findViewById(R.id.Button01);
    	button1.setOnClickListener(this);
    	button2 = (Button)findViewById(R.id.Button02);
    	button2.setOnClickListener(this);
    }
    
    public void onClick(View v)
    {
    	Log.v("Click", "Button id = " + v.getId());
    	switch(v.getId())
    	{
    	case R.id.Button01:
    		textChange.setText("Ok");
    		break;
    	case R.id.Button02:
    		textChange.setText("Cancal");
    		break;
    	default:
    		break;
    	}
    }
}


 

第三种:个人比较推荐。

  1. package leo.zheng.ButtonClick3;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6.   
  7. import android.util.Log;  
  8.   
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14.   
  15. public class ButtonClick3 extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     private Button button1;  
  18.     private Button button2;  
  19.     private TextView colorShow;  
  20.       
  21.     private button1_OnClickListener listener1 = new button1_OnClickListener();  
  22.     private button2_OnClickListener listener2 = new button2_OnClickListener();  
  23.       
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.           
  29.         button1 = (Button) findViewById(R.id.Button01);  
  30.         button2 = (Button) findViewById(R.id.Button02);  
  31.         colorShow = (TextView) findViewById(R.id.TextView01);  
  32.           
  33.         button1.setOnClickListener(listener1);  
  34.         button2.setOnClickListener(listener2);  
  35.     }  
  36.       
  37.     class button1_OnClickListener implements OnClickListener  
  38.     {  
  39.         public void onClick(View v)  
  40.         {  
  41.             Log.v("Click""Button GREEN");  
  42.             colorShow.setBackgroundColor(Color.GREEN);  
  43.         }  
  44.     }  
  45.       
  46.     class button2_OnClickListener implements OnClickListener  
  47.     {  
  48.         public void onClick(View v)  
  49.         {  
  50.             Log.v("Click""Button BLUE");  
  51.             colorShow.setBackgroundColor(Color.BLUE);  
  52.         }  
  53.     }  
  54. }  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值