Android判断两次输入密码是否相等并且不为空

Android开发中,登录注册都是很常有的,注册时需要判断用户输入密码是否为空,两次输入密码是否一致,下面就简单地说说

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"
    android:background="#FAFAFA"


    android:weightSum="1">

    <TextView
        android:text="输入密码完成注册"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tips"
        android:textColor="@color/green"
        android:textSize="35sp"
        android:gravity="center"

        android:layout_weight="0.25" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:textScaleX="1.2"
        android:hint=" 输入密码"
        android:background="@drawable/login_edit_bg"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:id="@+id/et_password"
        android:drawableLeft="@mipmap/password2"
        android:paddingLeft="10dp"
        android:drawableRight="@mipmap/no_look"
        android:paddingRight="15dp"
        android:layout_weight="0.06" />

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp">

    </View>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:textScaleX="1.2"
        android:hint=" 再次输入密码"
        android:background="@drawable/login_edit_bg"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:id="@+id/et_password2"
        android:drawableLeft="@mipmap/password2"
        android:drawableRight="@mipmap/no_look"
        android:paddingLeft="10dp"
        android:paddingRight="15dp"
        android:layout_weight="0.06" />

    <Button
        android:text="确定"
        android:layout_width="match_parent"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:layout_height="46dp"
        android:id="@+id/button_password"
        android:layout_marginTop="100dp"
        android:textSize="22sp"
        android:background="@drawable/login_button_bg"
        android:textColor="@color/white"
        android:layout_weight="0.02" />
</LinearLayout>

Activity中判断动作的代码:

//监听检查输入是否为空,输入是否相等...
button.setOnClickListener(new View.OnClickListener() {	//给注册按钮设置监听
    @Override
    public void onClick(View v) {
        password=editText.getText().toString().trim();//第一次输入的密码赋值给password
        password2=editText2.getText().toString().trim();//第二次输入的密码赋值给password2

        if (password.equals("")||password2.equals("")){	//判断两次密码是否为空
            Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_SHORT).show();
        }else if(password.equals(password2)){
            Toast.makeText(getApplication(),"注册成功",Toast.LENGTH_SHORT).show();
		//把Editext里面的密码上传到数据库
            BeanLab beanLab=BeanLab.get(getApplicationContext());
            beanLab.addValues(phone,password);
            //注册成功后进入提前写好的登录页面 
            Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
            //intent.putExtra(,);//可以填入用户信息,如ID等
            startActivity(intent);
            finish();


        }else if (password.equals("") != password2.equals("")){
            Toast.makeText(getApplication(),"密码不一致,请重新输入",Toast.LENGTH_SHORT).show();
        }

    }
});

以上代码只是其中的一部分,如果运行效果需要完善后才能体现,密码规范的提醒是通过手机下方的Toast呈现的。

猜你喜欢

转载自blog.csdn.net/Nobody_else_/article/details/80266124
今日推荐