Search code examples
androidandroid-edittextreturn-valuekeylistenergetview

EditText setOnKeyListener not working when value changes


Here's my code :

private EditText EditHal;
....
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
layout = convertView;
if (convertView == null) {
    layout = inflater.inflate(R.layout.my_fragment, null);
}
EditHal = (EditText) layout.findViewById(R.id.input_hal);
EditHal.setClickable(true);
EditHal.setFocusableInTouchMode(true);
....
EditHal.setText("" + 1, TextView.BufferType.EDITABLE); //hardcoded at Here
EditHal.requestFocus();
EditHal.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
            Toast.makeText(getActivity(), EditHal.getText(), Toast.LENGTH_SHORT).show();
            return true;
        }
    return false;
    }
});
return layout;
}

And here is XML of my_fragment layout :

....
    <EditText
        android:id="@+id/input_hal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="numberDecimal"
        android:hint="@string/hal"
        android:maxLength="3"
        android:clickable="true"
        android:textColor="@color/white"
        android:singleLine="true"
        android:textSize="14sp" >
        <requestFocus />
    </EditText>

The issue :

on the code above, (EditText) EditHal value is "1". But, if i changes the value of EditHal to the other number, and then i hit Enter/Done Key on the keyboard, the value of EditHal still "1", no changes at all.

I want that every time i changes the value inside EditHal field, and i hit Enter/Done Key, the Toast will show a number that i have input. Please tell me how to solve it. Thanks

Edited (solved) :

I changes the code above with this :

//private EditText EditHal;
....
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
layout = convertView;
if (convertView == null) {
    layout = inflater.inflate(R.layout.my_fragment, null);
}

//final added at EditText
final EditText EditHal = (EditText) layout.findViewById(R.id.input_hal);  

EditHal.setClickable(true);
EditHal.setFocusableInTouchMode(true);
....
EditHal.setText("" + 1, TextView.BufferType.EDITABLE); //hardcoded at Here
EditHal.requestFocus();
EditHal.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
            Toast.makeText(getActivity(), EditHal.getText(), Toast.LENGTH_SHORT).show();
            return true;
        }
    return false;
    }
});
return layout;
}

and the problem is gone. Thank you Jean Yang.


Solution

  • It is becauseEditText does not know whether user finishes typing or not.

    For example, you could add android:singleLine="true" of your EditText in xml file.

    When you hit ENTER, the keyboard will close automatically and the toast shows up. Android thinks that you finished typing and close the keyboard for you; otherwise, if you allow multi-line, Android catches ENTER KEY as a newline/line-break.

    EDIT

    Add toString method after getText

    Toast.makeText(getActivity(), EditHal.getText().toString(), Toast.LENGTH_SHORT).show();
    

    And since EditHal is being accessed within inner class, declare EditHal as a final variable.

    final EditText EditHal = (EditText) findViewById(R.id.input_hal);