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

huangapple go评论77阅读模式
英文:

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

问题

I see you're having issues with your Android app code. It seems like you're trying to set the text of various EditText fields, but they all return empty strings. The issue might be related to how you're retrieving the text from the EditText fields. You should use EditText.getText().toString() to get the text. Here's your code with corrected syntax:

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

Make sure to replace the existing code with this corrected version to properly retrieve the text from your EditText fields. This should resolve the issue you're facing with empty strings.

英文:

This problem occurs throughout the app but I am only posting my register activity.

Here is a screenshot of my register activity:

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

Here is the xml for my register activity. 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.

    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. In the last line in the code alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country), the alreadyAccountTextView becomes empty. 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(). But it isn't working for me, due to some reason.

答案1

得分: 1

以下是您要翻译的内容:

问题在于您在OnCreate函数中初始化了您的字符串。这个函数仅在活动启动时调用一次;因此,在此时,您的EditText都是空的,没有任何文本。

要获取EditText中当前的文本,您应该将初始化移到您的goToRegisterAs()函数中。

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("已注册。请登录。");
            }
        } else {
            alreadyAccountTextView.setText("请输入正确的电子邮件地址。");
        }
    } else {
        alreadyAccountTextView.setText("请输入所有字段");
        alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
    }
}
英文:

The problem is you initialise your Strings in your OnCreate function. This function is called only once, when your activity starts; therefore, at this point your EditTexts are all empty without any text in it.

To get the current text located in your EditTexts, you should move your initialisations in your 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);
}
}

huangapple
  • 本文由 发表于 2020年8月12日 00:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63362327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定