package a.a;  

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class AActivity extends Activity {
/** Called when the activity is first created. */ EditText Ev1; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); Ev1 = (EditText)findViewById(R.id.editText1); //第一种方式
Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源
Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听
public void onClick(View v) {
String strTmp = "点击Button01";
Ev1.setText(strTmp);
} }); //第二种方式
Button Btn2 = (Button) findViewById(R.id.button2);//获取按钮资源
Btn2.setOnClickListener(listener);//设置监听 } Button.OnClickListener listener = new Button.OnClickListener(){//创建监听对象
public void onClick(View v){
String strTmp="点击Button02";
Ev1.setText(strTmp);
} }; //第三种方式(Android1.6版本及以后的版本中提供了)
public void Btn3OnClick(View view){
String strTmp="点击Button03";
Ev1.setText(strTmp); }
}
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:onClick="Btn3OnClick"/> <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <requestFocus />
</EditText> </LinearLayout>

Android开发之onClick事件的三种写法的更多相关文章

  1. Android开发之onClick事件的三种写法(转)

    package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...

  2. Android开发之onClick事件的实现

    算是从2015年开始学习android开发,目前把onClick的事件实现写下来,记录下,以备参考. 实现button的点击功能,让textView显示一行文字,最简单的onClick事件. 直接贴代 ...

  3. (转)Ext.Button点击事件的三种写法

    转自:http://maidini.blog.163.com/blog/static/377627042008111061844345/ ExtJs的写法太灵活了,现在收集了关于Button点击事件的 ...

  4. Android journey3 @点击事件的4种写法

    对于android布局中的控件,如Button等会有相应的点击事件去响应它所需要的功能,今天我们就以电话拨号器的代码说明下几种点击事件: package com.itheima.phone; impo ...

  5. 16_点击事件第三种写法_activity实现接口

    第一种写法是有名内部类,第二种写法是匿名内部类,第三种写法是MainActivity实现接口OnClickListener.直接让MainActivity实现了OnClickListener这个接口. ...

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

    Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...

  7. Android 触发Button按钮事件的三种方式

    1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java  文件: package com.android. ...

  8. Android开发之Touch事件分发机制

    原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...

  9. [Android]Java中点击事件的四种写法

    点击事件的必备条件:实现OnClickListener接口,重写onclick(View v)方法 以拨号简单案例为例,如下图效果: 逻辑流程: 获取点击对象,获取数据 给对象设置监听类 实现OnCl ...

随机推荐

  1. Erlang第一课----基本变量

    1.(in Erlang shell)A sequence of expressions must be terminated with a period followed by whitespace ...

  2. Model1模式的学生信息增删改查

    Student.java package entity; public class Student { private int stuid; private String stuname; priva ...

  3. std::function,std::bind

    std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...

  4. JDK安装成功了,环境变量也配置好了,测试代码也可以运行,但是打不开eclipse

    解决办法:删除eclipse,重新解压后,将JDK文件夹下的jre文件夹拷贝到eclipse文件夹下,OK

  5. The C Programming Language (second edition) 实践代码(置于此以作备份)

    1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...

  6. JS实现打字机式字符输出效果

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. [转载]sed 简明教程

    文章转载自酷壳 – CoolShell.cn,作者:陈皓,地址http://coolshell.cn/articles/9104.html awk于1977年出生,今年36岁本命年,sed比awk大2 ...

  8. JavaScript的Eval与JSON.parse的区别

    JavaScript的Eval与JSON.parse的区别 json的定义以及用法: JSON(JavaScript Object Notation)是一种轻量级的数据格式,采用完全独立于语言的文本格 ...

  9. 在ubuntu上面安装phpmyadmin后,报404错误

    安装完mysql后,我想装一个phpmyadmin方便mysql管理. 在终端执行命令:sudo apt-get install libapache2-mod-auth-mysql phpmyadmi ...

  10. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...