安卓开发学习——day5

今天做的是用户登录

1.新建项目

2.新建activity模板

3.编写布局:

【login_activity.xml】

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:background="@drawable/bg"
    android:orientation="vertical"
    tools:context=".LoginActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/login_title"
        android:textSize="30sp"
        android:textColor="@color/purple_700"
        android:layout_marginBottom="30dp"
        />

    <LinearLayout
        style="@style/login_input_layout"
        >
        <TextView
            style="@style/login_text"
            android:text="@string/username"
            />
        <EditText
            style="@style/login_input"
            android:id="@+id/edt_user_name"
            android:background="@drawable/border"
            android:hint="@string/edt_username"
            />
    </LinearLayout>
    <LinearLayout
        style="@style/login_input_layout"
        >
        <TextView
            style="@style/login_text"
            android:text="@string/password"
            />
        <EditText
            style="@style/login_input"
            android:id="@+id/edt_password"
            android:background="@drawable/border"
            android:inputType="textPassword"
            android:hint="@string/edt_password"
            />
    </LinearLayout>

    <LinearLayout
        style="@style/login_input_layout">
        <Button
            style="@style/login_button"
            android:id="@+id/btn_login"
            android:text="@string/login"/>
        <Button
            style="@style/login_button"
            android:id="@+id/btn_cancel"
            android:text="@string/cancel"/>
    </LinearLayout>

</LinearLayout>

【string】

<resources>
    <string name="app_name">Task2</string>
    <string name="login_title">用户登录</string>
    <string name="username">用户:</string>
    <string name="password">密码:</string>
    <string name="edt_username">输入用户名</string>
    <string name="edt_password">输入密码</string>
    <string name="login">登录</string>
    <string name="cancel">取消</string>
</resources>

【values/style】

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="login_input_layout">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:gravity">center</item>
        <item name="android:paddingBottom">20dp</item>
    </style>
    
    <style name="login_text">
        <item name="android:layout_width">70dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:gravity">center|left</item>
        <item name="android:textSize">20sp</item>
    </style>
    <style name="login_input">
        <item name="android:layout_width">150dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="background">@drawable/border</item>
        <item name="android:padding">6dp</item>
        <item name="android:lines">1</item>
    </style>

    <style name="login_button">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_margin">6dp</item>
        <item name="android:textColor">@color/black</item>
    </style>
</resources>

【drawable/border】

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="1dp"
        android:color="@color/teal_400"/>
    <solid android:color="@color/white"/>
</shape>

4.编写java代码:

【LoginActivity.java】

package com.example.task2;

import androidx.appcompat.app.AppCompatActivity;

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 edtUserName;
    private EditText edtPassword;
    private Button btnLogin;
    private Button btnCancel;

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

        edtUserName = findViewById(R.id.edt_user_name);
        edtPassword = findViewById(R.id.edt_password);
        btnLogin = findViewById(R.id.btn_login);
        btnCancel = findViewById(R.id.btn_cancel);

        btnLogin.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                String strUsername = edtUserName.getText().toString().trim();
                String strPassword = edtPassword.getText().toString().trim();

                if (strUsername.equals("admin") && strPassword.equals("admin")) {
    
    
                    Toast.makeText(LoginActivity.this,"恭喜,用户名和密码正确!"
                        ,Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);

                    Bundle bundle = new Bundle();
                    bundle.putString("username", strUsername);
                    bundle.putString("password", strPassword);

                    intent.putExtras(bundle);

                    startActivity(intent);
                } else {
    
    
                    Toast.makeText(LoginActivity.this, "遗憾,用户名或密码错误!请重新输入!"
                    ,Toast.LENGTH_LONG).show();
                    edtUserName.setText("");
                    edtPassword.setText("");
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                edtUserName.setText("");
                edtPassword.setText("");
            }
        });
    }
}

【mainActivity.java】

package com.example.task2;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    
    

    private TextView tvMain;

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

        tvMain = findViewById(R.id.tv_main);

        // 获取意图
        Bundle data = getIntent().getExtras();
        String strUserName = data.getString("username");
        String strPassword = data.getString("password");

        tvMain.setText("Hello," + strUserName);
        tvMain.setTextSize(24);
    }
}

效果展示:

登录失败
登录失败
登录成功
登录成功


总结: 登录界面之前也做过一次,难度不大,就这是第一次使用意图来传递数据,查看了资料才完成。

猜你喜欢

转载自blog.csdn.net/weixin_45936162/article/details/112636302