0

i have 8 editText in same LinearLayout. i want with click on cancel button, to set focus to edtNum.

( edtUnitNum.requestFocus(); ).

This command works incorrectly and set focus to edtSaveMony, but i want focus to edtunitnum.

In total, you can not focus on EditText which is in another Linearlayout.

xml :

  <RelativeLayout 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">


    <include layout="@layout/drawer" />

 
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.33"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:orientation="horizontal">

                    <android.support.design.widget.TextInputLayout

                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="شماره واحد . . .">

                        <EditText
                            android:focusable="true"
                            android:focusableInTouchMode="true"
                            android:id="@+id/edt_Members_UnitNum"
                            android:layout_width="70dp"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="10dp"
                            android:fontFamily="@font/yekan"
                            android:imeOptions="actionNext"
                            android:inputType="number"
                            android:maxLines="1"
                            android:textSize="12sp" />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="wrap_content"
                        android:layout_height="55dp"
                        android:hint="نام مشترک . . .">

                        <EditText
                            android:id="@+id/edt_Members_Name"
                            android:layout_width="153dp"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/yekan"
                            android:imeOptions="actionNext"
                            android:inputType="text"
                            android:maxLines="1"
                            android:textSize="12sp" />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="متراژ . . .">

                        <EditText
                            android:id="@+id/edt_Members_aria"
                            android:layout_width="45dp"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/yekan"
                            android:imeOptions="actionNext"
                            android:inputType="number"
                            android:maxLines="1"
                            android:textSize="12sp" />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:id="@+id/textInputLayout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="تعداد . . .">

                        <EditText
                            android:id="@+id/edt_Members_Qty"
                            android:layout_width="40dp"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/yekan"
                            android:imeOptions="actionNext"
                            android:inputType="number"
                            android:maxLines="1"
                            android:textSize="12sp" />
                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.33"
                    android:orientation="horizontal">

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="مبلغ ذخیره . . .">


                        <EditText
                            android:id="@+id/edt_Members_saveMoney"
                            android:layout_width="78dp"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="10dp"
                            android:fontFamily="@font/yekan"
                            android:imeOptions="actionNext"
                            android:inputType="numberDecimal"
                            android:maxLines="1"
                            android:textSize="12sp" />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="تاریخ . . .">

                        <EditText
                            android:id="@+id/edt_Members_date"
                            android:layout_width="75dp"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/yekan"
                            android:imeOptions="actionNext"
                            android:inputType="none"
                            android:maxLines="1"
                            android:textSize="12sp" />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="155dp"
                        android:layout_height="wrap_content"
                        android:hint="توضیحات . . .">

                        <EditText
                            android:id="@+id/edt_Members_desc"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/yekan"
                            android:inputType="text"
                            android:maxLines="1"
                            android:textSize="12sp" />
                    </android.support.design.widget.TextInputLayout>


                </LinearLayout>

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

                    <Button
                        android:id="@+id/btn_Members_Insert"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:layout_gravity="center"
                        android:background="@drawable/btn"
                        android:fontFamily="@font/yekan"
                        android:gravity="center"
                        android:text="ثبت"
                        android:textSize="18sp" />

                    <Button
                        android:id="@+id/btn_Members_Cancel"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:layout_gravity="center"
                        android:background="@drawable/btn"
                        android:fontFamily="@font/yekan"
                        android:gravity="center"
                        android:text="انصراف"
                        android:textSize="16sp" />

                </LinearLayout>

            </LinearLayout>
</RelativeLayout>

java :

btncancel = (Button) findViewById(R.id.btn_Members_Cancel);
btnInsert = (Button) findViewById(R.id.btn_Members_Insert);
edtUnitNum = (EditText) findViewById(R.id.edt_Members_UnitNum);
edtSaveMoney = (EditText) findViewById(R.id.edt_Members_saveMoney);

  btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            edtUnitNum.clearFocus();
            edtUnitNum.setFocusable(true);
            edtUnitNum.setFocusableInTouchMode(true);
            edtUnitNum.requestFocus();
        }
   });
Farzad Kamali
  • 751
  • 2
  • 6
  • 24

1 Answers1

0

I have tried your code and it works fine if you remove

android:focusableInTouchMode="true"

from the parent LinearLayout. In fact, why do you need to make a transparent/borderless layout focusable.

Davi
  • 1,031
  • 12
  • 21
  • Yes you are right. Thanks, it works fine when I click on Insert Button(btnInsert), but why it does not work when you press `enter` key keyboard on last `EditText`?. I call ` btnInsert.callOnClick();` on `edtDesc.setOnKeyListener`method. and in `btnInsert.setonclick...` method use `edtUnitNum.requestFocus();` – Farzad Kamali Jun 07 '18 at 14:59
  • "enter" key will not work by default, you have to set OnKeyListener for that editText. Look at this https://stackoverflow.com/questions/4451374/use-enter-key-on-softkeyboard-instead-of-clicking-button – Davi Jun 07 '18 at 16:27