Question

I want to listen to key events for my EditText's, and whenever next is pressed I want to save text of EditText to my data object. I implemented OnKeyListener and its onKey() method which is never called and cant get why... Here's my code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState) {
    v = inflater.inflate(R.layout.frag_userprofile_myinfo, container, false);
    context = getActivity().getApplicationContext();
    global = (eKeshGlobal) context;

    pbWait = (ProgressBar) v.findViewById(R.id.pbWait);

    bnDone = (Button) v.findViewById(R.id.bnDone);
    rlBack = (RelativeLayout) v.findViewById(R.id.rlBack);

    etFirstName = (EditText) v.findViewById(R.id.etFirstName);
    etLastName = (EditText) v.findViewById(R.id.etLastName);
    etAdress = (EditText) v.findViewById(R.id.etAdress);
    etMobile = (EditText) v.findViewById(R.id.etMobile);
    etDob = (EditText) v.findViewById(R.id.etDob);
    etOib = (EditText) v.findViewById(R.id.etOib);

    etFirstName.setOnKeyListener(this);
    etLastName.setOnKeyListener(this);
    etAdress.setOnKeyListener(this);
    etMobile.setOnKeyListener(this);
    etDob.setOnKeyListener(this);
    etOib.setOnKeyListener(this);


    return v;

}

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

        switch(v.getId()) {

        case R.id.etFirstName:
            user.setFirstName(etFirstName.getText().toString());
            return true;

        case R.id.etLastName:
            user.setLastName(etLastName.getText().toString());
            return true;

        case R.id.etAdress:
            user.setAddress(etAdress.getText().toString());
            return true;

        case R.id.etMobile:
            user.setMobile(etMobile.getText().toString());
            return true;

        case R.id.etDob:
            user.setDateOfBirth(Utils.formatDate(context, 
etDob.getText().toString()));
            return true;

        case R.id.etOib:
            user.setOib(etOib.getText().toString());
            return true;

        default:
            return false;
        }   
    }

    return false;
}

Of course I've implemented listener:

public class UserProfileMyInfo extends Fragment implements View.OnKeyListener

And here's the sample of EditText xml:

<EditText
      android:id="@+id/etLastName"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="left"
      android:ems="10"
      android:hint="@string/my_profile_lastname"
      android:imeOptions="actionNext"
      android:inputType="text"
      android:singleLine="true" />

I'm really running out of ideas to get this work, I tryed with onKeyDown() too but didn't work either.. Thanks in advance!

Was it helpful?

Solution 2

You should consider using android:imeOptions if you only want to capture the Next key event.

Set this in your manifest as,

<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/search_hint"
android:inputType="text"
android:imeOptions="actionNext" />

and catch this in code calling setOnEditorActionListener() method on your EdiText

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    boolean handled = false;
    if (actionId == EditorInfo.IME_ACTION_SEND) {
        //do your stuff here
        handled = true;
    }
    return handled;
 }
});

There are whole bunch of other options. Do visit here.

OTHER TIPS

From the docs, View.OnKeyListener is for hardware keys. This is why it doesnt work.

Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top