新建联系人
在Android系统中,不仅需要把数据传递给不同页面,也经常会打开一个新的页面,把新页面中的数据回传给调用者。传递数据与回传数据同样使用的都是Intent对象。
创建主界面MainActivity,该Activity有一个Button按钮,点击进入第二个页面并将第二个页面的数据显示在界面上。
创建第二个界面SecondActivity,该Activity主要用于输入信息、从图库选择图片并将数据回传给MainActivity。
主界面:
activity_main.xml
<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"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp" >
<ImageView
android:id="@+id/ic_head"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_phone"
android:layout_below="@+id/tv_phone"
android:layout_marginTop="15dp"
android:text="email: "
android:textSize="18dp" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ic_head"
android:layout_marginBottom="21dp"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/ic_head"
android:text="姓名"
android:textSize="18dp" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ic_head"
android:layout_marginTop="22dp"
android:text="电话: "
android:textSize="18dp" />
RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:onClick="click"
android:text="新建联系人" />
RelativeLayout>
MainActivity.java
package com.example.asus.newlinkman;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv_name, tv_email, tv_phone;
private ImageView iv_head;
private String name;
private String email;
private byte[] imageByte;
private Bitmap bitmap;
private String phone_et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取页面控件
tv_name = (TextView) findViewById(R.id.tv_name);
tv_email = (TextView) findViewById(R.id.tv_email);
tv_phone = (TextView) findViewById(R.id.tv_phone);
iv_head = (ImageView) findViewById(R.id.ic_head);
}
//建立新联系人触发click点击事件
public void click(View view) {
//显示意图
Intent intent = new Intent(this, SecondActivity.class);
//期望数据回传,见方法onActivityResult,接收回传数据
startActivityForResult(intent,1);
}
//重写onActivityResult方法,接受回传数据
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
if (resultCode == 1) {//标识回传数据
if (requestCode == 1) {//与startActivityForResult(intent,1)方法的第二个参数对应
try {
name = data.getStringExtra("name");
email = data.getStringExtra("email");
phone_et = data.getStringExtra("phone");
imageByte = data.getByteArrayExtra("image");
//包装imageByte
bitmap = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
} catch (Exception e) {
//抛出异常
}
//编辑本界面数据,即显示回传数据
if (name != null) {
tv_name.setText("姓名 :" + name);
}
if (email != null) {
tv_email.setText("email :" + email);
}
if (phone_et != null) {
tv_phone.setText("电话号码 :" + phone_et);
}
if (bitmap != null) {
iv_head.setImageBitmap(bitmap);
}
}
}
}
}
}
编辑界面:
activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rl_head"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F0EDE5" >
<TextView
android:id="@+id/tv_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="新建联系人"
android:textColor="#86743f" />
<View
style="@style/view_wire"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_head" />
RelativeLayout>
<RelativeLayout
android:id="@+id/rl_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_head"
android:background="#e6e6e6" >
<ImageView
android:id="@+id/iv_select"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:src="@drawable/ic_launcher" />
<EditText
android:id="@+id/et_company"
style="@style/edtTxtStyle"
android:layout_alignBottom="@+id/iv_select"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_toRightOf="@+id/iv_select"
android:ems="10"
android:hint="公司或标签" />
<EditText
android:id="@+id/et_name"
style="@style/edtTxtStyle"
android:layout_alignLeft="@+id/et_company"
android:layout_alignTop="@+id/iv_select"
android:ems="10"
android:hint="姓名" >
<requestFocus />
EditText>
<View
style="@style/view_wire"
android:layout_alignLeft="@+id/et_name"
android:layout_centerVertical="true" />
RelativeLayout>
<View
android:id="@+id/view_wire3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_telephoneHint"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#e3e0d8" />
<TextView
android:id="@+id/tv_telephoneHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/rl_user"
android:layout_marginLeft="18dp"
android:layout_marginTop="20dp"
android:text="电话" />
<TextView
android:id="@+id/tv_phoneHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/view_wire3"
android:layout_marginLeft="18dp"
android:layout_marginTop="20dp"
android:text="手机 :" />
<View
android:id="@+id/view_wire4"
style="@style/view_wire"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_emailHint"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/tv_emailHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_phoneHint"
android:layout_marginLeft="18dp"
android:layout_marginTop="30dp"
android:text="邮箱" />
<TextView
android:id="@+id/tv_firmHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/view_wire4"
android:layout_marginLeft="18dp"
android:layout_marginTop="20dp"
android:text="公司 :" />
<EditText
android:id="@+id/et_email"
style="@style/edtTxtStyle"
android:layout_alignBottom="@+id/tv_firmHint"
android:layout_alignLeft="@+id/et_phone"
android:ems="10"
android:hint="电子邮件"
android:inputType="textEmailAddress" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#000000" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:layout_marginTop="20dp"
android:onClick="complete"
android:text="完成"
android:textSize="18dp" />
RelativeLayout>
<EditText
android:id="@+id/et_phone"
style="@style/edtTxtStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_phoneHint"
android:layout_alignRight="@+id/view_wire3"
android:ems="10"
android:hint="电话号码"
android:inputType="phone" />
RelativeLayout>
SecondActivity.java
package com.example.asus.newlinkman;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
public class SecondActivity extends Activity {
private EditText et_name, et_phone, et_email;
private ImageView select_ico;
private byte[] bitmapToBytes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
et_name = (EditText) findViewById(R.id.et_name);
et_phone = (EditText) findViewById(R.id.et_phone);
et_email = (EditText) findViewById(R.id.et_email);
select_ico = (ImageView) findViewById(R.id.iv_select);
//头像点击事件,选择头像
select_ico.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
}
//完成编辑,点击“完成”按钮触发事件
public void complete(View view) {
//获取数据
String name = et_name.getText().toString().trim();
String phone = et_phone.getText().toString().trim();
String email = et_email.getText().toString().trim();
Intent intent = new Intent();
//包装数据
intent.putExtra("name", name);
intent.putExtra("phone", phone);
intent.putExtra("email", email);
intent.putExtra("image", bitmapToBytes);
setResult(1, intent);//回传数据
this.finish();//销毁当前Activity
}
//私有方法,进行图片格式转换
private byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
//接受上传的头像数据
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
select_ico.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Bitmap image = ((BitmapDrawable) select_ico.getDrawable())
.getBitmap();
bitmapToBytes = Bitmap2Bytes(image);
}
}
}
布局样式:
-- Base application theme. -->