网站建设资讯

NEWS

网站建设资讯

onResultActivity

最近在做一个项目,里面很多地方都用到了函数回调机制

成都创新互联-专业网站定制、快速模板网站建设、高性价比青云谱网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式青云谱网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖青云谱地区。费用合理售后完善,十多年实体公司更值得信赖。

现在做一个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按钮 没有其他的布局了


网页名称:onResultActivity
网站网址:http://njwzjz.com/article/pjohdo.html