Android按钮响应事件的四种方式
一、按钮响应事件的四种方式
整体布局
沿用登录界面,对登录按钮进行事件响应处理
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:shrinkColumns="1"android:stretchColumns="2,3"
>
<!-- 第一行--><TableRowandroid:id="@+id/tableRow0"android:gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/iv_1"android:layout_width="100dp"android:layout_height="100dp"android:background="#F8F6F6"android:src="@drawable/touxiang"android:scaleType="fitXY"/></TableRow><!-- 第二行--><TableRowandroid:id="@+id/tableRow1"android:gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/textView1"android:layout_width="55dp"android:layout_height="wrap_content"android:text="用户名:"android:textSize="40px"android:layout_column="1" /><EditTextandroid:id="@+id/editText1"android:layout_width="80dp"android:layout_height="wrap_content"android:minWidth="100px"android:textSize="40px"android:hint="廖燕霞"android:textColor="#E91E63"android:maxLength="10"android:lines="1"android:selectAllOnFocus="true"android:layout_column="2" /><TextViewandroid:id="@+id/textView1_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" "android:textSize="100px"android:layout_column="3" /></TableRow><!-- 第三行--><TableRowandroid:id="@+id/tableRow2"android:gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/textView2"android:layout_width="55dp"android:layout_height="wrap_content"android:text="密 码:"android:textSize="40px"android:layout_column="1" /><EditTextandroid:id="@+id/editText2"android:layout_width="80dp"android:layout_height="wrap_content"android:minWidth="100px"android:textSize="40px"android:hint="请输入10位数字密码"android:textColor="#E91E63"android:maxLength="10"android:lines="1"android:digits="1234567890"android:selectAllOnFocus="true"android:layout_column="2" /><TextViewandroid:id="@+id/textView1_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" "android:textSize="100px"android:layout_column="3" /></TableRow><!-- 第四行--><TableRowandroid:id="@+id/tableRow3"android:gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:id="@+id/button1"android:layout_width="55dp"android:layout_height="wrap_content"android:text="注 册"android:textSize="40px"android:layout_column="1" /><Buttonandroid:id="@+id/button2"android:layout_width="80dp"android:layout_height="wrap_content"android:text="登 录"android:textSize="40px"android:layout_column="2"android:onClick="click"/><TextViewandroid:id="@+id/textView1_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" "android:textSize="100px"android:layout_column="3" /></TableRow><!-- 第四行--><TableRowandroid:id="@+id/tableRow4"android:gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/tv4"android:layout_width="match_parent"android:layout_height="48dp"android:gravity="center"android:text="@string/baidu"android:textSize="50px" ></TextView></TableRow>
</TableLayout>
1.通过布局文件中添加onClike事件重载处理
public class MainActivity extends AppCompatActivity {Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.logindemo);button=(Button)findViewById(R.id.button2);}//重载public void ClickButton2(View view){button.setText("(布局文件onclike)正在登陆中...");}
}
效果
2.利用匿名内部类方式处理
public class MainActivity extends AppCompatActivity {Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.logindemo);button=(Button)findViewById(R.id.button2);//匿名内部类button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {button.setText("(匿名内部类)正在登陆中...");}});}
}
效果
3.利用外部类方式处理
public class MainActivity extends AppCompatActivity {Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.logindemo);button=(Button)findViewById(R.id.button2);//关键button.setOnClickListener(new MyListener());}//外部类class MyListener implements View.OnClickListener {@Overridepublic void onClick(View v) {button.setText("(外部类)正在登陆中...");}}
}
效果
4.利用Lambda表达式处理
Lambda环境配置见:https://blog.csdn.net/qq_18625571/article/details/104466610
public class MainActivity extends AppCompatActivity {Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.logindemo);button=(Button)findViewById(R.id.button2);//Lambda表达式button.setOnClickListener(v->button.setText("(Lambda)正在登陆中..."));}
}
效果
Android按钮响应事件的四种方式相关推荐
- android按钮响应事件吗,Android 按钮响应事件的几种方式
目录 1.在布局中指定onClick属性 布局代码 android:id="@+id/btn1" android:layout_width="wrap_content&q ...
- android四种点击事件,android点击事件的四种方式
android点击事件的四种方式 第一种方式:创建内部类实现点击事件 代码如下: package com.example.dail; import android.text.TextUtils; im ...
- svg鼠标响应事件的四种方法(其中两种可支持火狐)
svg鼠标响应事件的四种方法 鼠标响应事件的四种方法,以click事件为例. Mouse Events - SMIL <?xml version="1.0" encoding ...
- Android中点击事件的四种写法
Android中点击事件的四种写法 使用内部类实现点击事件 使用匿名内部类实现点击事件 让MainActivity实现View.OnClickListener接口 通过布局文件中控件的属性 第一种方法 ...
- android xpath解析xml,Android 中处理 XML 的四种方式-DOM
Android 中处理 XML 的几种方式连载中,我们就不介绍合成 XML 了,因为合成 XML 可以直接拼接字符串,虽然看起很不高大上,但却很有效.我们主要介绍如何取 XML 中的值. 适用 DOM ...
- Android代码规范----按钮单击事件的四种写法
[前言] 按钮少的时候用第三种的匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类. 按钮多的时候一般选择第四种写法. 一.第一种写法:在XML文件中声明onClick属性(很少用) 在XM ...
- [Android] 按钮单击事件的五种写法
在平时学习安卓的过程中,不论是看视频还是看博客,我发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.所以我想把这些写法总结一下,比较下各种写法的优劣,希望可以让自己可以灵 ...
- android 辅助功能 模拟点击,Android模拟点击的四种方式
导论 在Android中模拟一个点击事件有三种方式是通过模拟MotionEvent来实现:一种是通过ADB来实现:一种是通过Instrumentation测试框架来实现 第一种:模拟MotionEve ...
- Android模拟点击的四种方式
导论 在Android中模拟一个点击事件有三种方式是通过模拟MotionEvent来实现:一种是通过ADB来实现:一种是通过Instrumentation测试框架来实现 第一种:模拟MotionEve ...
最新文章
- 2019央视春晚公布主持阵容 语言类节目将有突破
- FCKeditor 2.6.4在ASP.NET中的配置方法
- 噢,原来SpringCloud是这样的呀!
- 相见恨晚的 18 个 Linux 趣味命令,最后一个千万要尝试~
- HTML中怎么点击超链接让新页面在另一个窗口打开?
- java 正则 中文_Java使用正则表达式(regex)匹配中文实例代码
- 计算机网络——域名系统
- HDU - 3853 Loop [Maho shoujo] [概率DP]
- 如何才能得到YouTube的流量?
- 计算机合成音乐的交换标准,音乐合成器
- 2018沈阳k(经典的约瑟夫环)
- Java实验——编写一个程序,实现字符串大小写的转换并倒序输出
- 无线网络技术导论笔记(第三讲)
- git 合并分支(开发分支dev合并到主分支master)
- html5 涂鸦板
- 99%的房地产企业都会遇到的信息化问题
- 我xp电脑桌面没有计算机图标不见了,最新版:xp桌面上的我的计算机图标丢失了,如何找到它...
- 简约不简单:高级时钟插件Advanced Clock Widget Pro
- 自动驾驶控制器,车道偏离预警系统,基于Prescan设计场景和交通流,在Simulink中建立了相应的控制模型
- 【解决】Error:(6, 26) java: 程序包javax.servlet.http不存在
热门文章
- [Unity3D]自己动手重制坦克舰队ArmadaTank(2)从碰撞说起
- html设置按钮样式表,如何使用样式表设置QToolButton的图标?
- 关于Mysql中on条件和where条件的使用总结
- Knockout subscribe,computed,extend
- python判断字符类型编程_python判断字符类型怎么做
- selenium配置ip代理和无头selenium
- Context完全解析(一)什么是Context?
- Python之画蟒蛇
- 【打卡】电子表格符号转换(简单)
- 代码(软件)是怎么和硬件发生联系的?