Android_studio 开发简单登陆界面

展示:

 主要功能:页面跳转,登录,显示密码。

1.图标获取:https://www.iconfont.cn/    这个是阿里巴巴矢量图标库的链接

        关于添加图标可以参见

2.页面跳转:通过点击按钮实现

3.显示密码:利用CheckBox控制EditText(监听CheckBox是否被点击,从而决定EditText的是否显示)

4.登录条件:通过输入框获取用户名和密码,将其与所设用户名和密码比较,从而确定是否跳转界面,并根据情况给出提示,如:登陆成功

这个是第一个界面的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"
    tools:context=".TestActivity"
    android:orientation="vertical"
    android:background="@mipmap/img">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0sp"

        android:layout_weight="1"
        >
        <ImageView
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:background="@mipmap/img_1"
            android:id="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="35dp"
            >

        </ImageView>
        <TextView
            android:id="@+id/personIcon"
            android:layout_below="@id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/个人账号"
            android:textSize="45sp"
            android:textColor="#1296db"
            android:layout_marginTop="60dp"
            android:layout_marginBottom="25dp"
            android:layout_marginLeft="20dp"

            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:hint="请输入用户名:"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:layout_toRightOf="@id/personIcon"
            android:layout_alignBottom="@id/personIcon"
            android:maxLength="5"
            android:id="@+id/name"

            />

        <TextView
            android:id="@+id/codeIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/密码"
            android:textSize="45sp"
            android:textColor="#1296db"
            android:layout_marginBottom="25dp"
            android:layout_marginLeft="20dp"
            android:layout_below="@id/personIcon"
            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:hint="请输入密码:"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:layout_toRightOf="@id/codeIcon"
            android:layout_alignBottom="@id/codeIcon"
            android:inputType="numberPassword"
            android:maxLength="6"
            android:id="@+id/pass"
            />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/codeIcon"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/codeIcon"

            android:text="显示密码"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:id="@+id/checkbox">

        </CheckBox>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/checkbox"
            android:layout_marginLeft="130dp"
            android:layout_toRightOf="@id/checkbox"
            android:text="忘记密码?"
            android:textColor="#ffffff"
            android:textSize="30sp">

        </TextView>
        <Button
            android:id="@+id/buttonPanel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp"
            android:text="登录"
            android:textSize="30sp"
            >
        </Button>
    </RelativeLayout>

</LinearLayout>

 跳转后的界面XML源代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".Second"

    android:background="@mipmap/img"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="天官赐福"
            android:textColor="#ffffff"
            android:textSize="50sp"
            android:gravity="center_horizontal"
            >
        </TextView>
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="30sp"
            android:textColor="#1296db"
            android:lineSpacingExtra="30sp"
            android:text="@string/天官赐福"

            />

    </LinearLayout>


</ScrollView>

 启动界面Activity:

package com.example.myapplication77;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {

        EditText name;
        EditText pass;
        private  CheckBox mCheckBox;

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

        name= (EditText) findViewById(R.id.name);
        pass=(EditText) findViewById(R.id.pass);

        mCheckBox=(CheckBox)  findViewById(R.id.checkbox);
    mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if(isChecked){
                pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示
            }else{
                pass.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏
            }
        }
    });

    TextView personIcon=findViewById(R.id.personIcon);
        Typeface font=Typeface.createFromAsset(getAssets(),"iconfont.ttf");
        personIcon.setTypeface(font);

        TextView codeIcon=findViewById(R.id.codeIcon);
        Typeface font1=Typeface.createFromAsset(getAssets(),"iconfont_1.ttf");
        codeIcon.setTypeface(font1);

        Button buttonPanel = findViewById(R.id.buttonPanel);
        buttonPanel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                String mname="admin";
                String mpass="123456";
                String user=name.getText().toString().trim();
                String pwd=pass.getText().toString().trim();

                if(user.equals(mname)&&pwd.equals(mpass)){
                    Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    //新建一个Intent(当前Activity, SecondActivity)=====显示Intent
                    Intent intent = new Intent(MainActivity.this, Second.class);
                    startActivity(intent);
                    return;
                }else if(TextUtils.isEmpty(user)){
                    Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(pwd)){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(MainActivity.this,"此用户名不存在或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

  • 4
    点赞
  • 137
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值