android兩個頁面跳轉(zhuǎn)
我們使用 Intent()方法用于兩個Activity之間的跳轉(zhuǎn),按手機的返回鍵可以直接返回到前一個頁面。

1、修改MainActivity
代碼:
public class MainActivity extends AppCompatActivity {
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ?}
// ? ?創(chuàng)建按鈕的onclick方法
? ?public void onclick(View view) {
? ? ? ?Intent intent = new Intent(this,SecondActivity.class);
? ? ? ?startActivity(intent);
? ?}
}

2、修改Layout下的activity_main.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_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>

3、創(chuàng)建SecondActivity,選擇empty activity




這樣直接創(chuàng)建activity會自動在 AndroidManifest.xml 中對activity進行注冊,并且會自動創(chuàng)建對應(yīng)的 .xml 布局文件。
4、修改SecondActivity.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:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="第二個頁面"
? ? ? ? android:layout_gravity="center"
? ? ? ? android:textSize="40dp"
? ? ? ? />
</LinearLayout>

然后直接在android虛擬機中運行即可。

