Button控件的三种点击事件
①在布局文件中指定onClick属性的方法设置点击事件




②使用匿名内部类的方法设置点击事件



③实现Activity实现OnClickListen接口的方式设置点击事件



linear.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="按钮1"
/>
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:id="@+id/btn_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
/> </LinearLayout>
MainActivity代码
package com.iang.buttonclick; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn_one,btn_two,btn_three;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
btn_one=(Button)findViewById(R.id.btn_one);
btn_two=(Button) findViewById(R.id.btn_two);
btn_three=(Button) findViewById(R.id.btn_three); // 通话匿名类来监听鼠标点击事件
btn_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn_two.setText("按钮2已经被点击");
}
}); btn_three.setOnClickListener(this);
}
// 通过在xml文件里定义click来监听点击事件
public void click(View view){
btn_one.setText("按钮1已经被点击");
} // 通过定义接口方法来监听鼠标点击事件
@Override
public void onClick(View v) {
btn_three.setText("按钮3已经被点击");
}
}
Button控件的三种点击事件的更多相关文章
- 【MFC】Button控件和Picture Control的鼠标事件执行顺序
1.Button控件鼠标事件执行顺序 (1) WM_LBUTTONDOWN (2) WM_LBUTTONUP (3) OnBnClickedButton1(); 2.Picture Control的鼠 ...
- 模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。
需要用到的lib包 :解析json gson包,从网络地址解析json数据成String字符串的异步网络解析工具AsyncHttpClient等 下载地址:点击下载 Xlistview 下拉上拉第三 ...
- Jquery获取服务器端控件的三种方式
一 Jquery获得服务器控件值的方法由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,google了下,总结有以下3种方法: 服务器控件代码:<as ...
- Android 组件的三种点击事件写法
一.准备工作 1.项目结构 2.布局文件(activity_main.xml) <?xml version="1.0" encoding="utf-8"? ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
开发中常常需要自己定义Listview,去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点. 如果你的自定义Li ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- datetimepicker[jquery-ui]时间控件的三种初始化方法
1.只显示年月日 $( ".datepicker").datepicker({ needDay:true, changeMonth: true, //显示月份 changeYear ...
- 10. Extjs Button控件的handler配置项和click事件
转自:https://blog.csdn.net/lishk314/article/details/41541797 查看API: click( this, e, eOpts ) Fires when ...
- EXTJS4.2 控件之Grid 行点击事件
listeners: { 'itemclick': function (view, record, item, index, e) { //Ext.MessageBox.alert("标题& ...
随机推荐
- Lombok子类与父类的@Builder注解冲突
解决方法之一:去掉父类的@builder 缺点:父类不能使用Builder构造器模式 来源:https://www.cnblogs.com/lori/p/10266508.html
- thymeleaf中分类信息使用不同的样式
需求: 相关class类名:S224_on_point url:/notification/list url:/notification/list?type=2 thymeleaf代码如下: < ...
- 从成员函数指针生成可调用对象:function<>、mem_fn()和bind()
我们知道,普通函数指针是一个可调用对象,但是成员函数指针不是可调用对象.因此,如果我们想在一个保存string的vector中找到第一个空string,不能这样写: vector<string& ...
- Win10使用Tex Live和VS Code和Latex Workshop插件编写Latex文档(未完成版本)
首先取Tex Live官网下载安装包:https://www.tug.org/texlive/acquire-netinstall.html 我下载的是 http://mirror.ctan.org/ ...
- ubuntu 18.04安装jdk8和eclipse
JDK8的安装 1.安装ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装JDK sudo apt-ge ...
- 01.轮播图之三 : collectionView 轮播
个人觉得 collection view 做轮播是最方便的,设置下flowlayout 其他不会有很大的变动,没有什么逻辑的代码 let's begin…… 创建自定义的view .h 声明文件 @i ...
- 欧姆龙 EntherNet/IP(CIP报文格式)
Enthip/IP_ CIP报文格式 测试Demo在文章末尾 注册请求帧: 0x65 0x00 注册请求命令 2byte 0x04,0x00 header长度2byte < 封装头& ...
- 学习笔记:oracle之win10安装卸载oracle 11gR2步骤及常见问题解决
1.win10下安装oracle11g 1.1 工具原料 oracle11g安装包(64位) 1.2 步骤方法 1.在Oracle官网下载安装包,下载后,得到的文件如图所示: 2.将两个文件进行解压缩 ...
- Memory Barriers Are Like Source Control Operations
From: http://preshing.com/20120710/memory-barriers-are-like-source-control-operations/ If you use ...
- jenkins sonarqube 代码检测部署
install pgsql and sonarqube docker run --name postgresqldb -e POSTGRES_USER=sonar -e POSTGRES_PASSWO ...