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.

1

1 Answer 1

0

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);
10
  • still doesn't work. I just updated my question (xml file added). Please help.
    – wdyz
    Dec 4, 2015 at 9: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. Dec 4, 2015 at 9:26
  • I run the code on my Android phone (5.0) with SDK 23. It is OK. Dec 4, 2015 at 9:28
  • Or try add .toString() after getText(). Toast.makeText(getActivity(), EditHal.getText().toString(), Toast.LENGTH_SHORT).show(); Dec 4, 2015 at 9:29
  • no, i didn't use special input, jus a number with default keyboard. Would you like to share your "OK" code?
    – wdyz
    Dec 4, 2015 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.