android如何实现两个Activity数据交互?主要是根据Intent的携带功能,intent可以携带很多信息,比如Bundle,URI甚至对象(此时要序列化,并且对象里面的成员变量如果是对象,也要序列化),实现的具体代码如下:
package com.example.yzj.android_8_3; import android.content.Intent; import android.net.Uri; 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.ImageView; public class MainActivity extends AppCompatActivity { private ImageView img; private Button btn_register; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); setListener(); } private void setListener() { btn_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,RegisterActivity.class); startActivityForResult(intent, 0x123); } }); } private void init() { btn_register=(Button)findViewById(R.id.btn_register); img=(ImageView)findViewById(R.id.img); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==0x123&&resultCode==0x124){ String str=data.getStringExtra("uri"); if(str!=null) { Uri uri = Uri.parse(str); img.setImageURI(uri); } } } }
package com.example.yzj.android_8_3; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; /** * Created by YZJ on 2016/8/3. */ public class RegisterActivity extends Activity{ private ImageView img; private Button btn_register; private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_register); init(); setListener(); } private void setListener() { img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent,0x123); } }); btn_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=getIntent(); if(uri!=null){ intent.putExtra("uri",uri.toString()); } setResult(0x124, intent); finish(); } }); } private void init() { img=(ImageView)findViewById(R.id.img); img.setImageResource(R.drawable.a); btn_register=(Button)findViewById(R.id.btn_register); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==0x123&&resultCode==RESULT_OK){ uri=data.getData(); img.setImageURI(uri); } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxWidth="50dp" android:maxHeight="50dp" android:adjustViewBounds="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:src="@mipmap/ic_launcher"/> <EditText android:layout_below="@+id/img" android:id="@+id/et_user" android:layout_width="350dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> <EditText android:layout_below="@+id/et_user" android:id="@+id/et_psw" android:layout_width="350dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> <Button android:id="@+id/btn_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_psw" android:layout_centerHorizontal="true" android:text="注册"/> </RelativeLayout>
一般而言,传递一个标识符,如上面的uri是效率比较高的做法,然而,有时候我们需要传递一个对象,那么,可以用到parcelable去做,具体做法可以参考官方API的bitmap,即位图,bitmap实现了parcelable接口。
时间: 2024-10-24 08:15:22