同样的片段,EditText上和requestFocus的问题 [英] Same fragments, edittext and requestfocus issue

查看:293
本文介绍了同样的片段,EditText上和requestFocus的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,问在这个问题上另一次帮助,但所有其他职位没有帮助。

Sorry for asking another time help on this matter, but all others posts didn't help.

下面的情况:我有一个Acivity('A'),其采用了分屏片段中。该片段被交换用户输入。其中一个这种片段具有的EditText里面,我想专注于创造和表现出该死的软键盘。因此,在片段的onCreateView()我使用:

Here's the scenario: I have a Acivity ('A') that incorporates a Layout with a fragment inside. This fragment is swapped on user input. One of this fragments has a edittext inside, which I want to get focus on creation AND show the damn soft keyboard. So, in the onCreateView() of the fragment I use:



                mEt = (EditText) v.findViewById(R.id.et);
                mEt.setImeOptions(EditorInfo.IME_ACTION_DONE);
                mEt.requestFocus();

所以,它的工作原理是第一次,但如果片段被替换,后来重新创建的,它得到了焦点,但键盘没有出现。

So, it works the first time, but if the fragment is replaced and re-created later, it gets the focus but the keyboard does not appear.

我试图隐藏键盘之前,该片段是通过破坏:

I tried to hide keyboard before the fragment is destroyed via:



        InputMethodManager keyboard = (InputMethodManager)
        ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromWindow(et.getWindowToken(), 0);

或明确显示通过键盘:

or to explicit show the keyboard via:



            InputMethodManager keyboard = (InputMethodManager)
                ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.showSoftInput(et, 0);

但(你可以通过我张贴在这里:)其实想象),问题的风格。

but (as you can imagine by the fact I'm posting here :) ), the problem stay.

我也desperatly想过一个活动/片段问题,使用上的活动监听器相同的技术,没有运气。

I also desperatly thought about a activity/fragment problem and used same techniques with listeners on the activity, without luck.

很沮丧,请大家帮忙:)

Quite frustrated, please help :)

推荐答案

我刚刚解决了这个问题。我们有这样的换出的多个片段,在需要重点和键盘文本字段的活动。​​

I just solved this problem. We had an activity that swapped out multiple Fragments with text fields that needed focus and the keyboard.

有两种方法可以解决这个问题,这两个我打了。这是我最后用去的方法。

There are two ways to solve this, both of which I played with. This is the method I finally went with.

@Override
private View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ...
    if (savedInstanceState != null && !savedInstanceState.isEmpty()){
      msDialogMessage = savedInstanceState.getString(STATE_DAILOG_MSG);
    } else{
      Utils.setKeyboardFocus(mEditTextUserName);
    }
    ...
}

 /**
  * Used to set focus and show keyboard (if needed) for a specified text field
  * @author Ty Smith
  * @param primaryTextField
  */
 public static void setKeyboardFocus(final EditText primaryTextField) {
   (new Handler()).postDelayed(new Runnable() {
     public void run() {
       primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
       primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
     }
   }, 100);
 }

尽管如果你的碎片不玩好和生命周期方法不正确打电话,你可能会认为我的另一种方式。

Although if your fragments don't play nice and the lifecycle methods aren't calling right, you might consider my other way.

我不会发表code,但只是把抢对焦方式在一个定制的监听器,并从活动时,你把片段前调用它。

I won't post code, but just put the grab focus method in a custom listener and call it from the activity when you put the fragment to the front.

这篇关于同样的片段,EditText上和requestFocus的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