Intent实现页面跳转

上面2是有返回结果,也就是页面A与B有数据传递。

 

1.无返回结果的页面跳转方式:

FirstActivity.java  (主Activity)

package com.example.test2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class FirstActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        Button btn  = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                /**
                 * 第一个参数:上下文(由于匿名内部类不能访问局部变量,所以需要带类名)
                 *             第二种方式是:定义一个全局变量Context,初始化之后进行访问
                 * 第二个参数:目标文件
                 */
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                FirstActivity.this.startActivity(intent);
            }
        });
        
    }

}

 

 first_activity.xml

<?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" >
    
    <Button 
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一种跳转方式"
        />
    
       <Button 
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二种跳转方式"
        />
    
    
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="将第二个页面的结果写到这里"
        />
    

</LinearLayout>

 

 SecondActivity.java  (第二个页面)

package com.example.test2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class SecondActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
    }

}

 

second_activity.xml

<?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" >
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个页面的按钮"
        />
    

</LinearLayout>

 

AndroidManifest.xml  清单文件注册Activity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test2.FirstActivity"
            android:label="第一个界面" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity 
            android:name="com.example.test2.SecondActivity"            
            android:label="第二个界面"
            ></activity>
        
    </application>

</manifest>

 

效果:

 

 

 

 

 2.有返回结果的跳转方式

一个请求码,一个结果码确定是某个页面的回传结果。

 FirstActivity

创建Intent,startActivityForResult是打开第二个页面(带着一个请求参数)

onActivityResult:处理回传的数据(根据请求码与回传码确定一个页面)

 

package com.example.test2;

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

    private TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        Button btn  = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                /**
                 * 第一个参数:上下文(由于匿名内部类不能访问局部变量,所以需要带类名)
                 *             第二种方式是:定义一个全局变量Context,初始化之后进行访问
                 * 第二个参数:目标文件
                 */
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                FirstActivity.this.startActivity(intent);
            }
        });
        
        textView1 = (TextView) findViewById(R.id.textView1);
        Button btn2  = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                /**
                 * 第一个参数:上下文(由于匿名内部类不能访问局部变量,所以需要带类名)
                 *             第二种方式是:定义一个全局变量Context,初始化之后进行访问
                 * 第二个参数:目标文件
                 */
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                /**
                 * intent:目标文件
                 * 1:请求码
                 */
                startActivityForResult(intent, 1);
            }
        });
        
        
        
    }
    
    /*
     * 处理回传的数据:
     * requestCode:请求码
     * resultCode:结果码
     * data:数据(格式像map)
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        //如果请求码是1,结果码是2证明是点击第二个按钮时的页面回传的结果
        if(requestCode==1&&resultCode==2){
            textView1.setText(data.getStringExtra("data"));
        }
        
        
    }
    
}

 

 

 first_activity.xml

<?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" >
    
    <Button 
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一种跳转方式"
        />
    
       <Button 
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二种跳转方式"
        />
    
    
    
    <TextView 
         android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="将第二个页面的结果写到这里"
        />
    

</LinearLayout>

 

 SecondActivity.java

点击按钮的时候,回传数据并且结束自己的页面

package com.example.test2;

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

    private Button btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        btn3 = (Button) findViewById(R.id.sebtn);
        /**
         * 回传到第一个页面实际是一个intent对象
         */
        btn3.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //因为不做页面跳转所以不设置参数
                Intent data = new Intent();
                data.putExtra("data", "你好");
                /**
                 * 回传数据
                 * 第一个参数是回传码,第二个是数据
                 */
                setResult(2, data);
                //结束当前页面,回传到第一个页面
                finish();
                
            }
        });
    }
    
    
    

}

 

 

 second_activity.xml

<?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" >
    
    <Button 
        android:id="@+id/sebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个页面的按钮"
        />
    

</LinearLayout>

 

 

 

效果:

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值