您好,登錄后才能下訂單哦!
最近在做一個項目,里面很多地方都用到了函數回調機制
現在做一個demo說明一下
package com.example.testresult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ResultActivity extends Activity implements OnClickListener{
private Button btn1;
private Button btn2;
private EditText edt1;
private EditText edt2;
private int REQUEST_TO_ACTIVITY_A = 0;
private int REQUEST_TO_ACTIVITY_B = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
edt1 = (EditText) findViewById(R.id.editText1);
edt2 = (EditText) findViewById(R.id.editText2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
//這里函數命名是固定的 onActivityResult 而我剛開始命名為onActivityForResult導致一直收不到數據
//三個參數的意義
//requestCode 這個是請求回調指令,該指令可以自己命名,用于判斷是哪個請求
//resultCode 這個是結果回復指令
//data 里面存放回調的數據 在接收回調數據時 應用 Bundle extra = data.getExtras(); 這里容易出現錯誤
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_TO_ACTIVITY_A){
switch(resultCode){
case RESULT_OK:
Bundle extra = data.getExtras();
edt1.setText(extra.getString("name"));
break;
}
}else if(requestCode == REQUEST_TO_ACTIVITY_B){
switch(resultCode){
case RESULT_OK:
Bundle extra = data.getExtras();
edt2.setText(extra.getString("name"));
break;
}
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.equals(btn1)){
Intent intent = new Intent(ResultActivity.this, Activity_A.class);
startActivityForResult(intent, REQUEST_TO_ACTIVITY_A);
}else if(arg0.equals(btn2)){
Intent intent = new Intent(ResultActivity.this, Activity_B.class);
startActivityForResult(intent, REQUEST_TO_ACTIVITY_B);
}
}
}
package com.example.testresult;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Activity_A extends Activity implements android.view.View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_a);
btn = (Button) findViewById(R.id.button1_a);
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.equals(btn)){
Intent intent = new Intent();
intent.putExtra("name", "從Activity_A得到的回調數據");
setResult(RESULT_OK, intent);
finish();
}
}
}
package com.example.testresult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Activity_B extends Activity implements android.view.View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_b);
btn = (Button) findViewById(R.id.button1_b);
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.equals(btn)){
Intent intent = new Intent();
intent.putExtra("name", "從Activity_B得到的回調數據");
setResult(RESULT_OK, intent);
finish();
}
}
}
//Activity_A、Activity_B的xml布局文件里只有一個Button按鈕 沒有其他的布局了
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。