回调函数使用,使得代码变得部分跟不变部分更加明显,代码整体上有一个质的提升。

我是用抽象类里写的抽象回调函数,通过通知注册接口形式,可以随时传递数据,接受数据。

同时我用接口函数回调更新UI。下面请看代码。

抽象类CallBackListenner.java

package com.example.recallbackdemo;//回调抽象类CallBackListennerpublic abstract class CallBackListenner {// 回调函数public abstract void CallBackTime(int ntime);}
接口类OnTimeUpdateListInterface.java
package com.example.recallbackdemo;//更新UI接口public interface OnTimeUpdateListInterface {//抽象回调方法     public abstract void UpdateUIInterface(int ntime);}


时间管理类TimeManage.java

package com.example.recallbackdemo;import android.os.Handler;public class TimeManage {private int i = 0;private CallBackListenner mcallBackListenner = null;// 注册通知回调函数public void ReCallBackListenner(CallBackListenner callBackListenner) {mcallBackListenner = callBackListenner;}Handler handler = new Handler();// 将要执行的操作写在线程对象的run方法当中Runnable updateThread = new Runnable() {public void run() {// 停留1秒handler.postDelayed(updateThread, 1000);mcallBackListenner.CallBackTime(i);i++;}};// 开始计时public void StartTime() {if (i == 0) {handler.post(updateThread); // 启动线程}}// 结束计时public void EndTime() {handler.removeCallbacks(updateThread);i = 0;mcallBackListenner.CallBackTime(i);}}


MainActivity

package com.example.recallbackdemo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnTimeUpdateListInterface {private TimeManage mTimeManage = new TimeManage();;private int mntime = 0;private TextView mtvTime;private Handler mHandlerUI = new Handler() {public void handleMessage(Message msg) {Bundle dataBundle = msg.getData();if (dataBundle == null)return;mntime = dataBundle.getInt("time");if (mntime >= 0)mtvTime.setText(String.valueOf(mntime));}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mtvTime = (TextView) findViewById(R.id.tv_time);// 回调函数接受函数mTimeManage.ReCallBackListenner(new CallBackListenner() {@Overridepublic void CallBackTime(int ntime) {mntime = ntime;UpdateUIInterface(mntime);}});}// 按键开始监听public void Start(View v) {if (mntime > 0) {Toast.makeText(getApplicationContext(), "已经开始计时....",Toast.LENGTH_LONG).show();} else {mTimeManage.StartTime();}}// 按键结束监听public void End(View v) {mTimeManage.EndTime();}// 实现接口类里的抽象函数,用来回调更新ui内容public void UpdateUIInterface(int ntime) {Message msg = new Message();Bundle dataBundle = new Bundle();dataBundle.putInt("time", ntime);msg.setData(dataBundle);mHandlerUI.sendMessage(msg);}}

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout2"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="100dp"        android:gravity="center" >        <TextView            android:id="@+id/tv_time"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="66sp"            android:text="0" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:gravity="center" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="Start"            android:text="开始" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="End"            android:text="结束" />    </LinearLayout></LinearLayout>



这里还是用了线程实时更新UI。

demo还是前一章计时器,实现方法用了回调机制。

转载请注明出处:http://blog.csdn.net/qq_16064871

项目下载地址:http://download.csdn.net/detail/qq_16064871/8513331

更多相关文章

  1. Android(安卓)常用开发类库
  2. 新浪微博2.5.1 for Android(安卓)去广告
  3. Android-telephony各文件解释
  4. Android(安卓)Lame c库应用
  5. android中dumpsys函数介绍与使用
  6. 小多的Android入门教程系列---之1---贪吃蛇改进版
  7. Mac及Android环境下的JNI学习
  8. Android实现发送短信验证码倒计时功能示例
  9. android jni 实现

随机推荐

  1. Android(安卓)XMl写入
  2. Android(安卓)intent
  3. [android] No resource found that match
  4. java.util.zip.ZipException: duplicate
  5. Android(安卓)-- Wifi连接流程分析
  6. android中的autocomplete例子小结
  7. android 应用嵌入 admob 广告
  8. Android介绍
  9. 设置环境变量
  10. android之实现各个组件点击事件处理