Android程序设计逻辑分析之用户事件响应-回调函数的使用
来源:互联网 发布:手机域名劫持怎么解决 编辑:程序博客网 时间:2024/05/21 08:14
注: 本系列文章以实例分析为主,按照步骤完成,可以直接运行。
例子:完成两个整数的相加,并把结果显示出来。
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上。
- Android程序设计逻辑分析之用户事件响应-回调函数的使用
- 关于JAVA回调函数和Android事件响应
- 关于JAVA回调函数和Android事件响应
- 回调函数中响应MFC ActiveX事件的方法
- 回调函数中响应MFC ActiveX事件的方法
- 利用回调函数响应事件
- 自定义android用户控件,使用回调函数实现自定义事件
- 自定义android用户控件,使用回调函数实现自定义事件
- 自定义android用户控件,使用回调函数实现自定义事件
- 自定义android用户控件,使用回调函数实现自定义事件
- 自定义android用户控件,使用回调函数实现自定义事件
- 使用回调函数简化逻辑的实现
- android 开发零起步学习笔记(十七):自定义android用户控件,使用回调函数实现自定义事件
- android之回调函数的意义
- Android -- 回调函数的使用
- Android中回调函数的使用
- figure的响应回调函数
- Android控件之RecylerView详解,及其点击事件的实现(回调函数)
- C#多线程与异步的区别
- 最近学习SEO的一些收获
- 选择排序算法---对一个数组进行选择排序
- struts2——自定义拦截器
- Android 3.0 访问WebService 出现 android.os.NetworkOnMainThreadException异常
- Android程序设计逻辑分析之用户事件响应-回调函数的使用
- C语言直接驱动硬件实现PC机的串口操作
- SQL Server 的最大容量规范
- LDAP学习资料
- Apache2 httpd.conf 配置文件中文版详解
- ASP.net获取存储过程返回值
- C# ToString总结
- java单例模式(Singleton)
- 怎样使android apk 获取root权限