Android开发之Button事件实现方法的总结
下面介绍Button事件实现的两种方法
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/myButton1"
- android:text=" 按钮1 "
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/myButton2"
- android:text=" 按钮2 "
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, ButtonDemoActivity!</string>
- <string name="app_name">ButtonDemo</string>
- </resources>
第一种:
ButtonDemoActivity.java
- package com.android.ButtonDemo.activity;
- 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 ButtonDemoActivity extends Activity {
- Button myButton1,myButton2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myButton1=(Button)findViewById(R.id.myButton1);
- myButton2=(Button)findViewById(R.id.myButton2);
- //使用匿名类注册Button事件
- myButton1.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
- }
- });
- myButton2.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show();
- }
- });
- }
- }
第二种:
ButtonDemoActivity.java
- package com.android.ButtonDemo.activity;
- 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 ButtonDemoActivity extends Activity {
- Button myButton1,myButton2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myButton1=(Button)findViewById(R.id.myButton1);
- myButton2=(Button)findViewById(R.id.myButton2);
- myButton1.setOnClickListener(new ButtonClick());
- myButton2.setOnClickListener(new ButtonClick());
- }
- //创建一个类,来响应OnClickListener
- class ButtonClick implements OnClickListener
- {
- public void onClick(View v)
- {
- switch (v.getId()) {
- case R.id.myButton1:
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
- break;
- case R.id.myButton2:
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show();
- break;
- default:
- break;
- }
- }
- }
- }
Android开发之Button事件实现方法的总结的更多相关文章
- Android开发之onClick事件的实现
算是从2015年开始学习android开发,目前把onClick的事件实现写下来,记录下,以备参考. 实现button的点击功能,让textView显示一行文字,最简单的onClick事件. 直接贴代 ...
- Android开发之SQLite的使用方法
前言 SQLite是一种轻量级的小型数据库,虽然比较小,但是功能相对比较完善,一些常见的数据库基本功能也具有,在现在的嵌入式系统中使用该数据库的比较多,因为它占用系统资源很少.Android系统中也不 ...
- Android开发之onClick事件的三种写法
package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...
- Android开发之onClick事件的三种写法(转)
package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...
- Android开发之Touch事件分发机制
原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
- Android 开发之旅:深入分析布局文件&又是“Hello World!”
http://www.cnblogs.com/skynet/archive/2010/05/20/1740277.html 引言 上篇可以说是一个分水岭,它标志着我们从Android应用程序理论进入实 ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之Java必备基础
Android开发之Java必备基础 Java类型系统 Java语言基础数据类型有两种:对象和基本类型(Primitives).Java通过强制使用静态类型来确保类型安全,要求每个变量在使用之前必须先 ...
随机推荐
- [18] 螺旋楼梯(Spiral Stairs)图形的生成算法
顶点数据的生成 bool YfBuildSpiralStairsVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint sli ...
- OTL使用指南
1 OTL简介 OTL 是 Oracle, Odbcand DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持当前所有的各种主流数据库, ...
- 【手势识别】简介 GestureDetector ScaleGestureDetector
2017-3-6 单点触摸手势识别器GestureDetector 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等.一般情况下,我们可以通过View或Activ ...
- 九度OJ 打印日期 (模拟)
题目1186:打印日期 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4284 解决:1483 题目描写叙述: 给出年分m和一年中的第n天,算出第n天是几月几号. 输入: 输入包含两个整数 ...
- LeetCode【7】.Reverse Integer--java实现
Reverse Integer 题目要求:给定一个int 类型值,求值的反转,例如以下: Example1: x = 123, return 321 Example2: x = -123, ...
- Python源代码 -- C语言实现面向对象编程(基类&派生类&多态)
背景 python是面向对象的解释性语言.然而python是通过C语言实现的,C语言怎么跟面向对象扯上了关系? C语言能够实现面向对象的性质? 原文链接:http://blog.csdn.net/or ...
- unity3d插件Daikon Forge GUI 中文教程-3-基础控件Button和Sprite的使用
(游戏蛮牛首发)大家好我是孙广东.官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上.要观看是须要FQ的. 只是教 ...
- 理解进程调度时机跟踪分析进程调度与进程切换的过程(Linux)
----------------------------------------------------------------------------------- 理解进程调度时机跟踪分析进程调度 ...
- 【转载】Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法
原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stack Overflow找到答案(源地址).把解决方案翻 ...
- mac 终端 使用 gnu coreutils 工具 ls 颜色显示
mac 终端默认 ls 命令无颜色显示: 1: 使用 ls -G 可以显示基本颜色 2:使用 gnu coreutils 工具 mac 终端 使用 gnu coreutils 工具 ls 颜色显示 以 ...