Edit text focus control

Put your problem here if it does not fit any of the other categories.

Edit text focus control

Postby timcs » Sun Feb 10, 2013 11:50 pm

Hi

I am having problems controlling the focus of an edit text once the focus has been losted. My goal is to check if the edit text is blank after the focus is lost and then set the focus back to make the user enter a valid input. I am using the OnFocusChangeListener method to check for the loss of focus but no matter what I try to set the focus back, the edit text looks focused but no input happens instead the input stays in the edit text box that was moved to.

Code :

Code: Select all
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (Ehour.getText().length()<=0) {
                    Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
                    Calendar nohour = Calendar.getInstance();
                    Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
Ehour.setFocusable(true);
Ehour.requestFocus();


                }
            }
        }});


Snce this attempt I have tried the following

Attempt 1 :

Code: Select all
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (Ehour.getText().length()<=0) {
                    Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
                    Calendar nohour = Calendar.getInstance();
                    Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));

                    ((EditText)arg0).requestFocus();

                }
            }
        }});


Attempt 2 :

Change to the XML layout first :
Code: Select all
android:id="@+id/enterdata"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"


and addtions to the code:
Code: Select all
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (((EditText)arg0).getText().length()<=0) {
                    Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
                    Calendar nohour = Calendar.getInstance();
                    ((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
                    findViewById(R.id.enterdata).requestFocus();
                    ((EditText)arg0).setFocusable(true);
                    ((EditText)arg0).setFocusableInTouchMode(true);
                    ((EditText)arg0).requestFocus();

                }
            }
        }});


and finally I tried this Attempt:

Code: Select all
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (((EditText)arg0).getText().length()<=0) {

                    Calendar nohour = Calendar.getInstance();
                    ((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
                    if (((EditText)arg0).requestFocus()) {
                         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                         Emin.clearFocus();   
                         Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show(); 
                    }


                }
            }
        }});



Any Ideas on this ?

Thanks

Tim
timcs
Freshman
Freshman
 
Posts: 2
Joined: Thu Jan 12, 2012 8:33 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests