Android requestFocus 对 EditText 不能正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11278589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:41:19  来源:igfitidea点击:

requestFocus doesn't work properly for EditText

androidandroid-layoutfocuscursorandroid-edittext

提问by Dimon

A lot of time was spent to solve the problem, and it looks easy, but I'm really tired and couldn't find the solution. I have an Activity, activity has 4 EditText components, 2 of them has popup menu (AlertDialog) which contain the list, next one - is disabled for edit, and last one - is editable, and should show the soft keyboard, when user is tapping on it.

花了很多时间来解决问题,看起来很容易,但我真的很累,找不到解决方案。我有一个活动,活动有 4 个 EditText 组件,其中 2 个有包含列表的弹出菜单 (AlertDialog),下一个 - 禁止编辑,最后一个 - 是可编辑的,并且应该显示软键盘,当用户是点击它。

Also, my root LinearLayout has LinearLayout which contain inside RelativeLayout. The last one is need for AdvBanner. Last LinearLayout(RelativeLayout) is aligned to the bottom of root layout.

此外,我的根 LinearLayout 具有包含在 RelativeLayout 内的 LinearLayout。最后一个是 AdvBanner 的需要。最后一个 LinearLayout(RelativeLayout) 与根布局的底部对齐。

The part of XML that describes it:

描述它的 XML 部分:

  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_gravity="bottom"
      android:focusable="false"
      android:focusableInTouchMode="false" 
      android:gravity="bottom">
  <RelativeLayout 
      android:id="@+id/AdvLayoutReserveArea" 
      android:layout_width="fill_parent" 
      android:layout_height="50dp" 
      android:background="#FFFFFF"
      android:focusable="false"
      android:focusableInTouchMode="false" 
      android:gravity="bottom" /> 
  </LinearLayout>

When activity is start, editable EditText has focus with GREENborder and cursor is blinking. After few seconds left, the AdvBanner is loaded and shown. When it happens, editable EditText lost focus.. from this moment, my life be like a nightmare.

当活动开始时,可​​编辑的 EditText 具有绿色边框和光标闪烁的焦点。几秒钟后,AdvBanner 被加载并显示。当它发生时,可编辑的 EditText 失去了焦点......从这一刻开始,我的生活就像一场噩梦。

Let's look step by step.

让我们一步一步来看。

Problem 1.If in THIS MOMENT(when Adv loaded and appears) user is editing an EditText field via the soft keyboard, focus is lost, border take a GRAYcolor, and if user continue to typing a text is have no result - symbols are not printed (CURSOR in EditText is too lost).

问题1.如果THIS MOMENT(进阶时加载并出现)用户通过软键盘编辑一个EditText领域,焦点将丢失,边境采取了灰色的颜色,如果用户继续输入文字是没有结果-符号未打印(EditText 中的 CURSOR 丢失太多)。

I THINK any user will be annoyed - when you typing text, and cursor is inactive, because in background some adv is loaded and it take focus for self.

我认为任何用户都会生气 - 当您输入文本时,光标处于非活动状态,因为在后台加载了一些广告并且它会为自己集中注意力。

To solve this, in method when Adv is loaded (is shown) I try to back focus manually to EditText by requestFocus method.

为了解决这个问题,在加载 Adv 的方法中(如图所示),我尝试通过 requestFocus 方法将焦点手动返回到 EditText。

public void onAdLoaded() 
    {
// TODO Auto-generated method stub
// add app specific code for this event here...
// called when an ad is successfully displayed on device            
    CountEdit1.requestFocus();
}

Yes, the cursor is returned to EditText field, and if soft keyboard is active, user can still typing text, but border of EditText field stay GRAY...

是的,光标返回到 EditText 字段,如果软键盘处于活动状态,用户仍然可以输入文本,但 EditText 字段的边框保持灰色...

NOTE:actually I'm not sure about the difference between GREEN and GRAY border of focused EditText.. GREEN is usually when user is tapping on it, and GRAY, probably, when we want to request a focus manually (using requestFocus() etc)

注意:实际上我不确定聚焦 EditText 的绿色和灰色边框之间的区别。绿色通常是当用户点击它时,而灰色,可能是当我们想要手动请求焦点时(使用 requestFocus() 等) )

Problem 2.(As result of solvation Problem #1). After soft keyboard was closed, if user tap on editable EditText field, it take focus and cursor appears inside, but no reaction for showing soft keyboard again! Tapping of it do not show soft keyboard, but looks like the edit field in copy mode - when user can select a text and cut/copy it to clipboard.

问题 2.(作为求解问题 #1 的结果)。软键盘关闭后,如果用户点击可编辑的 EditText 字段,它会获得焦点并且光标出现在里面,但再次显示软键盘没有反应!点击它不会显示软键盘,但看起来像复制模式下的编辑字段 - 当用户可以选择文本并将其剪切/复制到剪贴板时。

My goal is easy for a first look. I just want to SAVE the cursor and focus to editable EditText field (CountEdit1) while soft keyboard is shown and user typing some text. And normal reaction when user tapping EditText - as usually, just show me the soft keyboard!

乍一看,我的目标很容易。我只想在显示软键盘并且用户键入一些文本时将光标和焦点保存到可编辑的 EditText 字段 (CountEdit1)。用户点击 EditText 时的正常反应 - 通常,只需向我展示软键盘即可!

I read all issues here, I combined different methods (clearFocus, requestFocusFromTouch etc), just not enough of time and space to describe all that I tried to do to solve this. The main problems are described above.

我在这里阅读了所有问题,我结合了不同的方法(clearFocus、requestFocusFromTouch 等),只是没有足够的时间和空间来描述我试图做的所有事情来解决这个问题。主要问题如上所述。

Hope for help and solving the problem... Thanks in advance..

希望得到帮助和解决问题......提前致谢..

回答by Dimon

The goal is solved, the workaround is an easier than I thought. Problem #2 is fixed by using onClick () method. Sufficient condition for appearing of soft keyboard that use of both clearFocus() and requestFocus() methods.

目标已解决,解决方法比我想象的要容易。问题#2 是通过使用 onClick () 方法修复的。出现同时使用 clearFocus() 和 requestFocus() 方法的软键盘的充分条件。

   CountEdit1.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v)
      {
              CountEdit1.clearFocus();
        CountEdit1.requestFocus();
      }
    });
   CountEdit1.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v)
      {
              CountEdit1.clearFocus();
        CountEdit1.requestFocus();
      }
    });

The soft keyboard appears when user is tapping on the EditText field. Fixed.

当用户点击 EditText 字段时会出现软键盘。固定的。

回答by iBog

Works for me:

对我有用:

InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
editField.requestFocus();
mgr.showSoftInput(editField, InputMethodManager.SHOW_IMPLICIT);

回答by Swayam

userInput.post(new Runnable() {
            public void run() {
                userIdInput.requestFocus();

            }
        });

Have a go at this and tell if your problem is still unsolved.

试试这个,看看你的问题是否仍然没有解决。

回答by zYberIs

You should request focus after view is created in fragment or activity:

在片段或活动中创建视图后,您应该请求焦点:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    CountEdit1.requestFocus();
}

回答by Irshu

try this:

尝试这个:

public void onAdLoaded() 
    {  
  final Handler handler = new Handler();
             handler.postDelayed(new Runnable() {
                 @Override
                 public void run() {
                     CountEdit1.requestFocus();
                     InputMethodManager mgr = (InputMethodManager) base.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                     mgr.showSoftInput(CountEdit1, InputMethodManager.SHOW_IMPLICIT);
                     CountEdit1.setSelection(CountEdit1.getText().length());
                 }
             },0);
}