Edit Text focus in android

2019-05-07 04:38发布

问题:

In my application i have edit text as follows.

EditText1   EditText2
EditText3   EditText4
EditText5   EditText6

in xml i declared android:imeOptions="actionNext" and also i write

          editText1.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                // TODO Auto-generated method stub
                 if (actionId == EditorInfo.IME_ACTION_DONE ||   actionId == EditorInfo.IME_ACTION_NEXT) {
                     editText2.requestFocus();

                  return false;
                  }
                return false;
            }
         });

Now in virtual key board in edit text 1 if i press next i am getting focus into edit text3 instead of editText2 etc.

How can i get focus on EditText2 instead of edittext3 for my application user only press "Next or done" he never goes for trackball.

回答1:

You could try adding the android:nextFocusDown attribute in your EditTexts.



回答2:

I just tested your code with the following layout and it worked as expected. (Expected = clicking next on the virtual keyboard the focused was passed to editText2)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="center"
android:padding="15dip">
<LinearLayout android:orientation="vertical"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:layout_gravity="center" android:paddingLeft="20dip"
    android:paddingRight="20dip">
    <TableLayout android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:layout_gravity="center"
        android:stretchColumns="*">

        <TableRow>
            <EditText android:id="@+id/edt1" />
            <EditText android:id="@+id/edt2" />
        </TableRow>

        <TableRow>
            <EditText android:id="@+id/edt3" />
            <EditText android:id="@+id/edt4" />
        </TableRow>

        <TableRow>
            <EditText android:id="@+id/edt5" />
            <EditText android:id="@+id/edt6" />
        </TableRow>


    </TableLayout>
</LinearLayout>

Here's also the source

package com.stackoverflow.test;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.inputmethod.EditorInfo;
    import android.widget.EditText;
    import android.widget.TextView;

public class TestEditTextFocusActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText editText1 = (EditText)findViewById(R.id.edt1);
    final EditText editText2 = (EditText)findViewById(R.id.edt2);

    editText1.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
          if (actionId == EditorInfo.IME_ACTION_DONE ||   
                  actionId == EditorInfo.IME_ACTION_NEXT) {
           editText2.requestFocus();
            return false;
          }

            return false;
        }
   });
}

}



回答3:

 ed1.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    // TODO Auto-generated method stub
                     if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
                        ed1.setNextFocusDownId(R.id.ed2);

                      return false;
                      }
                    return false;
                }
             });

After writing the above code my issue is solved,Thanks all for your answers and support.