Androidケーススタディ-新しい連絡先
新しい連絡先
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.javapackage 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.javapackage 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. -->