密码文字栏位 [英] Password text Field

查看:93
本文介绍了密码文字栏位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个班级做一个项目,我想在Android Studio中创建一个小的密码登录名.我想创建一些简单的东西,我知道如何在Java上做到这一点,但我不知道如何在此应用程序中做到这一点.我基本上想建立一个密码框和一个按钮.在按钮的下方,我想测试编辑文本密码框的输入,以查看其是否等于变量.该变量将被设置并确定为类似于root的名称.我需要找到一种方法来测试密码字段上的输出,以查看其是否等于变量.如果是这样,它将移动到另一个页面.代码将在下面

I am doing a project for class where I would like to create a little password login in Android Studio. I want to create something simple and I know how to do it on Java but I don't know how I would go about doing it in this application. I basically want to get up a password box and a button. On down of the button I would like to test the input of the edit text password box to see if it equals the variable. This variable would be set and definite to something like root. I need to find a way to test that output on the password field to see if it equals the variable. If it does then it would move to another page. The code will be below

我的Java文件:

package com.example.murdocbgould.passwordpt4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        String passwordA = "root";
    }
}

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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="com.example.murdocbgould.passwordpt4.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword"
        tools:layout_editor_absoluteX="85dp"
        tools:layout_editor_absoluteY="260dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password:"
        tools:layout_editor_absoluteX="160dp"
        tools:layout_editor_absoluteY="226dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="437dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="328dp"
        android:layout_height="43dp"
        android:text="Bluetooth Texting Login"
        android:textSize="30sp"
        tools:layout_editor_absoluteX="28dp"
        tools:layout_editor_absoluteY="147dp" />

</android.support.constraint.ConstraintLayout>

推荐答案

在这里从edittest获取文本,并在按钮单击时进行比较(如果匹配),然后进行比较.

Here getting text from edittest and compare on button click if it matches than go to another activity.

public class MainActivity extends AppCompatActivity {

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

            final String passwordA = "root";

            EditText editText = (EditText) findViewById(R.id.editText);
            Button button = (Button) findViewById(R.id.button);
            TextView textView2 = (TextView) findViewById(R.id.textView2);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

               textView2.setText(editText.getText().toString().trim());
                    if(editText.getText().toString().trim().equals(passwordA)){
                        Intent i = new Intent(MainActivity.this, SecondActivity.class);
                        startActivity(i);
                    }else{
                    // Do what you want when password is not matches.
                    }
                }
            });
        }
    }

这篇关于密码文字栏位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