• Android笔记(二十) Activity中的跳转和值传递


             我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递。

    显式Intent跳转

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity">
    
            </activity>
        </application>
    
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                }
            });
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    </LinearLayout>
    View Code

             从代码中可以看出,我们在一个Activity中,使用startActivity()传入一个Intent对象来实现Activity之间的跳转。

             运行结果:

    隐式Intent跳转

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity">
                <intent-filter>
                    <action android:name="maintosecond" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("maintosecond");
                    startActivity(intent);
                }
            });
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    </LinearLayout>
    View Code

             隐式Intent是在intent中指定激活组件的条件,程序会自动寻找最匹配的组件,然后进行跳转         

             运行结果:

    简单的传值

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity">
    
            </activity>
        </application>
    
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        Intent intent;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intent = new Intent(MainActivity.this, SecondActivity.class);
                    String str = ((EditText)findViewById(R.id.ed_input)).getText().toString();
                    intent.putExtra("data",str);
    
                    startActivity(intent);
                }
            });
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    import org.w3c.dom.Text;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
    
        TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            String str = getIntent().getStringExtra("data");
    
            ((TextView)findViewById(R.id.tv_text_show)).setText(str);
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    </LinearLayout>
    View Code

             运行结果:

             通过代码可以看出,MainActivity使用putExtra(xx,yy)方法将yy传递给SecondActivity,而SecondActivity通过getStringExtra(xx)来接受传递过来的值

    传入多个值

    方法一:继续使用Intent

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity"></activity>
        </application>
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    
    
                    intent.putExtra("string","hello world");
                    intent.putExtra("int",104);
                    startActivity(intent);
                }
            });
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Intent intent = getIntent();
            int i = intent.getIntExtra("int",555);
            String str = intent.getStringExtra("string");
    
    
            ((TextView) findViewById(R.id.tv_text_show)).setText(i + "");
            ((TextView) findViewById(R.id.tv_text_2)).setText(str);
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    
        <TextView
            android:id="@+id/tv_text_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#00ff00"
            android:textSize="90sp"/>
    </LinearLayout>
    View Code

    方法二:使用Bundle

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity"></activity>
        </application>
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    Bundle b = new Bundle();
                    b.putInt("int", 105);
                    b.putString("string", "hello world");
    
                    intent.putExtras(b);
                    startActivity(intent);
                }
            });
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Intent intent = getIntent();
            Bundle b = intent.getExtras();
            String str = b.getString("string");
            int i = b.getInt("int");
    
    
            ((TextView) findViewById(R.id.tv_text_show)).setText(i + "");
            ((TextView) findViewById(R.id.tv_text_2)).setText(str);
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    
        <TextView
            android:id="@+id/tv_text_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#00ff00"
            android:textSize="90sp"/>
    </LinearLayout>
    View Code

             运行结果:

             从代码可以看出,传入多个值可以直接在intent中put各种数据,也可以将数据打包成一个Bundle,然后将Bundle传入到新的Activity,新Activity接收到Bundle之后,可以从Bundle中get到打包的数据。

    传入一个对象

    方法一:Serializable方式 

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity"></activity>
        </application>
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    
                    Person person = new Person();
                    person.setAge(20);
                    person.setName("张三");
    
                    intent.putExtra("person", person);
    
    
                    startActivity(intent);
                }
            });
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Intent intent = getIntent();
            Person person = (Person) intent.getSerializableExtra("person");
            String name = person.getName();
            int age = person.getAge();
    
    
    
            ((TextView) findViewById(R.id.tv_text_show)).setText(age + "");
            ((TextView) findViewById(R.id.tv_text_2)).setText(name);
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    
        <TextView
            android:id="@+id/tv_text_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#00ff00"
            android:textSize="90sp"/>
    </LinearLayout>
    View Code

    Person.java

    package cn.lixyz.activitymanager;
    
    import java.io.Serializable;
    
    /**
     * Created by LGB on 2015/8/28.
     */
    public class Person implements Serializable {
    
        public Person() {
    
        }
    
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    View Code

             运行结果:

    方法二:Parcelable方式 

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity"></activity>
        </application>
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    
                    Person person = new Person();
                    person.setAge(20);
                    person.setName("张三");
                    person.setHeight(180);
    
                    Person person2 = new Person();
                    person2.setAge(10);
                    person2.setName("李四");
                    person2.setHeight(178);
    
                    intent.putExtra("person", person);
                    intent.putExtra("person2",person2);
    
                    startActivity(intent);
                }
            });
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Intent intent = getIntent();
            Person person = intent.getParcelableExtra("person");
            String name = person.getName();
            int age = person.getAge();
            int height = person.getHeight();
    
            Person person2 = intent.getParcelableExtra("person2");
            String name2 = person2.getName();
            int age2 = person2.getAge();
            int height2 = person2.getHeight();
    
            if (age2 > age) {
                ((TextView) findViewById(R.id.tv_text_show)).setText(age2 + "");
                ((TextView) findViewById(R.id.tv_text_2)).setText(name2);
                ((TextView) findViewById(R.id.tv_text_3)).setText(height2 + "");
            }else{
                ((TextView) findViewById(R.id.tv_text_show)).setText(age + "");
                ((TextView) findViewById(R.id.tv_text_2)).setText(name);
                ((TextView) findViewById(R.id.tv_text_3)).setText(height + "");
            }
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    
        <TextView
            android:id="@+id/tv_text_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#00ff00"
            android:textSize="90sp"/>
    
    </LinearLayout>
    View Code

    Person.java

    package cn.lixyz.activitymanager;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    import java.io.Serializable;
    
    /**
     * Created by LGB on 2015/8/28.
     */
    public class Person implements Parcelable {
    
    
        private String name;
        private int height;
        private int age;
    
    
        public static final Creator<Person> CREATOR = new Creator<Person>() {
            @Override
            public Person createFromParcel(Parcel in) {
                Person person = new Person();
                person.name = in.readString();
                person.age = in.readInt();
                person.height = in.readInt();
                return person;
            }
    
            @Override
            public Person[] newArray(int size) {
                return new Person[size];
            }
        };
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getHeight() {
            return height;
        }
    
        public void setHeight(int height) {
            this.height = height;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeInt(age);
            dest.writeInt(height);
        }
    }

             运行结果

             注意Person.java中红色代码的部分

    Activity中的数据回传

             在Activity的跳转中,我们往往需要在关闭这个Activity之后向上一个Activity回传数据,但使用startActivity无法达到这个要求,Android提供了startActivityForResult方法来帮我们完成这一需求。

    AndroidManiFest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lixyz.activitymanager">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity"></activity>
        </application>
    </manifest>
    View Code

    MainActivity.java

    package cn.lixyz.activitymanager;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    
                    startActivityForResult(intent, 1);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
                case 1:
                    if (resultCode == RESULT_OK) {
                        String str = data.getStringExtra("string");
                        ((TextView) findViewById(R.id.tv_text_show2)).setText(str);
                    }
            }
        }
    }
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/ed_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您想要传递的值"/>
    
        <Button
            android:id="@+id/bt_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    
        <TextView
            android:id="@+id/tv_text_show2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#00ee00"
            android:textSize="30sp"/>
    
    </LinearLayout>
    View Code

    SecondActivity.java

    package cn.lixyz.activitymanager;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    /**
     * Created by LGB on 2015/8/27.
     */
    public class SecondActivity extends Activity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            findViewById(R.id.bt_cloBack).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("string", "hello world");
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
    
        }
    }
    View Code

    activity_second.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">
    
        <TextView
            android:id="@+id/tv_text_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认文字"
            android:textColor="#00ff00"
            android:textSize="90sp" />
    
        <Button
            android:id="@+id/bt_cloBack"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="关闭并返回"/>
    
    </LinearLayout>
    View Code

             运行结果:

             

             从代码中可以看出

             1. 我们使用startActivityForResult(intent, 1)来启动了SecondActivity,其中1是一个请求码,可以是任意数字,只要是唯一值即可

             2. 我们在SecondActivity中构建了一个用来传递主句的Intent,然后调用了setResult()方法,该方法专门用于向上一个活动返回数据,该方法的第一个参数是向上一个活动返回处理结果,第二个参数是返回的数据

             3. 由于我们是使用startActivityForResult来启动的第二个Activity,那么在第二个Activity销毁之后会调用上一个活动的onActivityResult()方法,所以我们重写该方法用来得到返回的数据

  • 相关阅读:
    数据库权限分配操作
    1130-host ... is not allowed to connect to this MySql server
    ubuntu 14.04安装mysql-python
    Ubuntu中的MySQL修改root密码的多种方法
    ubuntu下安装mysql及卸载mysql方法
    XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地
    深度学习-1
    html-新闻滚动条
    Django部署uwsgi 与 nginx配置
    二叉树层次遍历
  • 原文地址:https://www.cnblogs.com/xs104/p/4764122.html
Copyright © 2020-2023  润新知