zoukankan      html  css  js  c++  java
  • 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. }  


     

    第二种: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. }  


     

    第三种:个人比较推荐。

    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. }  
  • 相关阅读:
    重新格式化namenode后,出现java.io.IOException Incompatible clusterIDs
    启动hadoop,报错Error JAVA_HOME is not set and could not be found
    hadoop错误org.apache.hadoop.yarn.exceptions.YarnException Unauthorized request to start container
    hadoop错误org.apache.hadoop.util.DiskChecker$DiskErrorException Could not find any valid local directory for
    hadoop错误org.apache.hadoop.mapred.TaskAttemptListenerImpl Progress of TaskAttempt
    hadoop错误org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bda0f2
    hadoop错误Operation category READ is not supported in state standby
    hadoop错误java.io.IOException Failed to replace a bad datanode on the existing pipeline due to no more good datanodes being available to try
    hadoop错误INFO util.NativeCodeLoader
    hadoop错误Ignoring exception during close for org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bda0f2 java.io.IOException Spill failed
  • 原文地址:https://www.cnblogs.com/malaya/p/2457127.html
Copyright © 2011-2022 走看看