Android Studio .compareTo和.equals,字符串不起作用 [英] Android Studio .compareTo and .equals with strings not working

查看:147
本文介绍了Android Studio .compareTo和.equals,字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个基本的创建帐户系统(它是我使用android工作室的第一天,所以不要过于严厉地判断)每次我测试程序它只是默认直接到其他,即使两个密码是非常不同的。非常感谢大家的帮助。

I am trying to create a basic create account system (its my first day using android studio so dont judge too harshly) everytime i test the program it simply defaults straight to else even when the two passwords are vastly different. Thank you all very much for your help in advance.

public class CreateAccount extends AppCompatActivity {

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

    Button Submit_btn = (Button) findViewById(R.id.Submit_btn);

    final EditText Username_txt = (EditText)findViewById(R.id.Username_txt);
    final EditText Email_txt = (EditText)findViewById(R.id.Email_txt);
    final EditText Password_txt = (EditText)findViewById(R.id.Password_txt);
    final EditText VerifyPassword_txt = (EditText)findViewById(R.id.VerifyPassword_txt);

    final TextView Error_txt = (TextView)findViewById(R.id.Error_txt);

    final String Username_string = Username_txt.getText().toString();
    final String Email_string = Email_txt.getText().toString();
    final String Password_string = Password_txt.getText().toString();
    final String VerifyPassword_string = VerifyPassword_txt.getText().toString();


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

            if (!Password_string.equals(VerifyPassword_string)) {
                Error_txt.setText("Passwords must be matching");

            } else {
                Error_txt.setText("No Error");
            }
      }
}


推荐答案

你试图在onCreate it self时获取EditText的值,此时它们不会有任何值,因为你可能只在屏幕打开后输入它们。所以你应该这样做此计算仅适用于您的onclick -

You are trying to get the values of your EditText at the time of onCreate it self at which time they won't have any values in them as you might be typing them only after screen is open. So you should do this calculation only on your onclick -

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

final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
    if (!Password_string.equals(VerifyPassword_string)) {
        Error_txt.setText("Passwords must be matching");

    } else {
        Error_txt.setText("No Error");
    }
}

因此,请在此处了解活动生命周期。调用 onCreate 来设置View作为Activity的第一种方法。那时您的视图中没有任何用户填充的数据。填充视图并看到UI后,用户就会与其进行交互。因此,在用户输入后您需要执行的任何计算都必须基于操作(在这种情况下是您的btn单击),因此尝试始终在操作时获取数据。

So understand the activity Lifecycle here. onCreate is called to setup your View as the first method to your Activity. At that time your view doesn't have any user filled data in it. Once the view is populated and you see the UI, then the user interacts with it. So any computation that you need to do after user input, has to be action based(in this case your btn click) and hence try to get the data always at the time of action.

这篇关于Android Studio .compareTo和.equals,字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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