Android

时间:2015-10-25 12:44:47

标签: android android-studio

作为Android的初学者,我正在尝试创建示例应用 - 注册并登录。我正在使用Android Studio!

每当我点击注册按钮时,Snackbar会显示"嘿,所有字段都是强制性的!"尽管填满了所有的领域!

以下是为注册而编写的代码。

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Register extends AppCompatActivity {

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

    new AlertDialog.Builder(this)
            .setTitle("Register Guidelines")
            .setMessage("Hey!\nAll fields are mandatory!\n\nPassword must have minimum of 8 and maximum of 20 Characters in length.\n\nIt should contains atleast:\n1 CAPITAL LETTER\n1 small letter\n1 Number\nSpecial Characters among these:\n\t$ @ ! % * ? &\nDouble Click on Register to proceed!")
            .setPositiveButton("Awesome!", null)
            .show();

    setupMessageButton();

}

public void setupMessageButton() {

    final Button messageButton = (Button) findViewById(R.id.registerButton);
    messageButton.setOnClickListener(new View.OnClickListener() {

                                         @Override
                                         public void onClick(View view) {
final EditText name = (EditText) findViewById(R.id.editText1);
                                             final EditText mobile = (EditText) findViewById(R.id.editText2);
                                             final EditText email = (EditText) findViewById(R.id.editText3);
                                             final EditText city = (EditText) findViewById(R.id.editText4);
                                             final EditText password = (EditText) findViewById(R.id.editText5);
                                             final EditText confirmPassword = (EditText) findViewById(R.id.editText6);

                                             String nameS = name.getText().toString().trim();
                                             String mobileS = mobile.getText().toString().trim();
                                             String emailS = email.getText().toString().trim();
                                             String cityS = city.getText().toString().trim();
                                             String passwordS = password.getText().toString().trim();
                                             String confirmPasswordS = confirmPassword.getText().toString().trim();

                                             Intent intent;

                                             if ((nameS.equals("")) || (mobileS.equals("")) || (emailS.equals("")) || (cityS.equals("")) || (passwordS.equals("")) || (confirmPasswordS.equals(""))) {
                                                 Snackbar.make(view, "Hey, all the fields are mandatory!", Snackbar.LENGTH_LONG).show();
                                             }
                                             if (nameS.equals(matcher))
                                             {
                                                 Snackbar.make(view, "This is name.", Snackbar.LENGTH_LONG).show();
                                             }
                                             else if (isValidName(nameS) && isValidMobile(mobileS) && isValidEmail(emailS) && isValidCity(cityS) && isValidPassword(passwordS) && isValidPassword(confirmPasswordS)) {
                                                 if (confirmPasswordS.equals(passwordS)) {
                                                     Snackbar.make(view, "Credentials validated successfully.", Snackbar.LENGTH_LONG).show();
                                                     startActivity(new Intent(Register.this, SignIn.class));
                                                 }
                                             }
                                         }

                                     }
    );
}

Pattern pattern;
Matcher matcher;

public boolean isValidName(final String name) {

    final String nameRegex = "^\\p{L}+(?: \\p{L}+)*$";
    pattern = Pattern.compile(nameRegex);
    matcher = pattern.matcher(name);
    return matcher.matches();
}

public boolean isValidMobile(final String mobile) {
    final String mobilePattern = "^[7-9][0-9]{10}$";
    pattern = Pattern.compile(mobilePattern);
    matcher = pattern.matcher(mobile);
    return matcher.matches();
}

public boolean isValidEmail(final String email) {
    final String emailRegex = "^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{10,})$";
    pattern = Pattern.compile(emailRegex);
    matcher = pattern.matcher(email);
    return matcher.matches();
}

public boolean isValidCity(final String city) {
    final String cityRegex = "^\\p{L}+(?: \\p{L}+)*$";
    pattern = Pattern.compile(cityRegex);
    matcher = pattern.matcher(city);
    return matcher.matches();
}

public boolean isValidPassword(final String password) {
    final String passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,20}";
    pattern = Pattern.compile(passwordRegex);
    matcher = pattern.matcher(password);
    return matcher.matches();
}

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_register"
tools:context=".Register">

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Register"
    android:id="@+id/textView1"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="12"
    android:inputType="text"
    android:hint="Name" >
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="12"
    android:inputType="phone"
    android:hint="Mobile" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="12"
    android:inputType="textEmailAddress"
    android:hint="Email" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="12"
    android:inputType="text"
    android:hint="City" />

