Android 为什么';活动中的findViewById()是否返回用户键入的值?

Android 为什么';活动中的findViewById()是否返回用户键入的值?,android,android-fragments,android-activity,android-fragmentactivity,Android,Android Fragments,Android Activity,Android Fragmentactivity,我正在创建一个Android应用程序,其中场景是用户注册,单击“注册”按钮,该应用程序将数据发送到RESTAPI 活动\u注册\u登录.java <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://sch

我正在创建一个Android应用程序,其中场景是用户注册,单击“注册”按钮,该应用程序将数据发送到RESTAPI

活动\u注册\u登录.java

<?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">

    <Button
        android:id="@+id/signUp"
        android:text="@string/sign_up"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/logIn"
        android:text="@string/log_in"
        app:layout_constraintStart_toEndOf="@+id/signUp" />

    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@+id/mobNum" />

    <EditText
        android:id="@+id/mobNum"
        app:layout_constraintTop_toBottomOf="@+id/countryCode" />


</android.support.constraint.ConstraintLayout>
    public class SignUpLogIn extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final ConnectionStatus connectionStatus = new ConnectionStatus(getApplicationContext());
            if(!connectionStatus.isOnline()){
                connectionStatus.displayMobileDataSettingsDialog(this, getApplicationContext());
            }
            setContentView(R.layout.activity_sign_up_log_in);


            final TextView mobNumberTextView = (TextView) findViewById(R.id.mobNum);
            final TextView passwordTextView = (TextView) findViewById(R.id.password);
            Button signUp = (Button) findViewById(R.id.signUp);


            final String mobNumber = mobNumberTextView.getText().toString();
            final String password = passwordTextView.getText().toString();

            signUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!validateInputFields(mobNumber, password)){
                        Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                        checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                        checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                        checkSignUpStatusIntent.putExtra("confirmPassword", password);
                        startService(checkSignUpStatusIntent);
                        connectionStatus.showProgress();
                    }
                }
            });
        }

