简体   繁体   English

为什么我的所有 EditText.getText().toString() 都返回一个空字符串?

[英]Why are all my EditText.getText().toString() returning an empty String?

This problem occurs throughout the app but I am only posting my register activity.这个问题发生在整个应用程序中,但我只发布我的注册活动。

Here is a screenshot of my register activity:这是我的注册活动的屏幕截图:

在此处输入图像描述

Here is the xml for my register activity.这是我的注册活动的 xml。 I don't think you need to read it.我认为你不需要阅读它。


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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"
        android:background="#6633ff"
        tools:context=".RegisterActivity">

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left_guideline_edit_text"
            app:layout_constraintGuide_percent=".12077"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/right_guideline_edit_text"
            app:layout_constraintGuide_percent=".87923"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_edit_text_name"
            app:layout_constraintGuide_percent=".09821"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_edit_text_name"
            app:layout_constraintGuide_percent=".17857"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_edit_text_email"
            app:layout_constraintGuide_percent=".21429"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_edit_text_email"
            app:layout_constraintGuide_percent=".29464"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_edit_text_password"
            app:layout_constraintGuide_percent=".33036"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_edit_text_password"
            app:layout_constraintGuide_percent=".41071"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_edit_text_city"
            app:layout_constraintGuide_percent=".44420"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_edit_text_city"
            app:layout_constraintGuide_percent=".52455"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_edit_text_country"
            app:layout_constraintGuide_percent=".56027"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left_guideline_register_button"
            app:layout_constraintGuide_percent=".27536"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/right_guideline_register_button"
            app:layout_constraintGuide_percent=".72222"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_register_button"
            app:layout_constraintGuide_percent=".70313"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_edit_text_country"
            app:layout_constraintGuide_percent=".64063"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_register_button"
            app:layout_constraintGuide_percent=".78125"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_edit_text_view_already_account"
            app:layout_constraintGuide_percent=".80692"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_text_view_already_account"
            app:layout_constraintGuide_percent=".84933"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_text_view_log_in"
            app:layout_constraintGuide_percent=".84933"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_text_view_log_in"
            app:layout_constraintGuide_percent=".89174"
            android:orientation="horizontal"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/edit_text_shape"
            android:id="@+id/edit_text_name"
            android:hint="Name"
            android:textColorHint="#bbbbbb"
            android:textColor="#000000"
            android:textSize="20sp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:inputType="textCapSentences"
            android:maxLines="1"
            android:imeOptions="actionNext"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_edit_text"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_edit_text"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_edit_text_name"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_edit_text_name"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/edit_text_shape"
            android:id="@+id/edit_text_email"
            android:hint="Email"
            android:textColorHint="#bbbbbb"
            android:textColor="#000000"
            android:textSize="20sp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:imeOptions="actionNext"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_edit_text"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_edit_text"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_edit_text_email"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_edit_text_email" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/edit_text_shape"
            android:id="@+id/edit_text_password"
            android:hint="Password"
            android:textColorHint="#bbbbbb"
            android:textColor="#000000"
            android:textSize="20sp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:inputType="textPassword"
            android:maxLines="1"
            android:imeOptions="actionNext"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_edit_text"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_edit_text"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_edit_text_password"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_edit_text_password" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/edit_text_shape"
            android:id="@+id/edit_text_city"
            android:hint="City"
            android:textColorHint="#bbbbbb"
            android:textColor="#000000"
            android:textSize="20sp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:inputType="textCapSentences"
            android:maxLines="1"
            android:imeOptions="actionNext"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_edit_text"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_edit_text"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_edit_text_city"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_edit_text_city" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/edit_text_shape"
            android:id="@+id/edit_text_country"
            android:hint="Country"
            android:textColorHint="#bbbbbb"
            android:textColor="#000000"
            android:textSize="20sp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:inputType="textCapSentences"
            android:maxLines="1"
            android:imeOptions="actionNext"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_edit_text"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_edit_text"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_edit_text_country"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_edit_text_country"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/register_button"
            android:background="@drawable/button_shape"
            android:fontFamily="@font/inter_extrabold"
            android:textSize="28sp"
            android:text="Register"
            android:textAlignment="center"
            android:textAllCaps="false"
            android:textColor="#6633ff"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_register_button"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_register_button"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_register_button"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_register_button"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_view_already_account"
            android:text="Already have an account?"
            android:fontFamily="@font/inter_thin"
            android:textSize="20sp"
            android:textColor="#ffffff"
            android:textAlignment="center"
            android:textAllCaps="false"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_edit_text_view_already_account"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_text_view_already_account"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_view_log_in"
            android:text="@string/LogIn"
            android:fontFamily="@font/inter_semibold"
            android:textSize="28sp"
            android:textColor="#ffffff"
            android:textAlignment="center"
            android:textAllCaps="false"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_text_view_log_in"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_text_view_log_in"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