<EditText
    android:id="@+id/editText5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText4"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="12"
    android:inputType="textPassword"
    android:hint="Password" />

<EditText
    android:id="@+id/editText6"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText5"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="12"
    android:inputType="textPassword"
    android:hint="Confirm Password" />

<Button
    android:id="@+id/registerButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText6"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:textSize="20dp"
    android:textColor="#5F3A7A"
    android:text="Register" />

    </LinearLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:1)

将以下代码移到onClick()方法:

    final EditText name = (EditText) findViewById(R.id.editText1);
    final EditText mobile = (EditText) findViewById(R.id.editText2);
    final EditText email = (EditText) findViewById(R.id.editText3);
    final EditText city = (EditText) findViewById(R.id.editText4);
    final EditText password = (EditText) findViewById(R.id.editText5);
    final EditText confirmPassword = (EditText) findViewById(R.id.editText6);

    String nameS = name.getText().toString().trim();
    String mobileS = mobile.getText().toString().trim();
    String emailS = email.getText().toString().trim();
    String cityS = city.getText().toString().trim();
    String passwordS = password.getText().toString().trim();
    String confirmPasswordS = confirmPassword.getText().toString().trim();

    Intent intent;

目前发生的情况是上述代码运行一次,而不是每次按下按钮。因此nameSmobileS等等始终为空。这就是它进入if区块的原因。严格来说,findViewById()代码不需要放在onClick()方法中。但是,您通过getText()获取文字的部分应该在onClick()方法

最好在onCreate()

之后立即初始化setContentView()中的所有观看次数

答案 1 :(得分:1)

更改你的代码(将你的EditText声明移到clicklistener之外):

    public class Register extends AppCompatActivity {

                EditText name;
                EditText mobile;
                EditText email;
                EditText city;
                EditText password;
                EditText confirmPassword;

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

                new AlertDialog.Builder(this)
                        .setTitle("Register Guidelines")
                        .setMessage("Hey!\nAll fields are mandatory!\n\nPassword must have minimum of 8 and maximum of 20 Characters in length.\n\nIt should contains atleast:\n1 CAPITAL LETTER\n1 small letter\n1 Number\nSpecial Characters among these:\n\t$ @ ! % * ? &\nDouble Click on Register to proceed!")
                        .setPositiveButton("Awesome!", null)
                        .show();

                name = (EditText) findViewById(R.id.editText1);
                mobile = (EditText) findViewById(R.id.editText2);
                email = (EditText) findViewById(R.id.editText3);
                city = (EditText) findViewById(R.id.editText4);
                password = (EditText) findViewById(R.id.editText5);
                confirmPassword = (EditText) findViewById(R.id.editText6);

                setupMessageButton();

            }

            public void setupMessageButton() {

                final Button messageButton = (Button) findViewById(R.id.registerButton);
                messageButton.setOnClickListener(new View.OnClickListener() {


                                                     @Override
                                                     public void onClick(View view) {
                                                     String nameS = name.getText().toString().trim();
                                                     String mobileS = mobile.getText().toString().trim();
                                                     String emailS = email.getText().toString().trim();
                                                     String cityS = city.getText().toString().trim();
                                                     String passwordS = password.getText().toString().trim();
                                                     String confirmPasswordS = confirmPassword.getText().toString().trim();

                                                     Intent intent;
                                                         if ((nameS.equals("")) || (mobileS.equals("")) || (emailS.equals("")) || (cityS.equals("")) || (passwordS.equals("")) || (confirmPasswordS.equals(""))) {
                                                             Snackbar.make(view, "Hey, all the fields are mandatory!", Snackbar.LENGTH_LONG).show();
                                                         }
                                                         if (nameS.equals(matcher))
                                                         {
                                                             Snackbar.make(view, "This is name.", Snackbar.LENGTH_LONG).show();
                                                         }
                                                         else if (isValidName(nameS) && isValidMobile(mobileS) && isValidEmail(emailS) && isValidCity(cityS) && isValidPassword(passwordS) && isValidPassword(confirmPasswordS)) {
                                                             if (confirmPasswordS.equals(passwordS)) {
                                                                 Snackbar.make(view, "Credentials validated successfully.", Snackbar.LENGTH_LONG).show();
                                                                 startActivity(new Intent(Register.this, SignIn.class));
                                                             }
                                                         }
                                                     }

                                                 }
                );
            }
    ...
    }