Android返回传值--老司机手把手教大家

1,建立当前的界面内容,获取到当前界面的指定需要传的值,我这儿的值获取的是服务器的值,先上代码看看;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * Created by rnd on 2018/2/27.
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout back;
    private TextView json_one,json_to,json_there,jsonfire,json_sex;
    private Button buttom_canle;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        handler=new MyHandler();
        json_one=(TextView)findViewById(R.id.json_one);
        json_to=(TextView)findViewById(R.id.json_to);
        json_there=(TextView)findViewById(R.id.json_there);
        jsonfire=(TextView)findViewById(R.id.json_fire);
        json_sex=(TextView)findViewById(R.id.json_sex);
        back=(LinearLayout)findViewById(R.id.back);
        back.setOnClickListener(this);//点击跳转界面
    }

    @Override
    public void onClick(View v) {
        if(v==back){
            String s1 = json_one.getText().toString();//获取到列表的数据值
            String s2 = json_to.getText().toString();
            String s4 = jsonfire.getText().toString();
            String s3 = json_there.getText().toString();
            Intent intent = new Intent(LoginActivity.this, NameActivity.class);
            intent.putExtra("message", s1);//跳转并且传递过去值,健值对形式;
            intent.putExtra("jsonfire", s4);
            intent.putExtra("jsonto", s2);
            intent.putExtra("json_there", s3);
            //启动Intent
            startActivityForResult(intent, 1000);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {//这个非常重要的接收返回过来的数据值onActivityResult函数;
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == 1001) {
            String result = data.getStringExtra("result");
            json_one.setText(result);
            String fire = data.getStringExtra("fire");
            jsonfire.setText(fire);
            String listool = data.getStringExtra("listool");
            json_to.setText(listool);
            String noneres1 = data.getStringExtra("noneres1");
            json_there.setText(noneres1);
        }
    }
}

现在主界面第一个界面功能传递做好了,然后就是第二个界面了。

public class NameActivity  extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout back,imei_tone;
    private EditText et_name,sms_name,email_name;
    private TextView android_none;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name);
        back = findViewById(R.id.back);
        et_name = findViewById(R.id.et_name);//定义控件初始化
        sms_name = findViewById(R.id.sms_name);
        email_name= findViewById(R.id.email_name);
        imei_tone= findViewById(R.id.imei_tone);
        android_none= findViewById(R.id.android_none);
        back.setOnClickListener(this);//点击返回数据到a界面

        Intent intent = getIntent();//接收a跳转过来的值

        String message = intent.getStringExtra("message");
        et_name.setText(message);//a界面传递过来的值显示到控件上;

        String jsonfire = intent.getStringExtra("jsonfire");
        sms_name.setText(jsonfire);

        String jsonto = intent.getStringExtra("jsonto");
        email_name.setText(jsonto);

        String json_there = intent.getStringExtra("json_there");
        android_none.setText(json_there);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //底部按钮事件点击切换
            case R.id.back:
                String result = et_name.getText().toString();//获取到更改之后的,或者可以说是当前b界面的控件值;
                String result2 = sms_name.getText().toString();
                String result3 = email_name.getText().toString();
                String result1 = android_none.getText().toString();
                Intent intent = new Intent();
                intent.putExtra("result", result);//此时返回b控件上的值到a界面;
                intent.putExtra("fire", result2);
                intent.putExtra("listool", result3);
                intent.putExtra("noneres1", result1);
                setResult(1001, intent);//通过这个1001指令发送
                finish();//关闭当前界面
            break;

        }
    }

nice,大功告成!android返回传值可以顺利的发送和接收到数据了,若对您有所帮助,请点个赞或关注下,还会继续努力更新文章,会把自己所做所学到的实践操作都分享给大家,还有不足之处,尽请谅解!有问题可联系博主,感谢您的阅读!

猜你喜欢

转载自blog.csdn.net/qq_37523448/article/details/79411335