Intent方法进行页面跳转适用于两个Activity之间的跳转,按返回键可以直接返回到前一个页面。
使用时需要注意在Manifest.xml文件中注册Activity
activity_main.xml(Mainactivity布局)
- <?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_first"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第一个页面"
- android:layout_gravity="center"
- android:textSize="40dp"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="onclick"
- android:text="进入第二个页面"
- />
- </LinearLayout>
-
创建按钮的onclick方法
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- public void onclick(View view) {
- Intent intent = new Intent(this,SecondActivity.class);
- startActivity(intent);
- }
- }
-
Intent方法两个参数,第一个是上下文,第二个是跳转的Activity
新建Activity方法:
.new——activity——empty activity
随后会产生一个相应的布局文件
- <?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:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第二个页面"
- android:layout_gravity="center"
- android:textSize="40dp"
- />
-
-
- </LinearLayout>
-
如图是运行效果。点击按钮 跳转到第二个页面
Andriod项目默认生成一个MainActivity.java,如果执行,会默认执行它,但如果在别的Activity中写了登录界面,如何先执行登录呢?
假设是这种情况,打开/项目名/AndroidManifest.xml这个xml文件中
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".SecondActivity"></activity>
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
就理解为过滤器,它指定了启动应用程序的Intent对象的动作和类型
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".SecondActivity">
-
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
-
- </activity>
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
-
这样第一个启动的就是SecondActivity了
首个启动的设置为
- <category android:name="android.intent.category.LAUNCHER" />
-
其他的设置为
- <category android:name="android.intent.category.DEFAULT" />
-