private boolean validateInputFields(String mobNum, String password){
        StringBuffer errors = new StringBuffer();
        int count = 0;
        boolean hasError = false;
        if(mobNum == null || mobNum.isEmpty()){
            ++count;
            errors.append(count + ") Please enter your mobile number");
            errors.append('\n');
            hasError = true;
        }
        if(password == null || password.isEmpty()){
            //cEmail field is empty
            ++count;
            errors.append(count + ") Please enter a password");
            errors.append('\n');
            hasError = true;
        }
        if(hasError){
            Toast.makeText(this, errors.toString(), Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }
在onClick()中,值为空,尽管用户输入了值

  • 为什么呢
  • 如何修复它
  • 请在线回答

    附言:


    我无法删除上面
    .setOnClickListener


    您需要从
    onClick()方法中获取
    mobNumber
    password

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
        String mobNumber = mobNumberTextView.getText().toString();
        String password = passwordTextView.getText().toString();
    
                if(!validateInputFields(mobNumber, password)){
                    Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                    checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                    checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                    checkSignUpStatusIntent.putExtra("confirmPassword", password);
                    startService(checkSignUpStatusIntent);
                    connectionStatus.showProgress();
                }
            }
        });
    
    编辑 如果您像这样使用变量,则无需将其声明为final

      TextView mobNumberTextView = (TextView) findViewById(R.id.mobNum);
      TextView passwordTextView = (TextView) findViewById(R.id.password);
    
    和内部
    onClick()

    编辑2 我刚发现你的问题

    在validateInputFields方法中,您正在传递两个字符串,mobNum和password,在if语句中,您正在尝试向该方法传递两个字符串,这没关系,
    if(!validateInputFields(mobNumber,password))…
    但是,您试图通过该方法传递的两个字符串在
    onClick()之外,无法达到这些值,并且您的
    validateInputFields()
    方法无法工作

    编辑3 我只是看到您的xml中有这两个EditText

     <EditText
            android:id="@+id/password"
            android:inputType="textPassword"
            app:layout_constraintTop_toBottomOf="@+id/mobNum" />
    
        <EditText
            android:id="@+id/mobNum"
            app:layout_constraintTop_toBottomOf="@+id/countryCode" />
    
    您应该在xml中添加两个文本视图,或者将
    TextView mobNum….
    更改为

    EditText mobNumber...
    
    像这样

     EditText mobNumberTextView = (EditText) findViewById(R.id.mobNum);
     EditText passwordTextView = (EditText) findViewById(R.id.password);
    

    您必须在onClick中获得值

    为什么?

    findViewById
    用于绑定视图,绑定后您会立即调用
    getText
    ,这就是为什么它是空的

    用这个替换你的代码

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                String mobNumber = mobNumberTextView.getText().toString();
                String password = passwordTextView.getText().toString();
    
                if(!validateInputFields(mobNumber, password)){
                    Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                    checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                    checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                    checkSignUpStatusIntent.putExtra("confirmPassword", password);
                    startService(checkSignUpStatusIntent);
                    connectionStatus.showProgress();
                }
            }
        });
    

    请从onClickListener中的EditText获取值

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String mobNumber = mobNumberTextView.getText().toString();
                final String password = passwordTextView.getText().toString();
                if(!validateInputFields(mobNumber, password)){
                    Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                    checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                    checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                    checkSignUpStatusIntent.putExtra("confirmPassword", password);
                    startService(checkSignUpStatusIntent);
                    connectionStatus.showProgress();
                }
            }
        });
    

    您需要从
    注册中的
    edittext
    获取值。setOnClickListener

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                 String mobNumber = mobNumberTextView.getText().toString();
                 String password = passwordTextView.getText().toString();
    
                if(!validateInputFields(mobNumber, password)){
                    Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                    checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                    checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                    checkSignUpStatusIntent.putExtra("confirmPassword", password);
                    startService(checkSignUpStatusIntent);
                    connectionStatus.showProgress();
                }
            }
        });
    

    单击后,
    onClick
    中不再需要final字样!对…没有必要做最后的决定。如果在内部类中使用,我们必须使外部类变量为final。@VishalChhodwani我得到的变量“mobNumberTextView”是从内部类中访问的,如果我删除setOnClickListener外部变量的final,则需要声明为final。@SpandanaKrishnan我刚刚更新了答案。无需声明变量final。我得到的
    变量“mobNumberTextView”是从内部类中访问的,如果我删除setOnClickListener外部变量的final,则需要声明为final
    。但是,在用户输入值之前是否将变量mobNumberTextView设为final,因此它总是空的??您可以从onClick中删除final,也可以从您的文本视图中删除final,没有必要保留它们。我无法为文本视图删除final。您可以编辑您的问题并让我们知道您在哪里使用这些final变量吗?
        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String mobNumber = mobNumberTextView.getText().toString();
                final String password = passwordTextView.getText().toString();
                if(!validateInputFields(mobNumber, password)){
                    Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                    checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                    checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                    checkSignUpStatusIntent.putExtra("confirmPassword", password);
                    startService(checkSignUpStatusIntent);
                    connectionStatus.showProgress();
                }
            }
        });
    
        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                 String mobNumber = mobNumberTextView.getText().toString();
                 String password = passwordTextView.getText().toString();
    
                if(!validateInputFields(mobNumber, password)){
                    Intent checkSignUpStatusIntent = new Intent(getApplicationContext(), CrossCheckSignUpService.class);
                    checkSignUpStatusIntent.putExtra("countryCode", countryCode);
                    checkSignUpStatusIntent.putExtra("confirmMobileNumber", mobNumber);
                    checkSignUpStatusIntent.putExtra("confirmPassword", password);
                    startService(checkSignUpStatusIntent);
                    connectionStatus.showProgress();
                }
            }
        });