先来一个HelloWorld.
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:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
</LinearLayout>
事件响应代码为将英文修改成中文。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置屏幕组件需要用的布局
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.tv);
// 修改文本控件的文字
tv.setText("你好,世界");
}
}
最后在手机上显示的画面如下
创建第二个页面
在res/layout文件夹下面新建一个xml文件
在res/values的strings.xml文件中添加内容
<resources>
<string name="app_name">OCR</string>
<string name="text2">Activity Main2</string>
</resources>
activity_main2.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:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text2" />
</LinearLayout>
在清单文件AndroidManifest.xml文件中添加activity_main2的配置。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.guanjian.ocr">
<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.OCR">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2" />
</application>
</manifest>
在Activity1中添加一个按钮来跳转到Activity2。
<?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:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转" />
</LinearLayout>
修改MainActivity的Java代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置屏幕组件需要用的布局
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.tv);
// 修改文本控件的文字
tv.setText("你好,世界");
Button button = findViewById(R.id.button);
// 给button设定点击事件的侦听
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 创建一个意图对象
Intent intent = new Intent();
intent.setClass(MainActivity.this,MainActivity2.class);
// 实现跳转
startActivity(intent);
}
});
}
}
在Java主目录中创建MainActivity2的响应类。
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
在手机上的运行效果如下
Activity