2

I have an EditText that let me type in the an IP address: In the xml:

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:id="@+id/enterIP"
            android:layout_weight="1"
            android:onClick="enterIP"
            android:inputType="textPhonetic"
            android:imeOptions="actionDone"
            />

I used android:imeOptions="actionDone" so that the input box will disappear after I press Done. In the Java:

public void enterIP(View view) {
        EditText theIP = (EditText) findViewById(R.id.enterIP);
        try {
            myIP = theIP.getText().toString(); 
            validIP = ipvalidator.validate(myIP);
        } catch (NullPointerException e)
        {
            Log.d("Error", "Input address is NULL.");
        }
        Toast.makeText(getApplicationContext(), "New IP is " + myIP, Toast.LENGTH_LONG).show();
    }

However, the problem with this is that when I pressed Done, myIP still holds the old value. Only when I touch the EditText to bring up the input again the value is updated.

So how can I make sure myIP will be updated when Done is pressed. ?

Thanks

J_yang
  • 2,672
  • 8
  • 32
  • 61
  • `onClick()` will be called when you click the `EditText`. What you're experiencing is the desired behaviour. – Sufian Sep 06 '16 at 11:47

3 Answers3

3

try this

 yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    //do something
                    myIP = theIP.getText().toString(); 
            validIP = ipvalidator.validate(myIP);
                }
                return false;
            }
        });
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45
1

Use this method:

theIP.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Do whatever you want here
        return true;
    }
    return false;
}
Edu Romero
  • 300
  • 1
  • 10
0

you should set onEditorActionListener for the EditText to implement the action you want to perform.

theIP.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            try {
            myIP = theIP.getText().toString(); 
            validIP = ipvalidator.validate(myIP);
        } catch (NullPointerException e)
        {
            Log.d("Error", "Input address is NULL.");
        }

            return true;
        }
        return false;
    }
});