当前位置:   article > 正文

Android studio 按钮点击页面跳转_android studio页面跳转

android studio页面跳转

(1)先创建一个要跳转的页面,即一个新的页面,该页面是点击之后跳转的。

步骤:app--->src-->main-->res-->layout(右击)-->New-->Activity-->Empty Activity

 创建好以后,此时会生成一个同名的java文件。

初始时的界面代码如下,界面展示在后面,仅供参考。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:gravity="center_horizontal"
  9. tools:context=".MainActivity">
  10. <LinearLayout
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:orientation="horizontal">
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="用户名:"/>
  18. <EditText
  19. android:id="@+id/edit1"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:hint="____________"/>
  23. </LinearLayout>
  24. <LinearLayout
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:orientation="horizontal">
  28. <TextView
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="密码:"/>
  32. <EditText
  33. android:id="@+id/edit2"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:hint="____________"
  37. android:password="true"/>
  38. </LinearLayout>
  39. <Button
  40. android:id="@+id/btn"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="登录"/>
  44. </LinearLayout>

跳转的界面代码,同样界面展示在后面。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:background="@drawable/bg">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="40dp"
  11. android:textColor="#DDE0E4"
  12. android:text="欢迎进入智慧农业系统"
  13. />
  14. </LinearLayout>

由于页面跳转没有变换,变化的是主界面,在主界面点击按钮,使页面发生变化,因此只需要编写

MainActivity.java文件。

注:不同的文件名,包名不同,以下为我个人的编写代码,仅供参考。

  1. package com.example.signin;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9. public class MainActivity extends AppCompatActivity {
  10. private Button btn;
  11. private EditText edit1;
  12. private EditText edit2;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. initView();
  18. }
  19. private void initView() {
  20. btn = (Button) findViewById(R.id.btn);
  21. edit1 = (EditText) findViewById(R.id.edit1);
  22. edit2 = (EditText) findViewById(R.id.edit2);
  23. btn.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. if(edit1.getText().toString().equals("aaa") &&
  27. edit2.getText().toString().equals("aaa")){
  28. Intent intent = new Intent(MainActivity.this,nextActive.class);
  29. startActivity(intent);
  30. }
  31. else {
  32. Toast.makeText(MainActivity.this,"输入有误,请重新输
  33. 入",Toast.LENGTH_LONG).show();
  34. }
  35. }
  36. });
  37. }
  38. }

以下是我完成的登录跳转页面,仅供参考。

初始时的界面:

输入正确的用户名和密码以后,跳转到如下界面,仅供参考。

 如果没有输入正确,将有文字提示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/267060
推荐阅读
相关标签
  

闽ICP备14008679号