首页 > 其他 > 详细

startActivityForResult的用法和demo

时间:2014-07-21 00:18:59      阅读:437      评论:0      收藏:0      [点我收藏+]

有时候我们需要把A activity提交数据给B  activity处理,然后把结果返回给A

 

这种方式在很多种情况需要用到,比如我应用的程序需要有拍照上传的功能。

 

一种解决方案是  我的应用程序 〉调用系统相机〉拍照〉关闭系统相机〉返回拍照后的照片到我的应用程序

 

bubuko.com,布布扣bubuko.com,布布扣

 

 

 

启动一个ForResult的意图:

Intent intent = new Intent(MainAcitvity.this,
      RequestActivity.class);
    //发送意图标示为REQUSET=1
    startActivityForResult(intent, REQUSET);

 

 

B Activity处理数据:

 

Intent intent=new Intent();
    intent.putExtra(KEY_USER_ID, et01.getText().toString());
    intent.putExtra(KEY_USER_PASSWORD, et02.getText().toString());
setResult(RESULT_OK, intent);
 finish();

 

 

那么startActivityForResult如何使用呢:

 

package com.cn.activityresult.demo;  
  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
import android.widget.Toast;  
  
public class MainAcitvity extends Activity {  
  
    private Button btn01;  
    private TextView tv01;  
    public static final int REQUSET = 1;  
  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        super.onActivityResult(requestCode, resultCode, data);  
        //requestCode标示请求的标示   resultCode表示有数据  
        if (requestCode == MainAcitvity.REQUSET && resultCode == RESULT_OK) {  
            String str = "账号"  
                    + data.getStringExtra(RequestActivity.KEY_USER_ID) + "\n"  
                    + "密码"  
                    + data.getStringExtra(RequestActivity.KEY_USER_PASSWORD);  
            tv01.setText(str);  
        }  
        Toast.makeText(  
                this,  
                "requestCode=" + requestCode + ":" + "resultCode=" + resultCode,  
                Toast.LENGTH_LONG).show();  
    }  
  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        btn01 = (Button) findViewById(R.id.btn01);  
        tv01 = (TextView) findViewById(R.id.tv01);  
        btn01.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                Intent intent = new Intent(MainAcitvity.this,  
                        RequestActivity.class);  
                //发送意图标示为REQUSET=1  
                startActivityForResult(intent, REQUSET);  
            }  
        });  
  
    }  
}  

 

 

 

import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
  
public class RequestActivity extends Activity {  
      
    private Button btn01;  
    private EditText et01;  
    private EditText et02;  
    public static final String KEY_USER_ID="KEY_USER_ID";  
    public static final String KEY_USER_PASSWORD="KEY_USER_PASSWORD";  
      
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.request_activity);  
        btn01=(Button)findViewById(R.id.btn01);  
        et01=(EditText)findViewById(R.id.et01);  
        et02=(EditText)findViewById(R.id.et02);  
          
          
        btn01.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                Intent intent=new Intent();  
                intent.putExtra(KEY_USER_ID, et01.getText().toString());  
                intent.putExtra(KEY_USER_PASSWORD, et02.getText().toString());  
                setResult(RESULT_OK, intent);  
                finish();  
            }  
        });           
    }  
}  
 

 

startActivityForResult的用法和demo,布布扣,bubuko.com

startActivityForResult的用法和demo

原文:http://www.cnblogs.com/niray/p/3857277.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!