Android笔记

Android 利用Intent 两界面互相传值

2019-07-23  本文已影响0人  Cedric_h

原文:https://blog.csdn.net/uyy203/article/details/52621599

大致思路

界面A
MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Button click;
    private EditText info;
    private TextView come;

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

        click=(Button) findViewById(R.id.click);
        info=(EditText) findViewById(R.id.info);
        come=(TextView) findViewById(R.id.come);

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();


                intent.putExtra("msg",info.getText().toString());



                intent.setClass(MainActivity.this,ReceiveActivity.class);
//                startActivity(intent);
                startActivityForResult(intent,0);//跳转并发送请求码


            }
        });



    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        come.setText(data.getExtras().get("back").toString());//获得返回信息,并刷新UI
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.poco.transfer.MainActivity">


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


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me"
        android:id="@+id/click"
        android:layout_below="@id/info"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/come"
        android:text="null"
        android:layout_below="@id/click"
        />
</RelativeLayout>

界面B
ReceiveActivity.java

public class ReceiveActivity extends AppCompatActivity {
    private TextView tv;
    private String s;
    private Button back;


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

        tv=(TextView) findViewById(R.id.tv);
        back=(Button) findViewById(R.id.back);

        s=getIntent().getStringExtra("msg");
        tv.setText(s);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.putExtra("back","back->"+s);


                setResult(1,intent);//发送返回码

                finish();//关闭当前界面
            }
        });


    }
}

activity_receive.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.poco.transfer.ReceiveActivity">

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back"
        android:id="@+id/back"
        android:layout_below="@id/tv"
        />

</RelativeLayout>
上一篇下一篇

猜你喜欢

热点阅读