简体   繁体   中英

how to change focus to edit text rather then button

layout file:-

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@drawable/scroll_bg" >

 <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="521dp"
  android:layout_marginBottom="60dp"
  android:background="@drawable/bg" >

<com.deemtech.widgets.MenuLayout
    android:id="@+id/menuTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/txtCustomerDetails"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="140dp"
    android:gravity="center"
    android:text="@string/customer_details"
    android:textColor="#ffffff"
    android:textSize="12sp" />

<EditText
    android:id="@+id/editNameCustomerDetails"
    style="@style/EditText"
    android:layout_below="@+id/txtCustomerDetails"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:ems="10"
    android:hint="NAME"
    android:inputType="text" />

<RelativeLayout
    android:id="@+id/innerLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="190dp"
    android:background="@drawable/bg_create_account1x"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    android:paddingTop="8dp" >

    <EditText
        android:id="@+id/editEmailCustomerDetails"
        style="@style/EditText"
        android:layout_alignLeft="@+id/editPhoneCutomerDetails"
        android:layout_alignParentTop="true"
        android:layout_marginTop="52dp"
        android:ems="10"
        android:hint="EMAIL ADDRESS"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editPhoneCutomerDetails"
        style="@style/EditText"
        android:layout_below="@+id/editEmailCustomerDetails"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="CONTACT NUMBER"
        android:maxLength="12"
        android:inputType="phone" />

    <!--
    <EditText
        android:id="@+id/editAdditionalInfo"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/editPhoneCutomerDetails"
        android:layout_alignRight="@+id/editPhoneCutomerDetails"
        android:layout_below="@+id/editPhoneCutomerDetails"
        android:layout_marginBottom="60dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/message_additional_box1x"
        android:ems="10"
        android:hint="ADDITIONAL INFO"
        android:inputType="text"
        android:padding="10dp"
        >

        <requestFocus />
    </EditText>
    -->

    <EditText
        android:id="@+id/editAdditionalInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editPhoneCutomerDetails"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/editPhoneCutomerDetails"
        android:layout_below="@+id/editPhoneCutomerDetails"
        android:layout_marginBottom="60dp"
        android:layout_marginTop="8dp"
        android:hint="ADDITIONAL INFO"
        android:background="@drawable/message_additional_box1x"
        android:ems="10"
        android:singleLine="true"
        android:gravity="top"
        android:lines="3"
        android:padding="10dp"
        android:width="150dp" >

        <requestFocus />
    </EditText>
</RelativeLayout>

<Button
    android:id="@+id/btnBackCustomerDetails"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="448dp"
    android:layout_toLeftOf="@+id/btnSubmitCustomerDetails"
    android:background="@drawable/btn_back"
    android:contentDescription="@string/app_name" />

<Button
    android:id="@+id/btnSubmitCustomerDetails"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/editNameCustomerDetails"
    android:layout_marginTop="448dp"
    android:background="@drawable/btn_submit"
    android:contentDescription="@string/app_name" />

and my code is:-

 editPhoneCustomerDetails.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {

                findViewById(R.id.editAdditionalInfo).requestFocusFromTouch();



            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
            InputMethodManager.HIDE_IMPLICIT_ONLY);


                //((ScrollView) findViewById(R.id.scrollView)).fullScroll(View.FOCUS_DOWN);

                //findViewById(R.id.editPhoneCutomerDetails).requestFocusFromTouch();

               // findViewById(R.id.btnBackCustomerDetails).requestFocusFromTouch(); 
            }
            return false;
        }
    });

here i want to perform as when user click next button from phone details, it should focus last edit text and should also show both buttons. my problem is that when i click on next button from soft input mode it focuses to back button rather then last edit text.

please help.

note:- i have to show both buttons together with last edit text.

This should do the trick:

EditText editNameCustomerDetails = 
    (EditText)findViewById(R.id.editNameCustomerDetails);

editNameCustomerDetails.requestFocus();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM