Android显式启动
打开应用主页面后,点击按钮跳转到新页面
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapplication"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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/Theme.MApplication"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
//将第二个页面添加到配置文件 <activity android:name=".BankActivity">
</activity>
</application>
</manifest>
|
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
//主页面设置按钮 <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
line_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BankActivity">
//新页面 <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ADFF2F" android:tooltipText="绿色背景,模拟pc鼠标悬停出现描述信息" android:layout_weight="1"/>
<Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="12345"/>
<View android:layout_width="match_parent" android:layout_height="3dp" android:background="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="bcd"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"/>
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:background="#DA70D6" android:layout_weight="1"/>
</LinearLayout>
|
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package com.example.mapplication;
import androidx.appcompat.app.AppCompatActivity; import androidx.core.view.KeyEventDispatcher;
import android.app.AlertDialog; import android.content.ComponentName; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.FrameLayout; import android.widget.GridLayout; import android.widget.ImageView; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button)findViewById(R.id.btn1); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
Intent intent = new Intent(); ComponentName componentName = new ComponentName(MainActivity.this,BankActivity.class); intent.setComponent(componentName); startActivity(intent); } });
}
}
|
BankActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.example.mapplication;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.PersistableBundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.LinearLayout;
import androidx.annotation.Nullable;
public class BankActivity extends Activity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linear_layout);
}
}
|
隐式启动
需要在xml文件中配置action属性,同时将category属性配置为default
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapplication"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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/Theme.MApplication"> <activity android:name=".MainActivity"> <intent-filter> //主页面 <action android:name="android.intent.action.MAIN" /> //启动器 <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<activity android:name=".BankActivity"> <intent-filter> //需要在xml文件中配置action属性,同时将category属性配置为default <action android:name="abcd.BankActivity"></action> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
</application>
</manifest>
|
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.example.mapplication;
import androidx.appcompat.app.AppCompatActivity; import androidx.core.view.KeyEventDispatcher;
import android.app.AlertDialog; import android.content.ComponentName; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.FrameLayout; import android.widget.GridLayout; import android.widget.ImageView; import android.widget.TextView;
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button)findViewById(R.id.btn1); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
Intent intent = new Intent(); intent.setAction("abcd.BankActivity"); startActivity(intent);
} });
}
}
|
关闭新打开的页面,回到旧的主页面
在第二个页面布局文件中使用按钮,点击时关闭新页面。
line_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BankActivity">
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ADFF2F" android:tooltipText="绿色背景,模拟pc鼠标悬停出现描述信息" android:layout_weight="1"/>
<Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="回退"/>
<View android:layout_width="match_parent" android:layout_height="3dp" android:background="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="bcd"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"/>
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:background="#DA70D6" android:layout_weight="1"/> </LinearLayout>
|
BankActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.example.mapplication;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.PersistableBundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.LinearLayout;
import androidx.annotation.Nullable;
public class BankActivity extends Activity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linear_layout); Button button = (Button) findViewById(R.id.btn2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { BankActivity.this.finish(); } });
}
}
|
如果有两个页面都符合隐式启动的条件,会弹出选择框选择。
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapplication"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="主页面" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MApplication"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<activity android:name=".BankActivity" android:label="第二个页面"> <intent-filter> <action android:name="abcd.BankActivity"></action> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
<activity android:name=".ThindActivity" android:label="第三个页面"> <intent-filter> <action android:name="abcd.BankActivity"></action> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
</application>
</manifest>
|
ThindActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.example.mapplication;
import android.app.Activity; import android.os.Bundle; import android.os.PersistableBundle; import android.view.View; import android.widget.Button;
import androidx.annotation.Nullable;
public class ThindActivity extends Activity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.thind_layout); Button button = (Button) findViewById(R.id.bitn3); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ThindActivity.this.finish(); } }); } }
|
thind_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="33333333"/> <Button android:id="@+id/bitn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="回退1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
|