简体   繁体   中英

How can I prevent focus from moving to the next field if a given EditText is empty?

I add five EditText controls dynamically, and I want to check that they are filled in before moving to the next step. Here's what I have so far:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_form)

    EditText nameedit = new EditText(this);
    nameedit.setHint("First name");
    blur.addView(nameedit);
    nameedit.setWidth(32);
    nameedit.setEms(50);
    MarginLayoutParams params3 = (MarginLayoutParams) nameedit.getLayoutParams();
    params3.leftMargin = 16; params3.topMargin = 125;
    nameedit.setLayoutParams(params3);
    Typeface font33=Typeface.SERIF;
    nameedit.setTypeface(font33);
    nameedit.setMaxLines(1);
    nameedit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
    nameedit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

Going to "Next step" might be done through a button click right? If so, just, use

nameedit.getText().toString().length() to get the length of input. Check if its zero or not. Do the same stuff for other 4 EditText .

Based on the validation proceed to next step.

Change your Reg_btn.setOnClickListener(this); to following:

Reg_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
                            if(nameedit.getText().toString().length()>0){// also use && to validate other 4 edittext views
                                   //do your stuff here
                            }

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM