登录界面及跳转

登陆界面的布局代码:

xml的代码

目录

登陆界面的布局代码:

xml的代码

MyActivity的代码:

密码的隐藏及显示

网页的跳转


<?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"
    android:background="@mipmap/bj5"
    android:layout_gravity="center_horizontal"
    >

    <ImageView
        android:layout_width="100sp"
        android:layout_height="100sp"
        android:layout_gravity="center"
        android:src="@mipmap/touxiang"
        android:layout_marginTop="100dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/denglu"/>

        <EditText
            android:id="@+id/zh"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:hint="请输入账户名        "
            android:onClick="zhjianting"
            android:textSize="30sp"/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginTop="60dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/mima"
            android:layout_gravity="center"/>

        <EditText
            android:id="@+id/mm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="30sp"
            android:hint="请输入密码"
            android:onClick="mmjianting"
            android:inputType="numberPassword" />
        
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <CheckBox
            android:id="@+id/xs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="显示密码"
            android:textSize="20sp" />

        <Button
            android:id="@+id/forget_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/xs"
            android:layout_toRightOf="@id/xs"
            android:text="忘记密码"
            android:layout_marginLeft="100dp"
            android:textSize="20sp" />

    </RelativeLayout>


    <Button
        android:id="@+id/bin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        android:layout_gravity="center"
        />

</LinearLayout>

CheckBox是复选框。

MyActivity的代码:

package com.example.myapplication;
/*
Toast是为了有好的提醒用户,而又不影响用户的操作的。
方法中的MainActivity.this表示在MainActivity里显示;
R.string.alert表示引用string下的alert中的资源;
LENGTH_SHORT表示Toast的显示时间一微秒计算,这里调用系统定义好扥时间,也可自己写入确定的时间。
关于.show(); 是用来将定义好的Toast显示在MainActivity里的,
如果不调用.show();方法定义的Toast就没有意义了。
 */
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Intent intent;
    String account="admin";
    String password="123456";
    String mima;
    String zhanghu;
    EditText m;
    EditText z;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jiemian_layout);
        //显示密码-----------------------------------------------------------------------------------
        CheckBox xianshi=findViewById(R.id.xs);
        xianshi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    //如果选中,显示密码
                    m.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    m.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        //-----------------------------------------------------------------------------------------
        //账户
        z=findViewById(R.id.zh);
        z.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable editable) {
                zhanghu=z.getText().toString();
            }
        });
        //-----------------------------------------------------------------------------------------
        //密码
        m=findViewById(R.id.mm);
        m.addTextChangedListener(new TextWatcher() {
            @Override
            //输入框改变前
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            //输入框改变时
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            //输入框改变后
            public void afterTextChanged(Editable editable) {
                //获取输入框改变后的信息
                mima=m.getText().toString();
            }
        });
        //-----------------------------------------------------------------------------------------
        //获取按钮
        Button bin=findViewById(R.id.bin);

        bin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if(zhanghu.equals(account)&&mima.equals(password)){
                    Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    //页面跳转(当前页面.this,要跳转的页面.class)-------------------------------------------
                    Intent intent=new Intent(MainActivity.this,MianActivity2.class);
                    //启动Intent
                    startActivity(intent);
                }else{
                    if(zhanghu.equals(account)){
                        Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }else if(mima.equals(password)){
                        Toast.makeText(MainActivity.this, "账户名错误", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(MainActivity.this, "账户名,密码都错误", Toast.LENGTH_SHORT).show();
                    }
                }
                Intent intent=new Intent(MainActivity.this,MianActivity2.class);
                startActivity(intent);
            }
        });
        //-----------------------------------------------------------------------------------------
    }
}

Toast是为了有好的提醒用户,而又不影响用户的操作的。 方法中的MainActivity.this表示在MainActivity里显示; R.string.alert表示引用string下的alert中的资源; LENGTH_SHORT表示Toast的显示时间一微秒计算,这里调用系统定义好扥时间,也可自己写入确定的时间。 关于.show(); 是用来将定义好的Toast显示在MainActivity里的, 如果不调用.show();方法定义的Toast就没有意义了。 

z.addTextChangedListener(new TextWatcher() {});输入框改变的监听事件

CheckBox的setOnCheckedChangeListener监听器:setOnCheckedChangeListener

获取输入框的文本信息时用getText().toString()方法

密码的隐藏及显示

if (b) {
    //如果选中,显示密码
    m.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
    //否则隐藏密码
    m.setTransformationMethod(PasswordTransformationMethod.getInstance());
}

网页的跳转

用Intent实现界面的跳转

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

//(当前页面.this,将要跳转的界面.class)

startActivity(intent);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值