Edit text first time to input a letter validation

2019-06-14 20:51发布

问题:

I'm creating an application that checks if the user inputs a letter or a number then a button will pop-out. And if the user didn't enter a single letter a validation will show.

Here's an example of what I'm trying to debug[main activity. PS. not my real code]:

private EditText edittext;

    Button checkBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addKeyListener();
    }

    public void addKeyListener() {
        edittext = (EditText) findViewById(R.id.editText);
        checkBtn = (Button) findViewById(R.id.button1);

        checkBtn.setVisibility(View.INVISIBLE);


        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {


                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    checkBtn.setVisibility(View.VISIBLE);

                    return true;

                else if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_9)) {

                // display a floating message
                Toast.makeText(MyAndroidAppActivity.this,
                        "Number 9 is pressed!", Toast.LENGTH_LONG).show();
                return true;
            }

            return false;
}
        });

    }
}

Question: Is there a way to make the button show automatically when a user types a letter without clicking the enter key. And if the user didn't input a letter a validation will pop out saying invalid and the button will not display.

I tried looking for the right answer but still I couldn't find it.

回答1:

You, my friend, need a

EditText editText = (EditText)findViewById(R.id.edittext);
editText .addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            ; //Do nothing
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            ; //Do nothing
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //TODO put your code here.
        } 

    });

Now, just check to see if that "s" Char Sequence contains your letter and you'll be on your way.


Edit: Once you have that CharSequence, just check to see that the first char is a letter.

To get the first char, do

s.charAt(0);  //Note: if there is nothing here, it could throw a null pointer.

The Character class has a static function that you can use to see if it is a letter (or a number, capital, lowercase, digit, digit or letter, etc) called

Character.isLetter(char); 

so you could do something like

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //Check to see that there is at least 1 char to look at.
            //Then check to see if it is a letter.
            //(Note: the && is AND, so both things have to be true)
            if (s.length() > 0 && Character.isLetter(s.charAt(0))) {
                ; //Display your button
            } else { 
                ; //Display your error
            }
        }

Does that help?


You can create a new class called MyTextWatcher (either in it's own file where you can import it anywhere or as a class within a class (if all of the edit texts are in the same area)), and then inside there do the regular stuff you are now expert at:

public class MyTextWatcher implements TextWatcher {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    @Override
    public void afterTextChanged(Editable s) {  
    }
}

I personally prefer doing it inside the class that contains the edittext, but it can go in a separate file (that you import) and then it's more reusable. And if for any reason, you need to access things with your TextWatcher, you can pass them in as a constructor. The advantage of doing things inside the class is that you don't have to pass things into a constructor. It's just less things for you to do. But here is an example of that constructor...

public class MyTextWatcher implements TextWatcher {
    private Thing1 mThing1;
    private Thing2 mThing2;
    public MyTextWatcher(Thing1 t1, Thing2 t2) {
        mThing1 = t1;
        mThing2 = t2;
    }
    ...
}

Note: Just be careful not to interlink things too much when doing that. It can get hairy later on. :)



回答2:

Try this I am not sure this is the exact solution you are expecting.

        button = (Button) findViewById(R.id.button1);
    button.setVisibility(View.GONE);

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if(editText.getText().length() > 0 && !s.toString().equals("\n")){
                button.setVisibility(View.VISIBLE);
            }
            else{
                button.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this, "Invalid", Toast.LENGTH_SHORT).show();
            }

       }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });