数据传递

一、向下一个活动传递数据

①通过putExtra()方法,把需要传递的数据暂存在Intent中,启动了另一个活动后,再从Intent中取出数据。

package com.example.james.putdatafromintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = "------------------------------------------Hello SecondActivity------------------------------------------";
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("extra_data", data);
                startActivity(intent);
            }
        });

        Log.d("TAG","----------------------------MainActivity6666666----------------------");
    }
}

②通过getIntent()方法获取到用于启动SecondActivity的Intent,然后调用getStringExtra()方法,传入相应的键值,就可以得到传递的数据。

package com.example.james.putdatafromintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = getIntent();
        String data = intent.getStringExtra("extra_data");
        Log.d("SecondActivity", data);
    }
}

二、返回数据给上一个活动

package com.example.james.onactivityresult;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private final static int REQUESTCODE = 1; //返回的结果码
    private EditText one , two, result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        one = (EditText) findViewById(R.id.Text_one);
        two = (EditText) findViewById(R.id.Text_two);
        result = (EditText) findViewById(R.id.Text_result);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入的两个值
                int a = Integer.parseInt(one.getText().toString());
                int b = Integer.parseInt(two.getText().toString());

                //意图实现activity的跳转
                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                intent.putExtra("a", a);
                intent.putExtra("b", b);

                //这种启动方式startActivity(intent),并不能返回结果
                startActivityForResult(intent, REQUESTCODE);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);

        //RESULT_OK,判断OtherActivity已经结束数据输入
        //operation succeeded默认值为-1

        String aa =  String.valueOf(requestCode);
        Log.d("-------requestCode-----", aa);
        if(resultCode  == 2){
            if(requestCode == REQUESTCODE){
                int three = data.getIntExtra("three", 0);
                //设置结果显示框的显示数值
                result.setText(String.valueOf(three));
            }
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/Text_one"
            android:layout_width="80dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="  +  " />

        <EditText
            android:id="@+id/Text_two"
            android:layout_width="80dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="  =  " />

        <EditText
            android:id="@+id/Text_result"
            android:layout_width="80dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="计算结果" />

</LinearLayout>
package com.example.james.onactivityresult;

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;
import android.widget.TextView;

public class OtherActivity extends Activity {

    private Button button;
    private TextView textView;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        button = (Button) findViewById(R.id.button2);
        textView = (TextView) findViewById(R.id.msg);
        editText = (EditText) findViewById(R.id.Text_three);
        // 去除传递过来的意图,并提取数据
        Intent intent = getIntent();//此处并不是创建而是直接获取一个intent对象Return the intent that started this activity.
        int a = intent.getIntExtra("a", 0); // 没有输入值默认为0
        int b = intent.getIntExtra("b", 0); // 没有输入值默认为0
        textView.setText(a + " + " + b + " = " + " ? ");

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                // 获取用户计算后的结果
                int three = Integer.parseInt(editText.getText().toString());
                intent.putExtra("three", three); //将计算的值回传回去
                //通过intent对象返回结果,必须要调用一个setResult方法,
                //setResult(resultCode, data);第一个参数表示结果返回码,一般只要大于1就可以,但是
                setResult(2, intent);

                finish(); //结束当前的activity的生命周期
            }
        });
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/Text_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回结果" />

</LinearLayout>

另外一个示例

package com.example.james.putdatafromintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        Log.d("TAG","----------------------------MainActivity6666666----------------------");

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){

        String aa = String.valueOf(resultCode);
        Log.d("-------------resultCode", aa);
        switch(resultCode){
            case 1:
                if(requestCode == 1){
                    String returnedData = data.getStringExtra("data_return");
                    Log.d("-----------MainActivity", returnedData);
                }
                break;
            default:
        }
    }
}
package com.example.james.putdatafromintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Button button2 = (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("data_return", "-----------------------Hello MainActivity------------------------");
                setResult(1, intent);
                finish();
            }
        });
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值