“我们已经有一个 EditText,只能有一个”
我为我的应用程序(LoginFragment)构建了一个片段,它可以处理两种主要的身份验证模式;即登录并注册用户。有一个按钮允许用户在“登录模式”和“注册”模式之间切换。每个“模式”都有一些其他视图不需要的额外视图。因此,有必要在切换模式时添加和删除视图。
我在 TextInputLayout 布局中使用 EditText 视图。当我执行以下操作时,我的应用程序崩溃:
- 以编程方式添加 EditText
- 以编程方式删除 EditText
- 以编程方式添加 EditText -> 崩溃
这是我得到的错误是:
java.lang.IllegalArgumentException: We already have an EditText, can only have one
at android.support.design.widget.TextInputLayout.setEditText(TextInputLayout.java:166)
at android.support.design.widget.TextInputLayout.addView(TextInputLayout.java:155)
at android.view.ViewGroup.addView(ViewGroup.java:3985)
at android.view.ViewGroup.addView(ViewGroup.java:3961)
at com.mydomain.myapp.fragments.LoginFragment.showActivateAccountViews(LoginFragment.java:317)
这来自 android.support.design.widget.TextInputLayout 它有一个内部私有 EditText 变量,该变量在添加视图时设置(下面的源代码)。看来,当我第二次尝试将视图添加到 TextInputLayout 时,mEditText 变量已经设置了。该类没有自己的 .removeView() 方法,所以我不知道应该如何删除它?
我怀疑我错误地删除了 EditText 视图,但无法弄清楚我做错了什么。我还阅读了其他一些有关删除视图的 Stack Overflow 帖子,但这些方法也没有解决问题。
有人对我如何使它起作用有任何想法吗?
下面是我自己的代码供参考。
登录片段.java
...
import android.support.design.widget.TextInputLayout;
import android.widget.EditText;
public class LoginFragment extends Fragment {
private RelativeLayout mContainer;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_login, container, false);
mContainer = ((RelativeLayout) view.findViewById(R.id.login_container));
showLoginViews();
LayoutTransition layoutTransition = mContainer.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
return view;
}
/**
* Show the view elements for Login mode
*/
private void showLoginViews() {
LayoutInflater li = (LayoutInflater)getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// Configure the button for the primary action
Button loginButton = (Button)mContainer.findViewById(R.id.button_login_fragment_primary_action);
...
// Configure the toggle button to navigate to Activate Account mode
TextView toggleButton = (TextView)mContainer.findViewById(R.id.button_toggle_mode);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginFragment.this.showActivateAccountViews();
}
});
toggleButton.setText(getResources().getString(R.string.action_activate_account));
// Hide the Member ID EditText
((TextInputLayout)mContainer.findViewById(R.id.member_id_inputlayout)).removeView(mContainer.findViewById(R.id.editText_member_id_field));
}
/**
* Show view elements for Activate Account mode
*/
private void showActivateAccountViews() {
LayoutInflater li = (LayoutInflater)getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// Configure the primary button for the primary action - Activate Account
Button activateAccountButton = (Button)mContainer.findViewById(R.id.button_login_fragment_primary_action);
...
// Add the Member ID EditText
((TextInputLayout)mContainer.findViewById(R.id.member_id_inputlayout)).addView(li.inflate(R.layout.login_member_id_element_layout, (ViewGroup)mContainer.findViewById(R.id.member_id_inputlayout), false));
// Configure the toggle button to navigate to Login mode
TextView toggleButton = (TextView)mContainer.findViewById(R.id.button_toggle_mode);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginFragment.this.showLoginViews();
}
});
toggleButton.setText(getResources().getString(R.string.action_login));
}
...
}
login_member_id_element_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editText_member_id_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/member_id" />
login_fragment.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.mydomain.myapp.fragments.LoginFragment">
<RelativeLayout
android:id="@+id/login_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<!--placeholder layout with params for activate account elements-->
<android.support.design.widget.TextInputLayout
android:id="@+id/member_id_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- a view can be added here-->
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/email_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText_email_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/button_login_fragment_primary_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/password_inputlayout"
android:text="@string/action_login" />
<!-- Toggle button for Login/Activate Account-->
<TextView
android:id="@+id/button_toggle_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/action_activate_account" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
android.support.design.widget.TextInputLayout(来自最新的22.2.1支持库)
public class TextInputLayout extends LinearLayout {
private EditText mEditText;
...
public void addView(View child, int index, LayoutParams params) {
if(child instanceof EditText) {
android.widget.LinearLayout.LayoutParams params1 = this.setEditText((EditText)child, params);
super.addView(child, 0, params1);
} else {
super.addView(child, index, params);
}
}
private android.widget.LinearLayout.LayoutParams setEditText(EditText editText, LayoutParams lp) {
if(this.mEditText != null) {
throw new IllegalArgumentException("We already have an EditText, can only have one");
} else {
this.mEditText = editText;
this.mCollapsingTextHelper.setExpandedTextSize(this.mEditText.getTextSize());
this.mEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
TextInputLayout.this.mHandler.sendEmptyMessage(0);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
this.mDefaultTextColor = this.mEditText.getHintTextColors().getDefaultColor();
this.mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View view, boolean focused) {
TextInputLayout.this.mHandler.sendEmptyMessage(0);
}
});
if(TextUtils.isEmpty(this.mHint)) {
this.setHint(this.mEditText.getHint());
this.mEditText.setHint((CharSequence)null);
}
if(this.mErrorView != null) {
ViewCompat.setPaddingRelative(this.mErrorView, ViewCompat.getPaddingStart(this.mEditText), 0, ViewCompat.getPaddingEnd(this.mEditText), this.mEditText.getPaddingBottom());
}
this.updateLabelVisibility(false);
android.widget.LinearLayout.LayoutParams newLp = new android.widget.LinearLayout.LayoutParams(lp);
Paint paint = new Paint();
paint.setTextSize(this.mCollapsingTextHelper.getExpandedTextSize());
newLp.topMargin = (int)(-paint.ascent());
return newLp;
}
}
}