注: 本系列文章以实例分析为主,按照步骤完成,可以直接运行。
例子:完成两个整数的相加,并把结果显示出来。
1.新建一个Android工程。
2.编辑res/layout/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"
>
<EditText
android:id="@+id/int1"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="0"
/>
<EditText
android:id="@+id/int2"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="0"
/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="执行"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结果是"
/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
/>
</LinearLayout>
</LinearLayout>
3.编辑src目录下的源文件。
package com.taisen.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.TextView;
public class CallBackAddMethodActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.submit);
OnClickListener l = new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
TextView int1 = (TextView)findViewById(R.id.int1);
TextView int2 = (TextView)findViewById(R.id.int2);
TextView result = (TextView)findViewById(R.id.result);
int a,b;
a = Integer.parseInt(int1.getText().toString());
b = Integer.parseInt(int2.getText().toString());
result.setText(String.valueOf(a+b));
}
};
b.setOnClickListener(l);
}
}
这个继承Activity的类完成了用户单击事件的响应。
通过调用OnClickListener接口,实现唯一的方法onClick(View v)。最后使用setOnClickListener()将该对象对应到View上。
分享到:
相关推荐
Java回调函数的理解。通过在Android小程序下运行一个Java回调程序。代码内部有简单讲解。
android 不同activity切换时它们的回调函数调用逻辑的研究
博客《PullToRefresh使用详解(四)--利用回调函数实现到底加载》对应的源码,博客地址:http://blog.csdn.net/harvic880925/article/details/17792755
主要介绍了Android编程之匿名内部类与回调函数用法,结合实例形式分析了Android编程中所涉及的java匿名内部类与回调函数的概念、定义、使用方法与相关注意事项,需要的朋友可以参考下
主要介绍了微信小程序中使用javascript 回调函数的相关资料,需要的朋友可以参考下
主要介绍了android自定义控件步骤示例,包括为View类增加属性、响应用户消息、自定义回调函数等方法
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...
android接口回调详解、简单例子+复杂例子
Android项目实战--手机卫士24--程序锁的实现以及逻辑
android-support-v4-v7-v13-v14-v17(官方最新完整版),官方最新版的,压缩包内包含android-support-v4、android-support-v7-appcompat,android-support-v7-cardview,android-support-v7-gridlayout,android-support-...
Android中Preference的使用以及监听事件分析
在Android应用程序中,有一类特殊的消息,是专门负责与用户进行交互的,它们就是触摸屏和键盘等输入事件。触摸屏和键盘事件是统一由系统输入管理器InputManager进行分发的。也就是说,InputManager负责从硬件接收...
Android中获取应用程序(包)的大小
Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明 Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明 Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
我的Android进阶之旅------>Android疯狂连连看游戏的实现 可以参考博客:http://blog.csdn.net/ouyang_peng/article/details/14115627
Android自定义View之组合控件 ---- LED数字时钟
ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar 要是需要最新的,下载地址: http://code.google.com/p/ksoap2-android/
6份GDI+程序设计资源整合[全零分] http://download.csdn.net/album/detail/625 2014年移动游戏行业数据分析 http://download.csdn.net/detail/cleopard/8340331 一文读懂2014年全球互联网广告新生态 ...
Android中Recyclerview使用,Recyclerview在Android开发中非常常用,在条目中如果有按钮,那么按钮的点击事件通过接口回调在,activity中调用