activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="360dp"
android:layout_height="240dp"
android:layout_centerVertical="true"
android:src="@drawable/img1"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mButton"
android:text="@string/b1"
android:textSize="24sp"
android:gravity="center"/>
</LinearLayout>
MainActivity.java
package com.example.yanhsama.ex3_3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.mButton);
btn.setOnClickListener(new btnclock());
}
class btnclock implements OnClickListener{
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, secondActivity.class);
startActivity(intent);
}
}
}
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"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t2"
android:textSize="40sp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2"
android:textSize="30sp"/>
</LinearLayout>
secondActivity.java
package com.example.yanhsama.ex3_3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class secondActivity extends AppCompatActivity {
Button btn2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
btn2=(Button)findViewById(R.id.button2);
btn2.setOnClickListener(new btnclock());
}
class btnclock implements View.OnClickListener{
public void onClick(View v){
Intent intent2=new Intent(secondActivity.this,MainActivity.class);
startActivity(intent2);
}
}
}
strings.xml
<resources>
<string name="app_name">ex3_3</string>
<string name="t1">切换页面</string>
<string name="t2">欢迎进入本系统</string>
<string name="b1">切换到另一页面</string>
<string name="btn2">切换到原来的页面</string>
</resources>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yanhsama.ex3_3">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".secondActivity"/>
</application>
</manifest>
效果截图