ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android-Edittext requestFocus()动态无法正常工作

2019-11-18 17:28:00  阅读:201  来源: 互联网

标签:android-edittext android


接下来,我有多个作用于Edittexts,我在xml中添加了nextFocus值.

但是,根据未成年人复选框值的状态,一个编辑文本是可见的还是不可见的.因此,我需要在YYYY editext动态之后将重点放在它上.但是动态的requestFocus()无法正常工作.

单击下一步需要做什么

enter image description here

仅当未选中18岁以下复选框时,此电子邮件视图才可见.

What happends on next button click after YYYY edittext : control goes
to username Edittext. I cant use FocusNext in xml as the app crashes
whenever the email textview is GONE

 dobYearEt.setOnEditorActionListener(new EditText.OnEditorActionListener() {

        @Override

        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.i("focus","on yyyy");
            if (actionId == EditorInfo.IME_ACTION_DONE ||
              actionId == EditorInfo.IME_ACTION_NEXT) {
                Log.i("focus",isUnderAge+"");
                if(isUnderAge){
                    username_reg_ev_dob_row.setFocusableInTouchMode(true);
                    username_reg_ev_dob_row.requestFocus();
                }else{
// NOTHING HAPPENS
//                    email_ev.setFocusableInTouchMode(true);
//                    dobYearEt.clearFocus();
                  email_ev.requestFocus();
//                    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    mgr.showSoftInput(email_ev, InputMethodManager.SHOW_IMPLICIT);
                }

                return false;
            }

Xml

  <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/register"
                android:layout_marginLeft="@dimen/sign_up_left_right_margin"
                android:layout_marginRight="@dimen/sign_up_left_right_margin"
                android:layout_marginTop="@dimen/sign_up_user_name_top_margin">

                <!--<RelativeLayout-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="wrap_content">-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">


                    <LinearLayout
                        android:id="@+id/name_container"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="3">

                        <EditText
                            android:id="@+id/firstname_ev"
                            style="@style/RegisterViewStyle"
                            android:hint="  FIRST NAME:"
                            android:textCursorDrawable="@null"
                            android:nextFocusForward="@+id/middle_ev"
                            android:nextFocusRight="@+id/middle_ev"
                            android:nextFocusDown="@+id/middle_ev"
                            android:nextFocusLeft="@+id/middle_ev"
                            android:nextFocusUp="@+id/middle_ev"/>

                        <View
                            android:layout_width="@dimen/sign_up_names_sep_width"
                            android:layout_height="wrap_content"/>

                        <EditText
                            android:id="@+id/middle_ev"
                            style="@style/RegisterViewStyle"
                            android:hint="  MIDDLE INITIAL:"
                            android:textCursorDrawable="@null"
                            android:nextFocusForward="@+id/lastname_ev"
                            android:nextFocusRight="@+id/lastname_ev"
                            android:nextFocusDown="@+id/lastname_ev"
                            android:nextFocusLeft="@+id/lastname_ev"
                            android:nextFocusUp="@+id/lastname_ev"/>

                        <View
                            android:layout_width="@dimen/sign_up_names_sep_width"
                            android:layout_height="wrap_content"/>

                        <EditText
                            android:id="@+id/lastname_ev"
                            style="@style/RegisterViewStyle"
                            android:hint="  LAST NAME:"
                            android:textCursorDrawable="@null"
                            android:nextFocusForward="@+id/dob_mm_et"
                            android:nextFocusRight="@+id/dob_mm_et"
                            android:nextFocusDown="@+id/dob_mm_et"
                            android:nextFocusLeft="@+id/dob_mm_et"
                            android:nextFocusUp="@+id/dob_mm_et"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">


                        <CheckBox
                            android:id="@+id/under18_check_box"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@color/raffle_blue_color"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/under18_tv"
                            android:text="I'm under 18 and/or don't have an email?"/>

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/dob_parent_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <!--android:weightSum="2"-->

                        <LinearLayout
                            style="@style/RegisterViewStyle2"
                            android:id="@+id/dob_container"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_marginRight="2dp"
                            android:layout_marginTop="@dimen/sign_up_field_sep_margin"
                            android:layout_weight="1"
                            android:orientation="horizontal">
                            <!--style="@style/RegisterViewStyle2"-->
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="DOB:"
                                android:textColor="@color/edit_text_hint_color"
                                android:id="@+id/dob_label_tv"
                                />

                            <!--<EditText-->
                                <!--android:id="@+id/dob_dd_et"-->
                                <!--android:layout_width="wrap_content"-->
                                <!--android:layout_height="wrap_content"-->
                                <!--android:background="@android:color/transparent"-->
                                <!--android:hint="_ _"-->
                                <!--android:textCursorDrawable="@null"-->
                                <!--android:inputType="number"-->
                                <!--android:maxLength="2"-->
                                <!--android:nextFocusForward="@+id/dob_mm_et"-->
                                <!--android:nextFocusRight="@+id/dob_mm_et"-->
                                <!--android:nextFocusDown="@+id/dob_mm_et"-->
                                <!--android:nextFocusLeft="@+id/dob_mm_et"-->
                                <!--android:nextFocusUp="@+id/dob_mm_et"/>-->

                            <EditText
                                android:id="@+id/dob_mm_et"
                                style="@style/RegisterViewStyle"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@android:color/transparent"
                                android:hint="MM"
                                android:textCursorDrawable="@null"
                                android:inputType="number"
                                android:maxLength="2"
                                android:nextFocusForward="@+id/dob_dd_et"
                                android:nextFocusRight="@+id/dob_dd_et"
                                android:nextFocusDown="@+id/dob_dd_et"
                                android:nextFocusLeft="@+id/dob_dd_et"
                                android:nextFocusUp="@+id/dob_dd_et"/>


                            <TextView
                                android:textColor="@color/edit_text_hint_color"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=" / "/>

                            <!--<EditText-->
                                <!--android:id="@+id/dob_mm_et"-->
                                <!--android:layout_width="wrap_content"-->
                                <!--android:layout_height="wrap_content"-->
                                <!--android:background="@android:color/transparent"-->
                                <!--android:hint="_ _"-->
                                <!--android:inputType="number"-->
                                <!--android:textCursorDrawable="@null"-->
                                <!--android:maxLength="2"-->
                                <!--android:nextFocusForward="@+id/dob_yyyy_et"-->
                                <!--android:nextFocusRight="@+id/dob_yyyy_et"-->
                                <!--android:nextFocusDown="@+id/dob_yyyy_et"-->
                                <!--android:nextFocusLeft="@+id/dob_yyyy_et"-->
                                <!--android:nextFocusUp="@+id/dob_yyyy_et"/>-->
                            <EditText
                                android:id="@+id/dob_dd_et"
                                style="@style/RegisterViewStyle"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@android:color/transparent"
                                android:hint="DD"
                                android:inputType="number"
                                android:textCursorDrawable="@null"
                                android:maxLength="2"
                                android:nextFocusForward="@+id/dob_yyyy_et"
                                android:nextFocusRight="@+id/dob_yyyy_et"
                                android:nextFocusDown="@+id/dob_yyyy_et"
                                android:nextFocusLeft="@+id/dob_yyyy_et"
                                android:nextFocusUp="@+id/dob_yyyy_et"/>


                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=" / "
                                android:textColor="@color/edit_text_hint_color"/>


                            <EditText
                                style="@style/RegisterViewStyle"
                                android:id="@+id/dob_yyyy_et"
                                android:layout_width="wrap_content"
                                android:textCursorDrawable="@null"
                                android:layout_height="wrap_content"
                                android:background="@android:color/transparent"
                                android:hint="YYYY"
                                android:inputType="number"
                                android:maxLength="4"
                                />
                            <!--android:nextFocusForward="@+id/email_ev"-->
                            <!--android:nextFocusRight="@+id/email_ev"-->
                            <!--android:nextFocusDown="@+id/email_ev"-->
                            <!--android:nextFocusLeft="@+id/email_ev"-->
                            <!--android:nextFocusUp="@+id/email_ev"-->
                        </LinearLayout>

                        <EditText
                            android:id="@+id/email_ev"
                            style="@style/RegisterViewStyle"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_marginTop="@dimen/sign_up_field_sep_margin"
                            android:layout_weight="1"
                            android:hint="  EMAIL:"
                            android:textCursorDrawable="@null"

                            />
                        <!--android:nextFocusForward="@+id/password_reg_ev"-->
                        <!--android:nextFocusRight="@+id/password_reg_ev"-->
                        <!--android:nextFocusDown="@+id/password_reg_ev"-->
                        <!--android:nextFocusLeft="@+id/password_reg_ev"-->
                        <!--android:nextFocusUp="@+id/password_reg_ev"-->
                        <!--style="@style/RegisterViewStyle2"-->
                        <EditText
                            android:id="@+id/username_reg_ev_dob_row"
                            style="@style/RegisterViewStyle"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_marginTop="@dimen/sign_up_field_sep_margin"
                            android:layout_weight="1"
                            android:hint="  USERNAME:"
                            android:textCursorDrawable="@null"
                            android:visibility="gone"
                            android:nextFocusForward="@+id/password_reg_ev"
                            android:nextFocusRight="@+id/password_reg_ev"
                            android:nextFocusDown="@+id/password_reg_ev"
                            android:nextFocusLeft="@+id/password_reg_ev"
                            android:nextFocusUp="@+id/password_reg_ev"/>

                    </LinearLayout>

                    <EditText
                        android:id="@+id/username_reg_ev"
                        style="@style/RegisterViewStyle2"
                        android:layout_below="@+id/email_ev"
                        android:layout_marginTop="@dimen/sign_up_field_sep_margin"
                        android:hint="  USERNAME:"
                        android:textCursorDrawable="@null"
                        android:nextFocusForward="@+id/password_reg_ev"
                        android:nextFocusRight="@+id/password_reg_ev"
                        android:nextFocusDown="@+id/password_reg_ev"
                        android:nextFocusLeft="@+id/password_reg_ev"
                        android:nextFocusUp="@+id/password_reg_ev"/>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <EditText
                            android:id="@+id/password_reg_ev"
                            style="@style/RegisterViewStyle2"
                            android:layout_below="@+id/username_reg_ev"
                            android:layout_marginTop="@dimen/sign_up_field_sep_margin"
                            android:hint="  PASSWORD:"
                            android:layout_marginRight="2dp"
                            android:textCursorDrawable="@null"
                            android:inputType="textPassword"
                            android:nextFocusForward="@+id/confirm_password_reg_ev"
                            android:nextFocusRight="@+id/confirm_password_reg_ev"
                            android:nextFocusDown="@+id/confirm_password_reg_ev"
                            android:nextFocusLeft="@+id/confirm_password_reg_ev"
                            android:nextFocusUp="@+id/confirm_password_reg_ev"/>

                        <EditText
                            android:id="@+id/confirm_password_reg_ev"
                            style="@style/RegisterViewStyle2"
                            android:layout_below="@+id/password_reg_ev"
                            android:layout_marginTop="@dimen/sign_up_field_sep_margin"
                            android:hint="  CONFIRM PASSWORD:"
                            android:textCursorDrawable="@null"
                            android:inputType="textPassword"
                            android:nextFocusForward="@+id/group_code_reg_ev"
                            android:nextFocusRight="@+id/group_code_reg_ev"
                            android:nextFocusDown="@+id/group_code_reg_ev"
                            android:nextFocusLeft="@+id/group_code_reg_ev"
                            android:nextFocusUp="@+id/group_code_reg_ev"/>
                    </LinearLayout>

                    <EditText
                        android:id="@+id/group_code_reg_ev"
                        style="@style/RegisterViewStyle2"
                        android:layout_below="@+id/confirm_password_reg_ev"
                        android:layout_marginTop="@dimen/sign_up_field_sep_margin"
                        android:textCursorDrawable="@null"
                        android:hint="  GROUP CODE(OPTIONAL):"/>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <CheckBox
                            android:id="@+id/terms_conditions_signup_checkbox"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@color/raffle_blue_color"
                            />

                        <TextView
                            android:id="@+id/terms_conditions_register_message"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/terms_conditions"/>

                    </LinearLayout>

                    <RelativeLayout
                        android:id="@+id/register_btn_rl"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/registration_message"
                        android:layout_marginBottom="6dp"
                        android:layout_marginTop="@dimen/sign_up_btn_top_margin"
                        android:background="@color/raffle_blue_color">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:layout_centerVertical="true"
                            android:id="@+id/register_tv"
                            android:padding="6dp"
                            android:text="REGISTER"
                            android:textColor="@android:color/white"
                            android:textSize="@dimen/sign_up_edit_text_size"/>
                    </RelativeLayout>
                </LinearLayout>
            </ScrollView>

我提到过各种解决方案,但没有任何效果

Set next EditText focused and editable on KEY_DOWN

requestFocus not working

EditText request focus not working

requestFocus not working

解决方法:

发生的是我从onEditorAction返回false,因此默认的编辑器操作也被前后执行,并且焦点立即从电子邮件editText移开.现在我返回true并且没有执行默认操作,现在可以正常工作了.

dobYearEt.setOnEditorActionListener(new EditText.OnEditor ActionListener() {

        @Override

        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.i("focus","on yyyy");
            if (actionId == EditorInfo.IME_ACTION_DONE ||
              actionId == EditorInfo.IME_ACTION_NEXT) {
                Log.i("focus",isUnderAge+"");
                if(isUnderAge){
                    username_reg_ev_dob_row.setFocusableInTouchMode(true);
                    username_reg_ev_dob_row.requestFocus();
                }else{

                  email_ev.requestFocus();

                }
                  return true ; // NOW WORKS
               // return false;
            }

标签:android-edittext,android
来源: https://codeday.me/bug/20191118/2029739.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有