• 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>

    效果:

  • 相关阅读:
    使用javap分析Java的字符串操作
    使用javap深入理解Java整型常量和整型变量的区别
    分享一个WebGL开发的网站-用JavaScript + WebGL开发3D模型
    Java动态代理之InvocationHandler最简单的入门教程
    Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
    Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
    Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
    Java实现 LeetCode 541 反转字符串 II(暴力大法)
    Java实现 LeetCode 541 反转字符串 II(暴力大法)
    Java实现 LeetCode 541 反转字符串 II(暴力大法)
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/7515984.html
Copyright © 2020-2023  润新知