主Activity程序
package com.example.xml2producttest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button inputButton;
private TextView textView;
private String information=null;
private int INPUT_CONTACT=1;
/** 初始化页面 */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputButton = (Button) findViewById(R.id.Input);
textView = (TextView) findViewById(R.id.textView);
inputButton.setOnClickListener(new inputButtonListener());
}
/** 输入监听器 */
private class inputButtonListener implements OnClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, InputTask.class);
startActivityForResult(intent, INPUT_CONTACT);
}
}
/** 跳转结果 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (20 == resultCode) {
String nameString = data.getExtras().getString("name");
String telephoneString = data.getExtras().getString("telephone");
String addressString = data.getExtras().getString("address");
saveData(nameString, telephoneString, addressString);
information=information+outputData();
textView.setText(information);
super.onActivityResult(requestCode, resultCode, data);
}
}
//存储
private void saveData(String name,String telephone,String address){
try {
OutputStream os=openFileOutput("file.text", Activity.MODE_PRIVATE);
String str="姓名 "+name+",电话 "+telephone+",地址 "+address+"\n";
os.write(str.getBytes("utf-8"));
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
//输出
private String outputData(){
InputStream is;
String str=null;
try {
is = openFileInput("file.text");
byte[] buffer=new byte[100];
int byteCount=is.read(buffer);
str=new String(buffer,0,byteCount,"utf-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
输入Activity
package com.example.xml2producttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class InputTask extends Activity {
private EditText nameEditText;
private EditText telephoneEditText;
private EditText addressEditText;
private Button ensureButton;
protected void onCreate(Bundle savedInstanceState) {
//创建页面
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
//初始化控件
nameEditText = (EditText) findViewById(R.id.name);
telephoneEditText = (EditText) findViewById(R.id.telephone);
addressEditText = (EditText) findViewById(R.id.address);
ensureButton = (Button) findViewById(R.id.ensure);
//设置监听器
ensureButton.setOnClickListener(new ensureButtonListener());
}
private class ensureButtonListener implements OnClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
String nameString = nameEditText.getText().toString();
String telephoneString = telephoneEditText.getText().toString();
String addressString = addressEditText.getText().toString();
Intent data = new Intent();
data.putExtra("name", nameString);
data.putExtra("telephone", telephoneString);
data.putExtra("address", addressString);
setResult(20, data);
finish();
}
}
}
主界面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"
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="com.example.xml2producttest.MainActivity" >
<Button
android:id="@+id/Input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginTop="74dp"
android:text="输入" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Input"
android:layout_marginLeft="21dp"
android:text="输入值" />
</RelativeLayout>
输入界面xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/telephone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/ensure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
</LinearLayout>