Android中回調(diào)接口的使用介紹
更新時(shí)間:2013年06月17日 16:40:49 作者:
回調(diào)接口在完成某些特殊的功能時(shí)還是蠻有用的,下面為大家分享下具體的使用方法,感興趣的朋友可以參考下哈
MainActivity如下:
package cn.testcallback;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
/**
* Demo描述:
* Android中回調(diào)接口的使用
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
for (int i = 0; i < 10000; i++) {
if (i == 9527) {
showToast(i, new CallBackInterface() {
@Override
public void callBackFunction(int i) {
Toast.makeText(MainActivity.this, "我的編號(hào):"+i, Toast.LENGTH_LONG).show();
}
});
}
}
}
//定義函數(shù),其中一個(gè)參數(shù)為CallBackInterface類型
private void showToast(int i, CallBackInterface callBackInterface) {
callBackInterface.callBackFunction(i);
}
//定義接口.且在接口中定義一個(gè)方法
public interface CallBackInterface {
public void callBackFunction(int i);
}
}
main.xml如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerInParent="true"
/>
</RelativeLayout>
復(fù)制代碼 代碼如下:
package cn.testcallback;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
/**
* Demo描述:
* Android中回調(diào)接口的使用
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
for (int i = 0; i < 10000; i++) {
if (i == 9527) {
showToast(i, new CallBackInterface() {
@Override
public void callBackFunction(int i) {
Toast.makeText(MainActivity.this, "我的編號(hào):"+i, Toast.LENGTH_LONG).show();
}
});
}
}
}
//定義函數(shù),其中一個(gè)參數(shù)為CallBackInterface類型
private void showToast(int i, CallBackInterface callBackInterface) {
callBackInterface.callBackFunction(i);
}
//定義接口.且在接口中定義一個(gè)方法
public interface CallBackInterface {
public void callBackFunction(int i);
}
}
main.xml如下:
復(fù)制代碼 代碼如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerInParent="true"
/>
</RelativeLayout>
您可能感興趣的文章:
- 詳細(xì)介紹Android中回調(diào)函數(shù)機(jī)制
- Android的Fragment的生命周期各狀態(tài)和回調(diào)函數(shù)使用
- 理解Android中Activity的方法回調(diào)
- android自定義控件和自定義回調(diào)函數(shù)步驟示例
- 深入淺析Android接口回調(diào)機(jī)制
- Android觀察者模式實(shí)例分析
- android開發(fā)中使用java觀察者模式
- Android源碼學(xué)習(xí)之觀察者模式應(yīng)用及優(yōu)點(diǎn)介紹
- 詳解Android中接口回調(diào)、方法回調(diào)
- Android回調(diào)與觀察者模式的實(shí)現(xiàn)原理
相關(guān)文章
Android實(shí)現(xiàn)帶有刪除按鈕的EditText示例代碼
本文給大家介紹一個(gè)很實(shí)用的小控件,就是在Android系統(tǒng)的輸入框右邊加入一個(gè)小圖標(biāo),點(diǎn)擊小圖標(biāo)可以清除輸入框里面的內(nèi)容,IOS上面直接設(shè)置某個(gè)屬性就可以實(shí)現(xiàn)這一功能,但是Android原生EditText不具備此功能,所以要想實(shí)現(xiàn)這一功能我們需要重寫EditText。下面來看看吧。2016-12-12
Android網(wǎng)絡(luò)數(shù)據(jù)開關(guān)用法簡單示例
這篇文章主要介紹了Android網(wǎng)絡(luò)數(shù)據(jù)開關(guān)用法,通過自定義函數(shù)調(diào)用系統(tǒng)服務(wù)實(shí)現(xiàn)開關(guān)功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Eclipse工程轉(zhuǎn)為兼容Android Studio模式的方法步驟圖文詳解
這篇文章主要介紹了Eclipse工程轉(zhuǎn)為兼容Android Studio模式的方法步驟,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-12-12
Android利用Sensor實(shí)現(xiàn)傳感器功能
這篇文章主要為大家詳細(xì)介紹了Android利用Sensor實(shí)現(xiàn)傳感器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Android開發(fā)實(shí)現(xiàn)在Wifi下獲取本地IP地址的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)在Wifi下獲取本地IP地址的方法,涉及Android編程Wifi的調(diào)用及IP地址的獲取與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android Retrofit和Rxjava的網(wǎng)絡(luò)請(qǐng)求
這篇文章主要介紹了Android Retrofit和Rxjava的網(wǎng)絡(luò)請(qǐng)求的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android應(yīng)用程序窗口(Activity)窗口對(duì)象(Window)創(chuàng)建指南
本文將詳細(xì)介紹Android應(yīng)用程序窗口(Activity)的窗口對(duì)象(Window)的創(chuàng)建過程,需要了解的朋友可以參考下2012-12-12

