IntentTest

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:id="@+id/name"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="张三"
    android:background="#0000aa"
    />
<TextView  
	android:id="@+id/age"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="21"
    android:background="#00aa00"
    />
    <Button
    android:id="@+id/enter"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="开始传值"/>
</LinearLayout>

othermain.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:id="@+id/name1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="等待传值"
    android:background="#0000aa"
    />
<TextView  
	android:id="@+id/age1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="等待传值"
    android:background="#00aa00"
    />
        <Button
    android:id="@+id/back"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="返回值"/>
</LinearLayout>
package com.hyz;

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 IntentTest extends Activity {
	private TextView name = null;
	private TextView age = null;
	private Button enter = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        name = (TextView) findViewById(R.id.name);
        age = (TextView) findViewById(R.id.age);
        enter = (Button)findViewById(R.id.enter);
        
        enter.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        	     Intent intent = new Intent();
        	        Bundle bundle = new Bundle();
        	        bundle.putString("Name", name.getText().toString());
        	        bundle.putString("Age", age.getText().toString());
        	        intent.putExtras(bundle);
        	        intent.setClass(IntentTest.this, OtherIntent.class);
        	       // startActivity(intent);
        	        
        	        startActivityForResult(intent, 1);
        	}
        });
           
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode==1)
			{
				if(resultCode==2)
				
					setTitle("Cancel****");
				else
					if(resultCode==3)
					{
						Bundle bundle1 = new Bundle();
						bundle1 = data.getExtras();
						age.setText(bundle1.getString("age"));					
					}
			}
	}
}

package com.hyz;

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 OtherIntent extends Activity {
	private TextView name = null;
	private TextView age = null;
	private Button back = null;	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.othermain);
        
        name = (TextView) findViewById(R.id.name1);
        age = (TextView) findViewById(R.id.age1);
        back = (Button)findViewById(R.id.back);
       
        Bundle bundle = getIntent().getExtras();
        name.setText(bundle.getString("Name"));
        
        back.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        	     Intent intent = new Intent();
        	     Bundle bundle = new Bundle();
        	     bundle.putString("age", "传回来了一个age");
        	     intent.putExtras(bundle);
        	     setResult(3,intent);
        	     finish();
        	}
        });
    }
}






 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.intenttest, PID: 8155 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.intenttest/com.example.intenttest.MyActivity}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2049) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1709) at android.app.Activity.startActivityForResult(Activity.java:5192) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:597) at android.app.Activity.startActivityForResult(Activity.java:5150) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:583) at android.app.Activity.startActivity(Activity.java:5521) at android.app.Activity.startActivity(Activity.java:5489) at com.example.intenttest.MainActivity$2.onClick(MainActivity.java:60) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
05-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值