Android 开发添加控件事件的三种方式
- 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 HelloActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btnMethod01;
- Button btnMethod02;
- Button btnMethod03;
- btnMethod01 = (Button)findViewById(R.id.button1);
- btnMethod02 = (Button)findViewById(R.id.button2);
- btnMethod03 = (Button)findViewById(R.id.button3);
- //第一种方法:匿名类
- btnMethod01.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v){
- Toast.makeText(HelloActivity.this,R.string.method01
- ,Toast.LENGTH_SHORT).show();
- }
- });
- //添加监听事件
- btnMethod02.setOnClickListener(new button1OnClickListener());
- }
- //第二种方法:内部类实现 有两部 1.写内部类 2.添加监听事件
- private class button1OnClickListener implements OnClickListener{
- public void onClick(View v){
- Toast.makeText(HelloActivity.this,R.string.method02,
- Toast.LENGTH_SHORT).show();
- }
- }
- //第三种方法:用xml方法配置,该名称要与 main.xml 中button03的
- //android:onClick="OnClickButton03"的名字一样
- public void OnClickButton03(View v){
- Toast.makeText(HelloActivity.this,R.string.method03
- ,Toast.LENGTH_SHORT).show();
- }
- }
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 HelloActivity extends Activity { /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnMethod01;
Button btnMethod02;
Button btnMethod03; btnMethod01 = (Button)findViewById(R.id.button1);
btnMethod02 = (Button)findViewById(R.id.button2);
btnMethod03 = (Button)findViewById(R.id.button3); //第一种方法:匿名类
btnMethod01.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){
Toast.makeText(HelloActivity.this,R.string.method01
,Toast.LENGTH_SHORT).show();
} }); //添加监听事件
btnMethod02.setOnClickListener(new button1OnClickListener());
}
//第二种方法:内部类实现 有两部 1.写内部类 2.添加监听事件
private class button1OnClickListener implements OnClickListener{
public void onClick(View v){
Toast.makeText(HelloActivity.this,R.string.method02,
Toast.LENGTH_SHORT).show();
} }
//第三种方法:用xml方法配置,该名称要与 main.xml 中button03的
//android:onClick="OnClickButton03"的名字一样
public void OnClickButton03(View v){
Toast.makeText(HelloActivity.this,R.string.method03
,Toast.LENGTH_SHORT).show();
} }
- <?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">
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/method01"/>
- <Button
- android:id="@+id/button2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/method02"/>
- <Button
- android:onClick="OnClickButton03"
- android:id="@+id/button3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/method03"/>
- </LinearLayout>
<?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">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/method01"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/method02"/> <Button
android:onClick="OnClickButton03"
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/method03"/> </LinearLayout>
Android 开发添加控件事件的三种方式的更多相关文章
- CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建
Android开发使用控件入门--环境搭建 2014-12-24 09:57 14人阅读 评论(0) 收藏 编辑 删除 CAD控件.CAD三维控件,手机 ...
- Android开发使用控件入门--环境搭建
Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...
- 【VS开发】在VS2010中开发ActiveX控件设置测试容器的方式
在VS2010中开发ActiveX控件设置测试容器的方式 借鉴文章http://blog.csdn.net/waxgourd0/article/details/7374669 在VS2010中开发MF ...
- 怎样在Android开发中FPS游戏实现的两种方式比较
怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...
- Android 触发Button按钮事件的三种方式
1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java 文件: package com.android. ...
- 界面跳转+Android Studio Button事件的三种方式
今天学习界面跳转 java类总是不能新建成功 看了网上教程 (20条消息) 关于android studio无法创建类或者接口问题的解决方法_qq_39916160的博客-CSDN博客 可以新建了 但 ...
- Android - 页面返回上一页面的三种方式
今年刚刚跳槽到了新公司,也开始转型做Android,由此开始Android的学习历程. 最近在解很多UI的bug,在解bug过程中,总结了在UI的实现过程中,页面返回上一页面的几种实现方式. 一. 自 ...
- NGUI注册事件的三种方式
1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ...
随机推荐
- 【laravel5.4】安装predis
1.服务器先安装redis-server,这是毋庸置疑的!!! 2.服务器开启redis-server,配置相关参数 3.laravel执行 : $ yourPhpPath composer.pha ...
- LFU缓存
https://leetcode-cn.com/problems/lfu-cache/description/ 缓存的实现可以采取多种策略,不同策略优点的评估就是"命中率".好的策 ...
- linux shell 脚本攻略学习8---md5校验,sort排序,uniq命令详解
一.校验与核实 目前最为出名的校验技术是md5sum和sha1sum,它们对文件内容使用相应的算法来生成校验和. 举例: amosli@amosli-pc:~/learn$ md5sum text.t ...
- 【java】详解集合
目录结构: contents structure [-] 集合概述 什么是集合 Collection和Map的区别 List和Set的区别 ArrayList和LinkedList的区别 HashSe ...
- MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...
- Spark+Hadoop+IDE环境搭建
下载地址:https://download.csdn.net/download/u014028392/8841545
- 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版
代码走查25条疑问 代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...
- MySQL-innodb_flush_log_at_trx_commit
有效取值为0.1.2.建议设置为1 -1:执行commit的时将重做日志缓冲区同步写到磁盘,即伴有fsync调用 -2:执行commit的时将重做日志异步写到磁盘,即先写到文件系统的缓冲中(因为文件系 ...
- 【转载】mysql配置模板(my-*.cnf)参数详细说明
原文:https://yq.aliyun.com/ziliao/142086 mysql 性能优化分享,好文章: http://www.jb51.net/article/28363.htm mysql ...
- Python dict 出现 Key error
解决方法: https://www.polarxiong.com/archives/Python-%E6%93%8D%E4%BD%9Cdict%E6%97%B6%E9%81%BF%E5%85%8D%E ...