Android代码规范----按钮单击事件的四种写法
【前言】
按钮少的时候用第三种的匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类。
按钮多的时候一般选择第四种写法。
一、第一种写法:在XML文件中声明onClick属性(很少用)
在XML文件中显式指定控件的onClick属性,点击按钮时会利用反射的方式调用对应Activity中的onClick()方法。
(1)xml文件代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="按钮1" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="按钮2" /> </LinearLayout>
上面的代码中,我们在第11行、18行指定了onClick属性,然后接下来在Activity中写一个onClick同名方法。
(2)MainActivity.java:
package com.vae01; import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private Button btn1, btn2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} // 方法:初始化View
private void initView() {
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
} //方法:控件View的点击事件
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
Toast.makeText(MainActivity.this, "btn1", Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Toast.makeText(MainActivity.this, "btn2", Toast.LENGTH_SHORT).show();
break; default:
break;
}
}
}
注意,第30行的onClick()方法的权限是public,毕竟xml文件还要访问的嘛。
二、第二种写法:使用内部类(很少用)
(1)xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" /> </LinearLayout>
(2)MainActivity.java:
package com.vae01; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private Button btn1, btn2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} // 方法:初始化View
private void initView() {
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(new MyClickListener());
btn2.setOnClickListener(new MyClickListener());
} class MyClickListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
Toast.makeText(MainActivity.this, "btn1", Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Toast.makeText(MainActivity.this, "btn2", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
注意第28、29行:不要忘记了绑定监听器。
三、第三种写法:匿名内部类(适合场景:测试、或者只有单个button的时候。使用较多)
(1)xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" /> </LinearLayout>
(2)MainActivity.java:
package com.vae01; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity { private Button btn1, btn2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} // 方法:初始化View
private void initView() {
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "btn1", Toast.LENGTH_SHORT).show();
} }); btn2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "btn2", Toast.LENGTH_SHORT).show();
} });
}
}
四、第四种写法:Activity实现View.OnClickListener接口(最常用)
Activity继承View.OnClickListener,由Activity实现OnClick(View view)方法,在OnClick(View view)方法中用switch-case对不同id代表的button进行相应的处理
(1)xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" /> </LinearLayout>
(2)MainActivity.java:
package com.vae01; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button btn1, btn2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化View
initView();
} // 方法:初始化View
private void initView() {
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2); //按钮绑定点击事件的监听器
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
} //方法:按钮的单击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
Toast.makeText(MainActivity.this, "btn1", Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Toast.makeText(MainActivity.this, "btn2", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
Android代码规范----按钮单击事件的四种写法的更多相关文章
- 转--Android按钮单击事件的四种常用写法总结
这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下 很多学习Android程序设计的人都会发现每个人对代码的 ...
- Android按钮单击事件的四种常用写法
这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下 很多学习Android程序设计的人都会发现每个人对代码的写法都有 ...
- Android按钮单击事件的四种常用写法总结
很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.因此本文就把这些写法总结一下,比较下各种写法的优劣,希望对大家灵活地选择编码方式可 ...
- [Android] 按钮单击事件的五种写法
在平时学习安卓的过程中,不论是看视频还是看博客,我发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.所以我想把这些写法总结一下,比较下各种写法的优劣,希望可以让自己可以灵 ...
- Android代码学习--点击事件的几种写法
由来:常规的写法参见<写一个apk>,每次点击按钮,按钮先查找文本框等元素,然后再操作,其实查找操作是很费时的操作,因此将该定义放到Activity的onCreate中:Oncreate只 ...
- Android中点击事件的四种写法详解
Android中点击事件的四种写法 使用内部类实现点击事件 使用匿名内部类实现点击事件 让MainActivity实现View.OnClickListener接口 通过布局文件中控件的属性 第一种方法 ...
- Android笔记---点击事件的四种写法
Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...
- Android开发系列之button事件的4种写法
经过前两篇blog的铺垫,我们今天热身一下,做个简单的样例. 文件夹结构还是引用上篇blog的截图. 详细实现代码: public class MainActivity extends Activit ...
- JAVA_SWT 事件的四种写法
一:匿名内部类写法 在一个组件下加入以下语句 text.addMouseListener(new MouseAdapter(){ public void mouseDoubleClich(MouseE ...
随机推荐
- 利用jquery实现网页禁止鼠标右键、禁止复制
很多时候,网站的内容辛苦写法被轻松复制,为了不让自己的劳动成果外流,可以利用禁止鼠标右键等方式保护自己的原创内容! 方式1:禁止鼠标右键操作 <script src="http://l ...
- 前端技巧:禁止浏览器static files缓存篇(转)
前端技巧:禁止浏览器static files缓存篇 由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. 本文记录博主的经验. Meta法 目前在chrome调试还没有遇到问题, ...
- OWIN概述
关于OWIN OWIN defines a standard interface between .NET web servers and web applications. The goal of ...
- jquery 下拉选择框/复选框常用操作
通常 1.我们需要获取select中选中的值,可以使用: $("#selectID").find("option:selected").val(); --一般 ...
- 「C语言」Windows+EclipseCDT下的C语言开发环境准备
之前写过一篇 「C语言」在Windows平台搭建C语言开发环境的多种方式 ,讨论了如何在Windows下用DEV C++.EclipseCDT.VisualStudio.Sublime Test.Cl ...
- 在Hadoop平台跑python脚本
1.开发IDE,我使用的是PyCharm. 2.运行原理 使用python写MapReduce的“诀窍”是利用Hadoop流的API,通过STDIN(标准输入).STDOUT(标准输出)在 ...
- [TypeScript] JSON对象转TypeScript对象范例
[TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...
- javascript函数中的三个技巧【三】
技巧三: [函数绑定] 在javascript与DOM交互中经常需要使用函数绑定,定义一个函数然后将其绑定到特定DOM元素或集合的某个事件触发程序上,绑定函数经常和回调函数及事件处理程序一起使用,以便 ...
- Force.com微信开发系列(七)OAuth2.0网页授权
OAuth是一个开放协议,允许用户让第三方应用以安全且标准的方式获取该用户在某一网站上存储的私密资源(如用户个人信息.照片.视频.联系人列表),而无须将用户名和密码提供给第三方应用.本文将详细介绍OA ...
- 极光推送和百度lbs android sdk一起使用使用proguard 混淆的问题
主要是http得类被混淆后,导致apk定位失败.经过确认,保留apache 的http类就好了 # To enable ProGuard in your project, edit project.p ...