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動(dòng)態(tài)添加View的問題解決方法
Android動(dòng)態(tài)添加View的問題解決方法,需要的朋友可以參考一下2013-05-05Android 超詳細(xì)深刨Activity Result API的使用
這篇文章主要介紹了Android開發(fā)中Activity Result API的使用,掌握了它以后你就可以放棄startActivityForResult了,感興趣的朋友一起來看看吧2022-03-03Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)
最近需要實(shí)現(xiàn)一個(gè)手機(jī)通訊錄的快速索引功能。根據(jù)姓名首字母快速索引功能,下面通過本篇文章給大家介紹Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)的相關(guān)代碼,需要的朋友參考下2015-12-12判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法
這篇文章主要介紹了判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法,本文直接給出實(shí)現(xiàn)代碼,,需要的朋友可以參考下2015-06-06android自定義控件ImageView實(shí)現(xiàn)圓形圖片
這篇文章主要為大家詳細(xì)介紹了android自定義控件ImageView實(shí)現(xiàn)圓形圖片,適用于用戶頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android?妙用TextView實(shí)現(xiàn)左邊文字,右邊圖片
這篇文章主要介紹了Android?妙用TextView實(shí)現(xiàn)左邊文字,右邊圖片的相關(guān)資料,需要的朋友可以參考下2023-07-07Android Okhttp斷點(diǎn)續(xù)傳面試深入解析
這篇文章主要給大家介紹了關(guān)于Android Okhttp斷點(diǎn)續(xù)傳面試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06