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等 ...
随机推荐
- iOS 常用的几个math函数
1.取整数 double ceil (double); 取上整 double floor (double); 取下整 2.绝对值 double fabs (double);求绝对值 double ca ...
- jquery 滑动取值
JavaScript 滑动条效果 jquery 滚动条插件 仿iphone苹果横行滚动条美化样式商品图片展示
- 进阶之路(基础篇) - 010 Arduino 函数(基本、串口、SPI)
一.基本函数 pinMode(引脚号,模式); digitalWrite(引脚号,电平状态); //默认低电平(或浮空) digitalRead(数字输入端口号); analogRe ...
- 树莓派SD卡制作,并成功启动
需要条件: 硬件部分: 1.SD卡一张(树莓派支持的) 2.树莓派3 B + 一个(其他的版本也是可以的,这里默3B+). 1.下载树莓派镜像(NOOBS_v1_9_2.zip) wget http: ...
- JQuery UI datepicker 使用方法(转)
官方地址:http://docs.jquery.com/UI/Datepicker,官方示例: http://jqueryui.com/demos/datepicker/. 一个不错的地址,用来DIY ...
- ef SQL Server 版本不支持数据类型“datetime2”
我遇到这个问题是在用数据库模型的时候.当时我电脑上是sql2008 通过vs2010建立了一个ADO.net数据库模型,之后在项目上线的时候,临时把数据库换成了sql2005,在添加新闻的时候出现了“ ...
- Android adjustresize全屏无效问题
屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize,在键盘显示时它未将Activity的Screen向上推动,所以你Activity的vie ...
- Oracle 12C -- ADR结构
ADR路径由参数diagnostic_dest参数决定: SQL> show parameter diagnostic_dest NAME TYPE VALUE ---------------- ...
- 利用eclipse+jdk1.8搭建Java开发环境(超具体的)
利用eclipse+jdk1.8搭建Java开发环境 转载请声明出处:http://blog.csdn.net/u013067166/article/details/50267003 引言:eclip ...
- CListCtrl自适应宽度
原文链接: http://blog.csdn.net/benny5609/article/details/1967084 void CListCtrlExDlg::AdjustColumnWidth( ...