1

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.

wdyz
  • 155
  • 3
  • 18
  • use this link for getting value when press enter or done http://stackoverflow.com/questions/8063439/android-edittext-finished-typing-event – Mayur Sakhiya Dec 04 '15 at 09:46

1 Answers1

1

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);
Jean Y.C. Yang
  • 3,682
  • 4
  • 16
  • 26
  • still doesn't work. I just updated my question (xml file added). Please help. – wdyz Dec 04 '15 at 09:24
  • did you use a special input keyboard? for instance, i use HTC input, it won't "send" what you type to EditText until you click its suggestion or press ENTER. This is, you have to double click enter. – Jean Y.C. Yang Dec 04 '15 at 09:26
  • I run the code on my Android phone (5.0) with SDK 23. It is OK. – Jean Y.C. Yang Dec 04 '15 at 09:28
  • Or try add `.toString()` after `getText()`. `Toast.makeText(getActivity(), EditHal.getText().toString(), Toast.LENGTH_SHORT).show();` – Jean Y.C. Yang Dec 04 '15 at 09:29
  • no, i didn't use special input, jus a number with default keyboard. Would you like to share your "OK" code? – wdyz Dec 04 '15 at 09:33
  • i tried to add .toString() after getText(), and run this app. And i change EditHal field number from "1" to "2", but the Toast still show a number "1" after i press an enter key – wdyz Dec 04 '15 at 09:34
  • Do you add `final` to your `EditText`? Something like `final EditText EditHal = (EditText) findViewById(R.id.input_hal);` Tell me If it still doesn't work, and I'll edit my answer and paste my code – Jean Y.C. Yang Dec 04 '15 at 09:41
  • haha.. thanks, bro! you save my life. It's weird. Why after i add `final` on my `EditText` and the problem is solved when my code doesn't show any error at all? Thanks! :D – wdyz Dec 04 '15 at 09:55
  • Your editor should give your a hint "Variable EditHal is accessed from within inner class needs to be declared final" and it seems not. You may update your Android Studio or your favorite IDE :P – Jean Y.C. Yang Dec 04 '15 at 09:59