EditText不会更新其文本

时间:2019-06-23 14:11:44

标签: java android android-edittext tostring

EDIT2: 使用

fragTransaction.add(fragment, tag);

代替

fragTransaction.add(id, fragment);

OR

出于某种奇怪的原因,删除MainActivity中的所有Fragments代码会使应用程序再次运行。


当按下按钮时,通过.getText().toString()获取文本将返回null / empty。

尝试手动.setText("something"),当调用.getText().toString()时有效,

修改文本后,调用.getText().toString()仍会返回“内容”。

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragTransaction = fragmentManager.beginTransaction();

        Fragment loginFrag = new LoginFragment();

        fragTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        fragTransaction.add(R.id.mainFrag, loginFrag);
        fragTransaction.commit();
    }

LoginFragment.java

public class LoginFragment extends Fragment{
    EditText fieldID;
    EditText fieldPassword;
    Button logButton;
    private FirebaseAuth mAuth;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login_menu, container, false);
    }

    @Override
    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        mAuth = FirebaseAuth.getInstance();
        view = v;

        fieldID = v.findViewById(R.id.fieldID);
        fieldPassword = v.findViewById(R.id.fieldPassword);
        logButton = v.findViewById(R.id.button_login);



        logButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signIn(fieldID.getText().toString(), fieldPassword.getText().toString());
            }
        });
    }

    private void signIn(String email, String password) {
        System.out.println("Email: " + email);
        System.out.println("Pass: " + password);

        if(!email.isEmpty() && !password.isEmpty()) {
            mAuth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
                                Log.d("Auth", "signInWithEmail:success");
                                FirebaseUser user = mAuth.getCurrentUser();
                                //updateUI(user);

                                view.findViewById(R.id.logoMain).setVisibility(View.GONE);
                            } else {
                                // If sign in fails, display a message to the user.
                                Log.w("Auth", "signInWithEmail:failure", task.getException());
                                //updateUI(null);
                            }
                        }
                    });
        }
    }
}

主要活动

<?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"
    tools:context=".presenter.Activity.MainActivity">

    <fragment
        android:id="@+id/mainFrag"
        android:name="com.raze.mfa.presenter.Fragment.LoginFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

片段布局

<?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="#FFFFFF"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="110dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/logoMain"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/bak"
        tools:srcCompat="@drawable/bak" />

    <ImageView
        android:id="@+id/iconlock"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toStartOf="@+id/fieldPassword"
        app:layout_constraintHorizontal_bias="0.523"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/iconuser"
        app:srcCompat="@mipmap/lockicon" />

    <ImageView
        android:id="@+id/iconuser"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="320dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toStartOf="@+id/fieldID"
        app:layout_constraintHorizontal_bias="0.522"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/usericon" />

    <Button
        android:id="@+id/button_login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/loginText"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fieldPassword"
        app:layout_constraintVertical_bias="0.188" />

    <EditText
        android:id="@+id/fieldPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="72dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:singleLine="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fieldID" />

    <EditText
        android:id="@+id/fieldID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="320dp"
        android:layout_marginEnd="72dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:singleLine="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/logoMain"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="108dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toTopOf="@+id/fieldID"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/logoimg" />

    <Button
        android:id="@+id/button_register"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/register"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_login"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

不相关: 我已经使用Google进行了几乎所有内容的搜索,但是似乎我不了解使用Fragments,Activites,上下文,View等的基础知识。

1 个答案:

答案 0 :(得分:2)

好的,我知道,您已经在MainActivity布局中使用了{% for username in usernames %} Username: {{ username }} {% endfor %} 标签,然后在其中添加了<fragment/>片段。

该事务适用于FragmentTransaction个为空且有片段的容器。通过FrameLayout标签静态添加片段时,片段已经加载到屏幕中。

当您将新片段添加到片段标签时,可能发生了奇怪的事情,也许它复制了片段,并且正在读取实际片段后面的片段,而没有从用户那里得到输入。

请阅读此处以正确使用FragmentTransaction:https://developer.android.com/training/basics/fragments/fragment-ui

根据上述文档,了解他们如何使用<fragment/>作为容器。

XML:

FrameLayout

Java:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />