Android开发之onClick事件的三种写法(转)
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事件的三种写法(转)的更多相关文章
- Android开发之onClick事件的三种写法
package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...
- Android开发之onClick事件的实现
算是从2015年开始学习android开发,目前把onClick的事件实现写下来,记录下,以备参考. 实现button的点击功能,让textView显示一行文字,最简单的onClick事件. 直接贴代 ...
- (转)Ext.Button点击事件的三种写法
转自:http://maidini.blog.163.com/blog/static/377627042008111061844345/ ExtJs的写法太灵活了,现在收集了关于Button点击事件的 ...
- Android journey3 @点击事件的4种写法
对于android布局中的控件,如Button等会有相应的点击事件去响应它所需要的功能,今天我们就以电话拨号器的代码说明下几种点击事件: package com.itheima.phone; impo ...
- 16_点击事件第三种写法_activity实现接口
第一种写法是有名内部类,第二种写法是匿名内部类,第三种写法是MainActivity实现接口OnClickListener.直接让MainActivity实现了OnClickListener这个接口. ...
- Android笔记---点击事件的四种写法
Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...
- Android 触发Button按钮事件的三种方式
1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java 文件: package com.android. ...
- Android开发之Touch事件分发机制
原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...
- [Android]Java中点击事件的四种写法
点击事件的必备条件:实现OnClickListener接口,重写onclick(View v)方法 以拨号简单案例为例,如下图效果: 逻辑流程: 获取点击对象,获取数据 给对象设置监听类 实现OnCl ...
随机推荐
- SQL Server 2008下日志清理方法
1.将数据库设置成 简单 模式 选择要收缩的数据库,点右键 属性->选项,选择 简单模式 2.选择任务->收缩->文件 3.选择日志
- 也谈Asp.net 中的身份验证
钱李峰 的这篇博文<Asp.net中的认证与授权>已对Asp.net 中的身份验证进行了不错实践.而我这篇博文,是从初学者的角度补充了一些基础的概念,以便能有个清晰的认识. 一.配置安全身 ...
- 如何查看Windows8.1计算机体验指数评分
如果你已经安装使用了Windows 8.1,你就会发现自从Vista时代开始的计算机体验评分消失了,在文章<微软取消Windows 8 计算机评分功能>中,我猜测了微软取消评分功能的可能原 ...
- ComboBoxEdit数据绑定
ComboBoxEdit也是DevExpress winform控件中经常使用的一个,我们在使用的过程中可能有时需要对ComboBoxEdit控件进行数据绑定,而ComboBoxEdit控件不像 Lo ...
- 断开SVN连接操作方法
SVN是日常项目管理中经常使用到的工具,然而拷贝备份需要与SVN服务器断开连接,具体操作步骤: 1.在桌面建立一个文本文件,取名为kill-svn-folders.reg(扩展名由txt改为reg), ...
- Contoso 大学 - 6 – 更新关联数据
原文 Contoso 大学 - 6 – 更新关联数据 By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Microsoft's ...
- (转)mysql、sqlserver、oracle的默认事务的隔离级别
1.mysql的默认事务的隔离级别:可重复读取(repeatable read); 2.sqlserver的默认事务的隔离级别:提交读取(read committed); 3.oracle的默认事务的 ...
- .NET常用网络资源收藏
.NET核心站点: Microsoft主页 https://www.microsoft.com ASP.NET主页 http://www.asp.net TechNet中文网 https://tech ...
- 加密解密以及CA签证
在当今互联网时代,数据越来越来重要.那么如何加密?解密?以及通过什么方式来认证了?? 接下来,我就会和大家谈谈加密,解密以及CA签证的实现. 首先大家的知道一些加密,解密的基本常识: 互联网上中间人一 ...
- Linux C 程序 文件属性,文件删除(15)
dup ,dup2,fcntl,ioctl系统调用 . dup ,dup2 函数 int dup(int oldfd) int dup(int oldfd , int newfd) dup用来复制参数 ...