Android Studio入门——页面跳转

莱莱  金牌会员 | 2024-6-13 21:42:29 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 574|帖子 574|积分 1722

1.工程目次 


2.MainActivity

  1. package com.example.demo01;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.TextView;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. public class MainActivity extends AppCompatActivity {
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.         //1.修改文本
  13.         TextView tv = findViewById(R.id.tv);
  14.         tv.setText("你好! "); //再次设置activity_main.xml  中  android:text="页面1"
  15.         //2.实现跳转
  16.         View button = findViewById(R.id.button);
  17.         button.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View v) {
  20.                 Intent intent = new Intent();
  21.                 intent.setClass(MainActivity.this,MainActivity2.class);
  22.                 startActivity(intent);
  23.             }
  24.         });
  25.     }
  26. }
复制代码
 3.MainActivity2

MainActivity2为右键layout——new——Activity——Empty Views Activity天生,也可手动创建。
  1. package com.example.demo01;
  2. import android.os.Bundle;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. public class MainActivity2 extends AppCompatActivity {
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.activity_main2);
  9.     }
  10. }
复制代码
 4.activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:gravity="center"
  6.     android:orientation="vertical">
  7.     <TextView
  8.         android:id="@+id/tv"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:text="页面1" />
  12.     <Button
  13.         android:id="@+id/button"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:text="跳转"/>
  17. </LinearLayout>
复制代码
 5.activity_main2.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:gravity="center"
  6.     android:orientation="vertical">
  7.     <TextView
  8.         android:id="@+id/tv2"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:text="页面2" />
  12. </LinearLayout>
复制代码
 6.启动项目


7.增补

text可以这样写,将文本写到strings.xml中,在activity_main2.xml中使用@string/文本的name  举行引用。




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莱莱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表