重新加载可扩展列表时如何重新获得编辑文本的焦点...?

发布于 2024-12-11 17:07:17 字数 425 浏览 0 评论 0原文

我有一个 ExpandableListView,其中我在组视图和子视图中加载自定义布局。

他们有一些编辑文本字段。

当我单击编辑文本时,会出现软键盘,此时会重新加载列表,但焦点不会位于编辑文本上。

因此,我正在键入的输入未输入到该编辑文本中。

如果我再次单击编辑文本,我可以输入值。

但即使在重新加载列表视图后我也想重新获得焦点。

我尝试在重新加载时存储最后单击的视图并将其存储在临时变量 requestFocus() 中,但它不起作用。

如何实现这个..?

这是一个屏幕截图。

image

如果有人有想法请帮助我..!

提前致谢。

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.

image

If anybody have an idea please help me..!

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌能量女王 2024-12-18 17:07:17

我尝试存储最后单击的视图并将其存储在临时中
变量,requestFocus() 重新加载,但它不起作用。

什么不起作用?您的活动是否正在重新创建,因此临时变量正在丢失?如果是这样,那么这个答案可能对您有帮助。

我以前没有使用过 ExpandableListView,但我必须在代码中解决类似的问题。当方向发生变化时,活动将被销毁,然后重新创建,因此我使用 onRetainNonConfigurationInstance() 来存储我想要保留的信息。

下面是一些示例代码:

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

然后,您只需在用户第一次触摸 EditText 时设置 mStoredState.mFieldToGiveFocusTo 即可。

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.

完美的未来在梦里 2024-12-18 17:07:17

我在 onCreate()onResume() 内调用 requestFocus() 时遇到了类似的问题。我通过稍微延迟设置焦点来解决这个问题。像这样的东西可能对你有用:

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

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

            }
        }, 100);

我认为问题的根源可能是 AndroidFramework 在处理 onResume() 后设置焦点。这就是为什么延迟可能会有所帮助。

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.

带刺的爱情 2024-12-18 17:07:17

管理您的活动的 SoftInput 键盘。

看看 android:windowSoftInputMode

Manage the SoftInput Keyboard for your activity.

Take a look at android:windowSoftInputMode

泛滥成性 2024-12-18 17:07:17

我想你会在这里找到答案:
ExpandableListView 子项在触摸时不会获得焦点

这个人有一个听起来与您的问题相同的问题。

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.

梨涡少年 2024-12-18 17:07:17

展开 listView 后,您可以通过添加此行

editText.requestFocus();来强制获得对 editText 的焦点。

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

editText.requestFocus();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文