家庭记账本app进度之对于登录和注册两个界面点击按钮的相互跳转

这次主要完成了两个两个android页面之间的跳转。从登录页面点击注册就会跳转到注册页面。在注册页面点击返回登录,这样就可以返回到登录界面。主要是这样的操作。其中遇到了一个困难主要是当点击按钮的时候,它并不会进行页面的相关跳转,而是显示软件停止工作。最终经过查找相应的网上资料发现。其问题出在自己没有在manifest.xml添加对应新建.java文件的activity。导致错误的出现。下面是主要的代码以及运行的结果图。

对于页面跳转的主要的应用的代码是如下:

Intent intent = new Intent(RegisterActivity1.this, MainActivity.class);
startActivity(intent);

依次来确定软件点击时候的页面的跳转。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_gravity="center_horizontal"

    tools:context=".MainActivity">
    <TextView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:padding="10dp"
        android:text="进入界面"
        android:textColor="#9832"
        android:textSize="30dp" />
    <LinearLayout
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv"
        android:layout_centerVertical="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="15dp"
        android:background="#ffffff">
        <TextView
            android:id="@+id/tv_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="账号"
            android:textColor="#000"
            android:textSize="20dp" />
        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/number"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码"
            android:textSize="20dp"
            android:textColor="#000" />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/tv_password"
            android:background="@null"
            android:inputType="textPassword"
            android:padding="10dp" />
    </LinearLayout>
    <Button
        android:id="@+id/button_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="60dp"
        android:background="#3c8dc4"
        android:text="登录"
        android:textColor="#ffffff"
        android:textSize="20dp" />
    <Button
        android:id="@+id/button_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_login"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="30dp"
        android:background="#7983"
        android:text="注册"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <CheckBox
        android:checked="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/checkBox"
        android:layout_below="@+id/password"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"/>
</RelativeLayout>

MainActivity.java

package com.example.myapplicationhome;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn_register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_register =(Button) findViewById(R.id.button_register);
        btn_register.setOnClickListener(this);
    }
        @Override
        public void onClick(View view){
            //String text = "csdnicjbsndicnsdicnsojdn";
            //Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this,RegisterActivity1.class);
            startActivity(intent);
            }
        }

register_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    tools:context=".RegisterActivity1">
    <TextView
        android:layout_marginTop="60dp"
        android:id="@+id/reg_number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="账号:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@+id/reg_number1"
        android:layout_toRightOf="@+id/reg_number1"
        android:id="@+id/reg_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
    <TextView
        android:id="@+id/reg_number2"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reg_number1"
        android:padding="10dp"
        android:text="密码:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@id/reg_number2"
        android:layout_toRightOf="@+id/reg_number2"
        android:id="@+id/reg_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
    <TextView
        android:id="@+id/reg_number3"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reg_number2"
        android:padding="10dp"
        android:text="密码:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@id/reg_number3"
        android:layout_toRightOf="@+id/reg_number3"
        android:id="@+id/reg_password2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />
    <TextView
        android:id="@+id/reg_number4"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reg_number3"
        android:padding="10dp"
        android:text="邮箱:"
        android:textColor="#000"
        android:textSize="20dp" />
    <EditText
        android:layout_alignBottom="@id/reg_number4"
        android:layout_toRightOf="@+id/reg_number4"
        android:id="@+id/reg_mail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定注册"
        android:background="#74e674"
        android:id="@+id/reg_btn_sure"
        android:layout_marginTop="38dp"
        android:layout_below="@+id/reg_mail"
        android:layout_marginLeft="90dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回登录"
        android:background="#f27758"
        android:id="@+id/reg_btn_login"
        android:layout_alignBottom="@id/reg_btn_sure"
        android:layout_toRightOf="@id/reg_btn_sure"
        android:layout_marginLeft="40dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="账号注册"
        android:textSize="30dp"
        android:layout_marginTop="5dp"
        />
</RelativeLayout>

RegisterActivity1.java

package com.example.myapplicationhome;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
    public class RegisterActivity1 extends AppCompatActivity implements View.OnClickListener {
        private Button reg_btn_login;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.register_activity);
            reg_btn_login = (Button) findViewById(R.id.reg_btn_login);
            reg_btn_login.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(RegisterActivity1.this, MainActivity.class);
            startActivity(intent);
        }
    }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplicationhome">

    <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:name=".RegisterActivity1">
        </activity>
    </application>

</manifest>

对应的运行结果如下:

刚开始的时候:

 之后当点击注册按钮的时候:

 当点击返回登录的时候就会返回到刚刚的登录界面。

原文地址:https://www.cnblogs.com/dazhi151/p/12248455.html