how to regain focus on a edit text when expandable list is reloaded…?

天大地大妈咪最大 提交于 2019-12-08 10:44:42

问题


I have a ExpandableListView in which i am loading custom layouts in both group and child views.

They are having some edit text fields.

When i click on the edit text, the soft keyboard appears, that time the list is reloaded, but focus will not be on the edit text.

As a result the input i am typing is not entered in that edit text.

If i clicked on the edit text again i can enter the values.

But I want to regain its focus even after reloading the list view.

I tried with storing the last clicked view and storing it in temporary variable, requestFocus() on reloading, but its not working.

How to achieve this..?

here is a screen shot.

If anybody have an idea please help me..!

Thanks in Advance.


回答1:


Manage the SoftInput Keyboard for your activity.

Take a look at android:windowSoftInputMode




回答2:


I tried with storing the last clicked view and storing it in temporary variable, requestFocus() on reloading, but its not working.

What isn't working? Is your activity being recreated so the temporary variable is being lost? If so, then this answer might help you.

I haven't used an ExpandableListView before, but I've had to solve a similar problem in my code. When an orientation change happens the activity is destroyed and then recreated, so I use onRetainNonConfigurationInstance() to store information that I want to preserve.

Here's some sample code:

  protected StoredState mStoredState;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    final Object data = getLastNonConfigurationInstance();

    if (data == null)
    {
      // First time initialization
      mStoredState = new StoredState();
    }
    else
    {
      // StoredState data exists
      mStoredState = (StoredState)data;

      if (mStoredState.mFieldToGiveFocusTo != -1)
      {
        // Give focus to the field that had focus before the
        // activity was recreated
        View viewToFocus = findViewById(mStoredState.mFieldToGiveFocusTo);
        viewToFocus.requestFocus();
      }
    }
  }

  @Override
  public Object onRetainNonConfigurationInstance()
  {
    return mStoredState;
  }

  /**
   * Class to store the information about this activity when the orientation
   * is changed
   */
  private static class StoredState
  {
    int mFieldToGiveFocusTo;

    private StoredState()
    {
      mFieldToGiveFocusTo = -1;
    }
  }

Then you just need to set mStoredState.mFieldToGiveFocusTo when the user first touches the EditText.




回答3:


I had similar problems with calling requestFocus() inside onCreate() or onResume(). I was able to solve this by setting the focus with a slight delay. Something like this might work for you:

new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                viewToFocus.requestFocus();

            }
        }, 100);

I think the root of the problem might be that the AndroidFramework sets the Focus after onResume() is processed. So that's why a delay might help.




回答4:


I think you will find the answer here: ExpandableListView child items don't get focus when touched

This guy has an issue that sounds the same as yours.




回答5:


After the listView is expanded, you gain force gain focus on the editText by adding this line,

editText.requestFocus();



来源:https://stackoverflow.com/questions/7848037/how-to-regain-focus-on-a-edit-text-when-expandable-list-is-reloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!