And here is my code for the activity.这是我的活动代码。 The main part of the code is at the bottom in the goToRegisterAs() method.代码的主要部分位于 goToRegisterAs() 方法的底部。

    package com.example.treeapp;

    import androidx.appcompat.app.AppCompatActivity;

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class RegisterActivity extends AppCompatActivity {
        private EditText nameEditText;
        private EditText emailEditText;
        private EditText passwordEditText;
        private EditText cityEditText;
        private EditText countryEditText;
        private Button registerButton;
        private TextView alreadyAccountTextView;
        private TextView logInTextView;
        private String name;
        private String email;
        private String password;
        private String city;
        private String country;

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

            nameEditText = findViewById(R.id.edit_text_name);
            emailEditText = findViewById(R.id.edit_text_email);
            passwordEditText = findViewById(R.id.edit_text_password);
            cityEditText = findViewById(R.id.edit_text_city);
            countryEditText = findViewById(R.id.edit_text_country);
            registerButton = findViewById(R.id.register_button);
            alreadyAccountTextView = findViewById(R.id.text_view_already_account);
            logInTextView = findViewById(R.id.text_view_log_in);

            name = nameEditText.getText().toString();
            email = emailEditText.getText().toString();
            password = passwordEditText.getText().toString();
            city = cityEditText.getText().toString();
            country = countryEditText.getText().toString();

            registerButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    goToRegisterAs(view);
                }
            });

            countryEditText.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_ENTER) {
                        goToRegisterAs(view);
                        return true;
                    } else {
                        return false;
                    }
                }
            });

            logInTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(), MainActivity.class);

                    view.getContext().startActivity(intent);
                }
            });
        }

        void goToRegisterAs(View view) {
            if (!(name.equals("") || email.equals("") || password.equals("") ||
                city.equals("") || country.equals(""))) {
                if (email.contains("@") && email.contains(".")) {
                    if (MainActivity.database.donorsDao().checkEmail(email) == 0 &&
                        MainActivity.database.plantersDao().checkEmail(email) == 0) {
                        Intent intent = new Intent(view.getContext(), RegisterAsActivity.class);
                        intent.putExtra("name", name);
                        intent.putExtra("email", email);
                        intent.putExtra("password", password);
                        intent.putExtra("city", city);
                        intent.putExtra("country", country);

                        view.getContext().startActivity(intent);
                    } else {
                        alreadyAccountTextView.setText("Already registered. Please log in.");
                    }
                } else {
                    alreadyAccountTextView.setText("Please enter correct email address.");
                }
            } else {
                alreadyAccountTextView.setText("Please enter all fields");
                alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
            }
        }
    }

The app was not working as I wanted it to.该应用程序没有按我的意愿运行。 So after some trying, I figured out that my EditTexts are all returning an empty string.所以经过一番尝试,我发现我的 EditTexts 都返回一个空字符串。 In the last line in the code alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country) , the alreadyAccountTextView becomes empty.在代码的最后一行alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country)中, alreadyAccountTextView 变为空。 I wrote this line here because this block of code was being executed even though I had input text in every field.我在这里写了这一行,因为即使我在每个字段中都输入了文本,这段代码仍在执行。 I have read many questions and answers on StackOverflow about getting text from EditTexts and all of them say that I should use EditText.getText().toString().我在 StackOverflow 上阅读了很多关于从 EditTexts 获取文本的问题和答案,他们都说我应该使用 EditText.getText().toString()。 But it isn't working for me, due to some reason.但由于某种原因,它对我不起作用。

The problem is you initialise your Strings in your OnCreate function.问题是您在 OnCreate function 中初始化字符串。 This function is called only once, when your activity starts;当您的活动开始时,此 function 仅被调用一次; therefore, at this point your EditTexts are all empty without any text in it.因此,此时您的 EditTexts 都是空的,其中没有任何文本。

To get the current text located in your EditTexts, you should move your initialisations in your goToRegisterAs() function.要获取位于 EditTexts 中的当前文本,您应该在goToRegisterAs() function 中移动您的初始化。

    void goToRegisterAs(View view) {
            name = nameEditText.getText().toString();
            email = emailEditText.getText().toString();
            password = passwordEditText.getText().toString();
            city = cityEditText.getText().toString();
            country = countryEditText.getText().toString();
                if (!(name.equals("") || email.equals("") || password.equals("") ||
                    city.equals("") || country.equals(""))) {
                    if (email.contains("@") && email.contains(".")) {
                        if (MainActivity.database.donorsDao().checkEmail(email) == 0 &&
                            MainActivity.database.plantersDao().checkEmail(email) == 0) {
                            Intent intent = new Intent(view.getContext(), RegisterAsActivity.class);
                            intent.putExtra("name", name);
                            intent.putExtra("email", email);
                            intent.putExtra("password", password);
                            intent.putExtra("city", city);
                            intent.putExtra("country", country);
    
                            view.getContext().startActivity(intent);
                        } else {
                            alreadyAccountTextView.setText("Already registered. Please log in.");
                        }
                    } else {
                        alreadyAccountTextView.setText("Please enter correct email address.");
                    }
                } else {
                    alreadyAccountTextView.setText("Please enter all fields");
                    alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM