分类目录

展开|收起

看你喜欢

(1) (1) (42) (1) (1) (1) (16) (2) (1) (1) (4) (1) (2) (7) (4) (1) (1) (1) (1) (3) (1) (5) (1) (1) (1) (1) (1) (2) (1) (4) (4) (3) (1) (1) (2) (1) (37) (2) (1) (5) (3) (1) (4) (1) (1) (11) (3) (1) (9) (3) (1) (23) (2) (1) (2) (1) (1) (1) (1)

最新精华

《Android应用开发详解》- Chapter05_Activity_StartActivityForResult – 启动另一个Activity并返回结果

有时候我们需要启动另一个Activity,并返回一个结果,这是通过startActivityForResult来实现。例如,在注册会员的时候经常需要多个步骤,常常使用“上一步”、“下一步”的方式来完成,那么但用户选择返回“上一步”时,该如何使之前的设置得以保留并重新显示出来呢?本例就来演示这种情形。

1. 布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView 
android:text="用户名称:" 
android:id="@+id/TextView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></TextView>

<EditText 
android:text="" 
android:id="@+id/username" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></EditText>

<TextView 
android:text="用户密码:" 
android:id="@+id/TextView02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"

></TextView>

<EditText 
android:text="" 
android:id="@+id/password" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:password="true"
></EditText>

<Button 
android:text="下一步" 
android:id="@+id/Button01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></Button>

</LinearLayout>

2. 布局文件next.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView 
android:text="Email:" 
android:id="@+id/email" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></TextView>

<EditText 
android:text="" 
android:id="@+id/EditText01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></EditText>

<TextView 
android:text="Mobile:" 
android:id="@+id/mobile" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></TextView>

<EditText 
android:text="" 
android:id="@+id/EditText02" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></EditText>

<Button 
android:text="上一步" 
android:id="@+id/Button02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></Button>

</LinearLayout>

3. MainActivity.java

package com.amaker.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	private EditText username,password;
	private Button b1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        b1 = (Button)findViewById(R.id.Button01);
        
        b1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				username = (EditText)findViewById(R.id.username);
		        password = (EditText)findViewById(R.id.password);
		       
		        String str_username = username.getText().toString();
		        String str_password = password.getText().toString();
		        
		        Bundle b = new Bundle();
		        b.putString("username", str_username);
		        b.putString("password", str_password);
		        
		        Intent intent = new Intent(MainActivity.this,NextActivity.class);
		        intent.putExtras(b);
		        
				startActivityForResult(intent, 1);
			}
		});
    }
    
    @Override
    protected void onActivityResult(
    		int requestCode, int resultCode, Intent data) {
    	
    		Log.i("requestcode", requestCode+"-----------");
    		
    		Log.i("resultCode", resultCode+"-----------");
    		
    		Bundle b = data.getExtras();
    		
    		String str_username = b.getString("username");
    		String str_password = b.getString("password");
    		
    		System.out.println(str_username);
    		
    		Log.i("abc", data.getStringExtra("abc"));
    		
    		username.setText(str_username);
    		password.setText(str_password);
    }
    
}

4. NextActivity.java

package com.amaker.test;

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;

public class NextActivity extends Activity {
	private Button b2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.next);
		b2 = (Button)findViewById(R.id.Button02);
		b2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = getIntent();
				intent.putExtra("abc", "test");
				NextActivity.this.setResult(5, intent);
				NextActivity.this.finish();
			}
		});
		
	}
}

5. 运行结果

Chapter05_Activity_StartActivityForResult-1

输入用户名和密码:

Chapter05_Activity_StartActivityForResult-2

进入下一个设置界面,这时点击“上一步”返回:

Chapter05_Activity_StartActivityForResult-3

之前设置的用户名和密码仍然得以保留:

Chapter05_Activity_StartActivityForResult-4

Chapter05_Activity_StartActivityForResult实例源码:

Chapter05_Activity_StartActivityForResult
标题: Chapter05_Activity_StartActivityForResult (413 次点击)
文件: Chapter05_Activity_StartActivityForResult.7z
大小: 18 kB
描述:

  打分:5.0/5 (共1人投票)
(浏览总计: 60 次)
Add Comment Register



发表回复

  

  

  

您可以使用这些HTML标签

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>