views:

320

answers:

1

Currently I have an edittext field that when the user presses enter it does mostly what I want it to, validate an IP Address format and inform the user if it is wrong. How do I make it so when the user presses enter it checks it like it is supposed to be does NOT enter the newline character?

Here is my code for it.

    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
        if(validateIPaddress(m_etIPAddress.getText().toString()))
        {
            ConfigData.m_IPAddress = m_etIPAddress.getText().toString();
        }
        else
        {
            showAlertDialog("Invalid IP Address\n Example: \n255.255.255.255\n0.0.0.0","Error: ");
            m_etIPAddress.setText(ConfigData.m_IPAddress);
            m_etIPAddress.requestFocus();
        }   
        return false;
    }

Another problem I have is that in the false condition of the validation, that it will not bring up the soft keyboard to allow the user to reedit that text field. If the user clicks on another edit text the window gives it focus, and allows the user to edit the second text field while still maintaining the 'green outline' around the original edittext. Is there a way to fix this?

EDIT:

Thanks for the response. The EditText still creates a newline. I tried calling that when I create the EditText and it shows the dialog then inserts a newline character at the beginning.. which is weird because the

m_etIPAddress.setText(ConfigData.m_IPAddress); 

should automatically overwrite anything in that field to the static IP saved within ConfigData. (my settings class) and I think the focus might work, the problem is that after requestFocus, that EditText shows it has focus but is unresponsive.

I can click on other EditText's and modify them, while it still shows the focus outline on the IP EditText. If I click on the IP EditText it doesn't actually do anything. Its kind of strange.

A: 

I think for your EditText creating a new line, you can do that by replacing the enter button by a done button like that :

yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);

And to go with that, you can put this to your xml file describing your EditText :

android:maxLines="1"
Sephy
And if focus does not come, you can try .dispatchTouchEvent on the EditText, maybe this can simulate a click on the EditText, but requestFous should work...
Sephy
I've tried adding both, and it doesn't allow the edittext to grow which is great, however it still adds the newline character, which causes it to make the IP Address invalid.If I remove the set max lines, the edittext still grows and still adds a newline character.
TxAg
So it looks like it might just be the phone I am using to test. Because the Android Emulator changes the button from a return to done, however when I test it on the HTC Incredible it does not.
TxAg
If you use the INPUTTYPE for Phone it does not allow newline characters, and allows the '.' in the IP Address
TxAg
You need to use and inputtype other than the one containing "multiline" to describe the type of data I think. OR you can replace maxLines by singleLine, though I think it's deprecated
Sephy