“We already have an EditText,can only have one”
我为我的应用程序(LoginFragment)构建了一个Fragment,它可以处理两种主要的身份验证模式;即登录和注册用户.有一个按钮允许用户在“登录模式”和“注册”模式之间切换.每个“模式”都有一些额外的视图,而另一个视图则不需要.因此,在切换模式时需要添加和删除视图.
我在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帖子,但这些方法也没有解决问题.
>添加视图 – https://stackoverflow.com/a/13889257/1500317
>删除视图 – Call removeView() on the child’s parent first
有没有人对如何让这个工作有任何想法?
以下是我自己的代码供参考.
LoginFragment.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,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 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),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; } }}解决方法 com.androID.support.design库(v22.2.1)似乎存在限制.您无法在运行时直接删除然后将EditText添加到TextinputLayout.你可以为这个BUG here加注星标.
我已经为这个问题设计了一个解决方法.我修改了xml布局,以便不在运行时从TextinputLayout添加/删除EditText视图(这不起作用),我们将TextinputLayout本身添加/删除到linearLayout持有者.使用此解决方案,我们永远不需要从TextinputLayout中实际删除EditText.
关于此解决方案唯一需要注意的是,它使您的视图层次结构比其他方式更深入.因此,如果您已经遇到UI性能问题,请记住这一点.如果您正在阅读此版本并且com.androID.support.design v22.2.1版本可用,则可能需要检查此问题是否已解决.
否则,请参阅下面的示例代码以了解我的变通方法的实现.
LoginFragment.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,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 toggle button to navigate to Activate Account modes 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 ((linearLayout)mContainer.findVIEwByID(R.ID.member_ID_holderlayout)).removeVIEw(mContainer.findVIEwByID(R.ID.member_ID_inputlayout)); } /** * 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 ((linearLayout)mContainer.findVIEwByID(R.ID.member_ID_holderlayout)).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"?><androID.support.design.Widget.TextinputLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/member_ID_inputlayout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <EditText androID:ID="@+ID/editText_member_ID_fIEld" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="@string/member_ID" /></androID.support.design.Widget.TextinputLayout>
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"> <relativeLayout androID:ID="@+ID/login_container" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <!--placeholder for TextinputLayout to be dynamically added at runtime--> <linearLayout androID:ID="@+ID/member_ID_holderlayout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <!-- a login_member_ID_element_layout can be dynamically added/removed here at runtime--> </linearLayout> <!--TextinputLayout for static fIElds,the EditText is not removed at runtime--> <androID.support.design.Widget.TextinputLayout androID:ID="@+ID/email_inputlayout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@ID/member_ID_holderlayout"> <EditText androID:ID="@+ID/editText_email_fIEld" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:drawablepadding="@dimen/edittext_drawable_padding" androID:drawableStart="?emailicon" androID:focusable="true" androID:hint="Email" androID:inputType="textEmailAddress" /> </androID.support.design.Widget.TextinputLayout> </relativeLayout></androID.support.design.Widget.CoordinatorLayout>总结
以上是内存溢出为你收集整理的android – 在TextInputLayout中添加/删除EditText全部内容,希望文章能够帮你解决android – 在TextInputLayout中添加/删除EditText所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)