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>
时间: 2024-08-08 09:07:30

Android开发之onClick事件的三种写法(转)的相关文章

Android开发之onClick事件的两种主要形式

第一种也是最常用的形式:通过为onClick事件添加监听器,来激发当按钮被单击时应该处理的事件.如: btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "" + "通过为onClick()事件添加监听器的方式&quo

Android开发之onClick事件的实现

算是从2015年开始学习android开发,目前把onClick的事件实现写下来,记录下,以备参考. 实现button的点击功能,让textView显示一行文字,最简单的onClick事件. 直接贴代码: public void onClick_method(View view){ String contextString="点击了这个按钮,实现了点击功能"; TextView textView=(TextView) findViewById(R.id.testTV); textVie

Android笔记---点击事件的四种写法

Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // ... private class MyListener implements OnClickListener{ public void Onclick(View v){ // ... 点击事件的响应 } } } 2. 採用匿名内部类 ? ?缺点:若是点击事件太多,则须要的匿名内部类太多 class M

Android开发之Touch事件分发机制

原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewGroup处理. View在ViewGroup内,ViewGroup也可以在其他ViewGroup内,这时候把内部的ViewGroup当成View来分析. ViewGroup的相关事件有三个:onInterceptTouchEv

Android基础--点击事件的四种写法

1.定义内部类,实现点击事件,使用时将内部类对象传入事件源的setOnClickListener()方法中 private class MyClickListener implements View.OnClickListener{ @Override public void onClick(View v) { // 功能代码 } } 2.使用匿名内部类的方式实现点击事件 setOnClickListener(new View.OnClickListener() { @Override publ

android 控件onClick事件的4种实现方式

1. <span style="white-space:pre"> </span>TextView tv = (TextView) findViewById(R.id.tv); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //do something... }

Android Button点击事件的三种方式

一.在XML中 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <!-- 现行排序 -->> xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"<!-- 全屏覆盖 -->> android:lay

【转】Android 开发之旅:view的几种布局方式及实践

引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理及布局文件可谓有了比较深刻的认识和理解,并且用“Hello World!”程序来实践证明了.在继续深入Android开发之旅之前,有必要解决前两篇中没有介绍的遗留问题:View的几种布局显示方法,以后就不会在针对布局方面做过多的介绍.View的布局显示方式有下面几种:线性布局(Linear Layout).

android开发之Animations的使用(三)

android开发之Animations的使用(三) 本博文主要讲述的是,Animations在android开发中的用来循环播放动画的效果: MainActivity.java: package com.example.animationtest3; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.v