how to force focus to edit text

2019-02-14 02:29发布

问题:

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


                }
            }
        }});

回答1:

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



回答2:

new OnEditorActionListener(){
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      editText.requestFocus();
      //used ******* return true ******
      return **true**;
   }
} 


回答3:

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,