7

I read around about the questions on how to set an object to be focused however I cannot seem to find either an answer to what I am trying to do.

Using the On Focus Listener I have done the following:

  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();


                }
            }
        }});

I have tried the suggestions from the follow posts:

I read this post but using these suggestions as well do not seem to work: How to set focus on a view when a layout is created and displayed?

My goal is that when the edit text focus is lost, I need to move back to it if the entry given is invalid.

Furthermore as per the suggestion given by Bill Mote I have also tried:

 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();

                }
            }
        }});

** EDIT ** I even added the below line before the requestFocus() :

((EditText)arg0).setFocusable(true);

*** EDIT ****

Tried from this post : Stop EditText from gaining focus at Activity startup

The following changes : In the layout at the first layout header I added :

android:id="@+id/enterdata"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

Then in the OnChangeFocusListener I did this:

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();

                }
            }
        }});

However I still get the same behaviour as before, the edit text that the focus goes to stays that way but the edit text that I want the focus to go to looks focused but it cannot seem to be used as though it is still waiting to be focused.

*** EDIT ****

Code below as per recent post found on stackoverflow where the user who asked the question is experiencing the same problem as me:

 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();  
                    }


                }
            }
        }});
Community
  • 1
  • 1
TimCS
  • 363
  • 1
  • 7
  • 17
  • Looking at this post : http://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup I tried on of the answers which I will add to my question – TimCS Feb 10 '13 at 09:10
  • I have even tried to hard code all of the edit text boxes to clear focus and set the setFocusable and setFocusableInTouchMode to false but the behaviour is still the same it looks like the edit text I want the focus to is focused but any typing ends up in the edit text that the user would move to !!!!! HELP!!!! – TimCS Feb 10 '13 at 10:36
  • It appears in this post :http://stackoverflow.com/questions/14327412/set-focus-on-edittext?rq=1 the user had the same problem and it did not look like it was solved , however I am going to try the suggestions here to see if they work for me – TimCS Feb 10 '13 at 15:27
  • I have tried the suggestion given in the above post and this has made no difference to the situation, the code will be added to the question to show what I have tried – TimCS Feb 10 '13 at 16:09

3 Answers3

3

Try arg0.requestFocus().

Not sure if View implements requestFocus() off the top of my head so you might have to cast it ... ((EditText) arg0).requestFocus().

Joop
  • 3,706
  • 34
  • 55
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
  • Sorry should have said in my question, tried as you suggested with the same results as mentioned – TimCS Feb 09 '13 at 13:32
  • 1
    The behaviour of the two edit texts appears odd to me in that the one I want to move back to looks focused and appears to have a cursor against it but the one that the focus has moved to also appears to be the same , even if I "click" back into the one I want to use the edit text that it changes is not this one – TimCS Feb 09 '13 at 20:18
  • It looks to me that there seems to be now way around this. Therefore I will have to try and do validation checks at the point of accepting the record instead of checking for it at the time the user moves away from the field. This is on Android 2.2 so I am unsure if it extends to 4 – TimCS Feb 12 '13 at 12:02
1

I had this same issue, here the code that I used to solve:

EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

Best Regards,

Deivison Sporteman
  • 2,334
  • 1
  • 19
  • 22
  • 1
    here's how to do it in Kotlin from a fragment ```editText.requestFocus() activity?.let { val imm = it.getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager imm?.let { manager -> manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT) } } ``` – Anna Harrison Jan 01 '20 at 19:01
0
new OnEditorActionListener(){
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      editText.requestFocus();
      //used ******* return true ******
      return **true**;
   }
}