Java开发Android入门学习——AndroidStudio在Activity实现简单登录功能,跳转页面显示登录用户名

Java开发Android入门学习——AndroidStudio在Activity实现简单登录功能,跳转页面显示登录用户名

  1. 安装AS:
    AndroidStudio安装,在安装好AS之后,配置好相对应的SDK,AVD,成功后进行下一步。

  2. 创建项目:
    这次我们选择空项目来进行创建:Empty–>Next
    在这里插入图片描述
    选择项目名称、存储位置(建议建立单独的文件夹来存放AS项目文件,以便以后查找)、SDK版本(注意:SDK版本需要跟AVD一致)
    在这里插入图片描述
    创建Activity空项目:
    在这里插入图片描述
    在这里插入图片描述
    依次创建LoginActivity.java用于登录界面,创建SuccessActivity.java用于登录后跳转显示用户信息。本次不实现其他功能。

  3. 文件源代码:
    LoginActivity.java

package com.example.homework3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button  login;
    private Button cancel;

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

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.btn_login);
        cancel = (Button) findViewById(R.id.btn_cancel);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username1 = username.getText().toString();
                String password1 = password.getText().toString();
                /*if(username1.equals("admin")&& password1.equals("admin")){
                    Toast.makeText(LoginActivity.this,"恭喜登录成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this,SuccessActivity.class);
                    intent.putExtra("username",username1);
                    startActivity(intent);*/
                if(username1.equals("admin")&& password1.equals("admin")){
                    Toast.makeText(LoginActivity.this,"恭喜登录成功",Toast.LENGTH_SHORT).show();
                    SuccessActivity.actionStart(LoginActivity.this,username1);
                }else{
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.homework3.LoginActivity">

    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textColor="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/username"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:hint="请输入用户名" />
    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

        <TextView
            android:id="@+id/user_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密   码:"
            android:textColor="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:inputType="textPassword"
            android:hint="请输入密码"
            />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_login"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            />

        <Button
            android:id="@+id/btn_cancel"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消"
            />
    </LinearLayout>
</LinearLayout>

Success.java

package com.example.homework3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SuccessActivity extends AppCompatActivity {


    public static void actionStart(Context context,String username1){
        Intent intent = new Intent(context, SuccessActivity.class);
        intent.putExtra("username",username1);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_success);

        TextView textView=(TextView) findViewById(R.id.textView);

        Intent intent = getIntent();
        String username = intent.getStringExtra("username");
        textView.setText("欢迎你:"+username);
    }
}

activity_success.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

AndroidManifest.xml中设置LoginActivity为项目首页:

        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  1. 运行结果
    在这里插入图片描述
    在这里插入图片描述
  • 22
    点赞
  • 154
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值