Android实现登录界面和跳转界面

MainActivity:
package com.example.myapp;

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 {
//
//
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//
//        Button button; //声明组件
//        button=findViewById(R.id.button_1); //找到组件
//        button.setOnClickListener(new View.OnClickListener() { //为组件设置点击事件
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this,"hello",Toast.LENGTH_SHORT).show();
//            }
//        });
//    }
//}


public class MainActivity extends AppCompatActivity {
    private Button Login_btn;
    private Button Setting_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Setting_btn = (Button) findViewById(R.id.setting_btn);
        Login_btn = (Button) findViewById(R.id.login_btn);
        Login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                Intent intent=new Intent();//Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。
                intent.setClass(MainActivity.this, MainActivity.class);
                startActivity(intent);}
        });

        Setting_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.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"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <EditText

        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:hint="用户名"
        android:inputType="textLongMessage"
        android:textColor="#000000"
        android:textSize="16sp"
        android:layout_marginTop="50dp"/>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:hint="密码"
        android:inputType="numberPassword"
        android:textColor="#000000"
        android:textSize="16sp"
        android:layout_below="@id/username"/>

    <Button
        android:id="@+id/login_btn"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_alignParentStart="true"
        android:layout_marginStart="6dp"
        android:layout_marginTop="50dp"
        android:text="登录"
        android:textSize="16sp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="6dp" />

    <Button
        android:id="@+id/setting_btn"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"

        android:layout_alignParentEnd="true"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="50dp"
        android:text="设置"
        android:textSize="16sp"
        android:layout_alignParentRight="true" />

</RelativeLayout>

 

  • 6
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Android Studio登陆界面跳转可以通过以下步骤实现: 1. 在登陆界面布局文件中添加一个按钮,用于跳转到下一个界面。 2. 在Activity中获取该按钮,并为其设置点击事件。 3. 在点击事件中使用Intent跳转到下一个界面。 示例代码如下: 1. 在登陆界面布局文件中添加一个按钮: ``` <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆" android:layout_marginTop="20dp" android:layout_gravity="center_horizontal"/> ``` 2. 在Activity中获取该按钮,并为其设置点击事件: ``` public class LoginActivity extends AppCompatActivity { private Button mBtnLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mBtnLogin = findViewById(R.id.btn_login); mBtnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 点击事件中跳转到下一个界面 Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); } }); } } ``` 3. 在点击事件中使用Intent跳转到下一个界面。这里跳转到MainActivity,可以根据实际情况修改。 需要注意的是,跳转到下一个界面前需要先进行登陆验证等操作,否则可能会出现安全问题。 希望能对你有所帮助! ### 回答2: 在Android开发中,登陆界面跳转是一个非常常见的需求。一般来说,登陆界面跳转实现步骤如下: 1.设计登陆界面 首先,我们需要设计一个简洁、美观的登陆界面。登陆界面中要包含两个输入框,分别用于输入用户名和密码,还需要一个按钮,用于触发登陆事件,并将用户的输入信息提交到服务器进行验证。 2.编写登陆代码 在界面设计好之后,我们就需要编写登陆代码了。首先,我们需要获取用户输入的用户名和密码,然后将其封装成请求参数,发送到服务器进行验证。如果验证成功,就将用户信息保存到本地,并跳转到目标界面。如果验证失败,则需要给用户提示错误信息。 3.实现页面跳转Android Studio中,页面跳转通常使用Intent类来实现。我们可以在登陆成功后,创建一个Intent对象,并使用startActivity()方法来启动目标界面实现页面跳转。 总之,登陆界面跳转Android应用程序开发中必不可少的一部分。在实现登陆界面跳转时,我们需要注意UI设计、请求验证、页面跳转等方面的细节,以确保实现一个优秀的、完善的应用程序。 ### 回答3: Android Studio 是一款非常强大的开发工具,开发者可以利用它创建众多类型的 Android 应用程序。在大多数应用程序中,登陆界面是必不可少的功能,因此本文将讨论如何在 Android Studio 中实现登陆界面跳转实现登陆界面跳转包含两个步骤: 1. 创建登陆界面 2. 实现跳转 1. 创建登陆界面 创建登陆界面的方法有很多,但最常见的方法是使用 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_horizontal"> <EditText android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:padding="16dp"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:padding="16dp" android:inputType="textPassword"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Log in"/> </LinearLayout> 上述示例包含两个 EditText 组件用于输入邮箱和密码,以及一个 Button 组件用于登陆。使用这个布局文件创建完整的登陆界面非常简单,只需在相应的 Java 类中引用即可。 2. 实现跳转实现登陆界面跳转,我们需要新建一个 Activity 并将其与上述布局文件关联。接下来,我们将编写代码来处理登陆逻辑并实现界面之间的跳转。 首先,在 XML 布局文件中,给登陆按钮添加一个 OnClickListener 事件,当用户单击按钮时,触发登陆事件。 <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Log in" android:onClick="loginClicked"/> 接着,在 Java 类中添加一个名为 loginClicked 的方法,处理登陆逻辑和界面跳转。 public void loginClicked(View view) { // 获取用户输入的邮箱和密码 String email = ((EditText) findViewById(R.id.et_email)).getText().toString(); String password = ((EditText) findViewById(R.id.et_password)).getText().toString(); // 验证输入的邮箱和密码是否正确 if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) { Toast.makeText(this, "Please enter email and password", Toast.LENGTH_SHORT).show(); return; } else if (!email.equals("user@example.com") || !password.equals("password")) { Toast.makeText(this, "Email or password is incorrect", Toast.LENGTH_SHORT).show(); return; } // 如果登陆成功,跳转到主页 Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } 在上述代码中,我们使用 TextUtils.isEmpty() 方法检查用户是否输入了邮箱和密码,然后检查输入的邮箱和密码是否正确。如果输入正确,我们使用 Intent 类创建一个新的 Intent,指定要跳转到的 Activity,然后使用 startActivity() 方法启动新的 Activity。最后,我们调用 finish() 方法关闭当前 Activity。 总结 本文介绍了在 Android Studio 中实现登陆界面跳转的步骤。要实现跳转,我们需要创建一个登陆界面和一个用于验证登陆的 Java 类,并在 Java 类中处理跳转逻辑。希望这篇文章对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值