`
DanielHan
  • 浏览: 54791 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
074641d7-eb86-343f-a745-65a0f693edb5
设计模式
浏览量:7170
社区版块
存档分类
最新评论

Intent用法

 
阅读更多
用法一:跳转后的activity不需要回传参数
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到另一个Activity"/>
</LinearLayout>



receive.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <TextView 
       android:id="@+id/txt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>


Send.java
package com.example.intentdemo;

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 Send extends Activity {

	private Button btn=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send);
		btn=(Button)findViewById(R.id.btn);
		
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(Send.this, Receive.class);
				intent.putExtra("param", "你好!");
				startActivity(intent);
			}
		});
	}
}


Receive.java
package com.example.intentdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Receive extends Activity {

	private TextView txt=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.receive);

		txt=(TextView)findViewById(R.id.txt);
		Intent intent=getIntent();
		txt.setText(intent.getStringExtra("param"));
	}
}


用法二:跳转后的activity需要回传参数



send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
     <TextView 
       android:id="@+id/txt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到Receive1"/>
    <Button 
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到Receive2"/>
</LinearLayout>


receive.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
   <TextView 
       android:id="@+id/txt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
   <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="回到第一个Activity"/>
</LinearLayout>


Send.java
package com.example.intentdemo;

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 Send extends Activity {

	private Button btn1=null;
	private Button btn2=null;
	private TextView txt=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send);
		btn1=(Button)findViewById(R.id.btn1);
		btn2=(Button)findViewById(R.id.btn2);
		txt=(TextView)findViewById(R.id.txt);
		
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(Send.this, Receive1.class);
				intent.putExtra("param", "这是Receive1");
				startActivityForResult(intent, 1);
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(Send.this, Receive2.class);
				intent.putExtra("param", "这是Receive2");
				startActivityForResult(intent, 2);
			}
		});
		
		
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode==RESULT_OK){
			switch(requestCode){
				case 1:
					txt.setText("从Receive1返回数据"+data.getStringExtra("ret"));
					break;
				case 2:
					txt.setText("从Receive2返回数据"+data.getStringExtra("ret"));
					break;
				default:
					break;
			}
				
		}
	}
}


Receive1.java
package com.example.intentdemo;

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 Receive1 extends Activity {

	private TextView txt=null;
	private Button btn=null;
	private Intent intent=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.receive);

		txt=(TextView)findViewById(R.id.txt);
		btn=(Button)findViewById(R.id.btn);
		
		intent=getIntent();
		txt.setText(intent.getStringExtra("param"));
		
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				intent.putExtra("ret", "1");
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
}


Receive2.java
package com.example.intentdemo;

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 Receive2 extends Activity {

	private TextView txt=null;
	private Button btn=null;
	private Intent intent=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.receive);

		txt=(TextView)findViewById(R.id.txt);
		btn=(Button)findViewById(R.id.btn);
		
		intent=getIntent();
		txt.setText(intent.getStringExtra("param"));
		
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				intent.putExtra("ret", "2");
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
}

用法二的结果:






  • 大小: 13.8 KB
  • 大小: 8.2 KB
  • 大小: 6.8 KB
  • 大小: 10.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics