zoukankan      html  css  js  c++  java
  • Android开发之onClick事件的三种写法(转)

     1 package a.a;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.Button;
     7 import android.widget.EditText;
     8 
     9 public class AActivity extends Activity {
    10     /** Called when the activity is first created. */
    11 
    12     EditText Ev1;
    13 
    14     @Override
    15     public void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.main);
    18 
    19         Ev1 = (EditText)findViewById(R.id.editText1);  
    20 
    21         //第一种方式  
    22         Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源  
    23         Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听  
    24             public void onClick(View v) {  
    25                 String strTmp = "点击Button01";  
    26                 Ev1.setText(strTmp);  
    27             }  
    28 
    29         });  
    30 
    31         //第二种方式  
    32         Button Btn2 = (Button) findViewById(R.id.button2);//获取按钮资源  
    33         Btn2.setOnClickListener(listener);//设置监听  
    34 
    35     }
    36 
    37     Button.OnClickListener listener = new Button.OnClickListener(){//创建监听对象  
    38         public void onClick(View v){  
    39             String strTmp="点击Button02";  
    40             Ev1.setText(strTmp);  
    41         }  
    42 
    43     };
    44 
    45 
    46     //第三种方式(Android1.6版本及以后的版本中提供了)  
    47     public void Btn3OnClick(View view){  
    48         String strTmp="点击Button03";
    49         Ev1.setText(strTmp);
    50 
    51     }  
    52 }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent"
     5     android:orientation="vertical" >
     6 
     7     <TextView
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:text="@string/hello" />
    11 
    12     <Button
    13         android:id="@+id/button1"
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:text="Button1" />
    17 
    18     <Button
    19         android:id="@+id/button2"
    20         android:layout_width="wrap_content"
    21         android:layout_height="wrap_content"
    22         android:text="Button2" />
    23     
    24  <Button
    25         android:id="@+id/button3"
    26         android:layout_width="wrap_content"
    27         android:layout_height="wrap_content"
    28         android:text="Button3" 
    29         android:onClick="Btn3OnClick"/>
    30 
    31  <EditText
    32      android:id="@+id/editText1"
    33      android:layout_width="match_parent"
    34      android:layout_height="wrap_content" >
    35 
    36      <requestFocus />
    37  </EditText>
    38 
    39 </LinearLayout>
  • 相关阅读:
    java 抽象类
    ClassNotFoundException: dao.impl.ActionImpl
    侦听状态一直为T的处理
    Duplicate entry '1' for key 'PRIMARY'(报错)
    ibatis学习笔记
    java中的堆、栈和常量池
    servlet学习
    三大排序
    第一次面试??交流
    毕业季,学长,学姐们的践行
  • 原文地址:https://www.cnblogs.com/sandea/p/4018000.html
Copyright © 2011-2022 走看看