1.在已经创建的java文件MainActivity(点击app,点击java)下里面编写
package com.example.myapplication1120;import android.content.Intent;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//调用xml中activity_main。Button ok = (Button) this.findViewById(R.id.btn);ok.setOnClickListener(new ButtonListener());}private class ButtonListener implements View.OnClickListener{@Overridepublic void onClick(View v){Intent intent=new Intent(MainActivity.this,TwoActivity.class);//设置切换对应activitystartActivity(intent);//开始切换}}}
2.在activity_main.xml(打开app,打开res,在layout下)里面编写
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:layout_marginBottom="60dp"android:text="第二页"android:layout_gravity="center_horizontal"/></LinearLayout>
3.新建一个layout Resource File(打开app,打开res,在layout下新建一个layout Resource File)命名为twolayout,在里面编写
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:layout_marginBottom="60dp"android:text="第二页"android:layout_gravity="center_horizontal"/></LinearLayout>
4.新建一个java文件(在app下,在java下,在com.example…………命名为TwoActivity,在里面编写
package com.example.myapplication1120;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;public class TwoActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.twolayout);//调用twolayout}}
5.在AndroidManifest中添加
<activity android:name=".TwoActivity"></activity>
添加的位置在<application与</application>之间
6.点击运行
7.实现呐
备注:学习记录