当前位置: 首页 > article >正文

Android Studio入门——页面跳转

1.工程目录 

2.MainActivity

package com.example.demo01;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.修改文本
        TextView tv = findViewById(R.id.tv);
        tv.setText("你好! "); //再次设置activity_main.xml  中  android:text="页面1"
        //2.实现跳转
        View button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });


    }
}

 3.MainActivity2

MainActivity2为右键layout——new——Activity——Empty Views Activity生成,也可手动创建。

package com.example.demo01;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

 4.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:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="页面1" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"/>

</LinearLayout>

 5.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:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="页面2" />

</LinearLayout>

 6.启动项目

7.补充

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


http://www.kler.cn/a/271329.html

相关文章:

  • 还是了解下吧,大语言模型调研汇总
  • 迷茫了!去大厂还是创业?
  • 小结:Node.js Express VS Koa
  • 我公司网站建设奉行“每个细节都一丝不苟”得标杆、全方位践诺
  • 【Java基础】IO流(三):字符流的FileReader(文件字符输入流)和 FileWriter(文件字节输出流)
  • FastJSON错误Could not read JSON: Unrecognized field
  • 如何重置iPhone的网络设置?这里提供详细步骤
  • 【Leetcode】top 100 矩阵
  • NCV4275CDT50RKG稳压器芯片中文资料规格书PDF数据手册引脚图图片价格功能
  • SpingBoot集成Rabbitmq及Docker部署
  • xss-labs level 10-20
  • Lua-Lua与C++的交互2
  • Redis远程连接本机——Docker
  • PDF Expert:强大注释与批注功能,让PDF阅读更高效
  • springboot整合swagger,postman,接口规范
  • java serlvet 高校学生画像平台系统Myeclipse开发mysql数据库web结构java编程计算机网页项目echarts图形展现
  • 算法训练营day50(补),单调栈2
  • MySQL安装(Mac系统)
  • 智慧城市与数字孪生:共创未来城市的智慧生活
  • HCIP—OSPF课后练习一