用OnActivityResult实现更新页面

本文介绍了如何在Android应用程序中利用OnActivityResult方法来实现在用户完成特定操作后更新界面的内容,主要关注于XML布局和交互流程。

主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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值