找传奇、传世资源到传世资源站!

android_Intent传值实例_附截图和源码_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

android Intent 参数传值实例

android_Intent传值实例_附截图和源码_ Android平台开发-第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">
	<Button android:id="@ id/button1" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="button1" />
	<Button android:id="@ id/button2" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="button2" />
	<Button android:id="@ id/button3" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="button3" />
</LinearLayout>

second.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
	<Button android:id="@ id/button2" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="button2" />
</LinearLayout>

third.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
	<Button android:id="@ id/button3" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="button3" />
</LinearLayout>

 

后端代码:

MainActivity.java

 

package com.test;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

	android.widget.Button button1;
	android.widget.Button button2;
	android.widget.Button button3;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button1 = (android.widget.Button) this.findViewById(R.id.button1);
		button1.setOnClickListener(this);
		button2 = (android.widget.Button) this.findViewById(R.id.button2);
		button2.setOnClickListener(this);
		button3 = (android.widget.Button) this.findViewById(R.id.button3);
		button3.setOnClickListener(this);
	}

	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.button1:
			android.content.Intent intent = new android.content.Intent();
			intent.putExtra("name", "handson");
			intent.putExtra("age", 1);
			com.pojo.Person person = new com.pojo.Person();
			person.setId(1);
			person.setName("root");
			person.setPassword("root");
			intent.putExtra("person", person);
			intent.setClass(this, SecondActivity.class);
			this.startActivity(intent);
			break;
		case R.id.button2:
			android.content.Intent intent2 = new android.content.Intent();
			intent2.setClass(this, SecondActivity.class);
			this.startActivityForResult(intent2, 300);
			break;
		case R.id.button3:
			android.content.Intent intent3 = new android.content.Intent();
			intent3.setAction("com.android.anbo.xueshu");
			/*
			 * System.out.println(Uri.parse("anbo://xinlong:90").getScheme());
			 * System.out.println(Uri.parse("anbo://xinlong:90").getHost());
			 * System.out.println(Uri.parse("anbo://xinlong:90").getPort());
			 * System
			 * .out.println(Uri.parse("anbo://xinlong:90/xueshu?name=aaaa")
			 * .getQueryParameter("name"));
			 */
			intent3.setData(Uri.parse("anbo://xinlong:90/xueshu?name=aaaa"));
			this.startActivity(intent3);
			break;
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		System.out.println("result");
		System.out.println(requestCode);
		System.out.println(resultCode);
		System.out.println(data.getStringExtra("name"));
	}
}

SecondActivity.java

 

package com.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class SecondActivity extends Activity implements OnClickListener {

	android.widget.Button button2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		/*
		 * System.out.println(this.getIntent().getStringExtra("name"));
		 * System.out.println(this.getIntent().getIntExtra("age", 0));
		 * com.pojo.Person person = (com.pojo.Person) this.getIntent()
		 * .getSerializableExtra("person"); System.out.println(person.getId());
		 * System.out.println(person.getName());
		 * System.out.println(person.getPassword());
		 */

		button2 = (android.widget.Button) this.findViewById(R.id.button2);
		button2.setOnClickListener(this);
	}

	public void onClick(View v) {
		Intent i = new Intent();
		i.putExtra("name", "sa");
		this.setResult(200, i);
		this.finish();
		// android.content.Intent intent = new android.content.Intent();
		// intent.setClass(this, MainActivity.class);
		// this.startActivity(intent);
	}

}

ThridActivity.java

 

package com.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class ThridActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thrid);
		System.out.println(this.getIntent().getData().getScheme());
		System.out
				.println(this.getIntent().getData().getQueryParameter("name"));
	}

}

 

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复