requestFocus的不能正常进行工作的EditText [英] requestFocus doesn't work properly for EditText

查看:618
本文介绍了requestFocus的不能正常进行工作的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个大量的时间花在解决问题,而且看起来很容易,但我真的累了,找不到解决办法。 我有一个活动,活动有4的EditText组件,其中2个有弹出菜单(AlertDialog)包含列表中,下一个 - 是编辑禁用,最后一个 - 是可编辑的,并应表现出软键盘,当用户攻就可以了。

另外,我的根的LinearLayout有LinearLayout中包含内部RelativeLayout的。最后一个是需要AdvBanner。最后的LinearLayout(RelativeLayout的)对准根布局的底部。

XML的部分是这样描述的:

 <的LinearLayout
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =FILL_PARENT
      机器人:layout_gravity =底
      机器人:可聚焦=假
      机器人:focusableInTouchMode =假
      机器人:重力=底和GT;
  < RelativeLayout的
      机器人:ID =@ + ID / AdvLayoutReserveArea
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =50dp
      机器人:后台=#FFFFFF
      机器人:可聚焦=假
      机器人:focusableInTouchMode =假
      机器人:重力=底部/>
  < / LinearLayout中>
 

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

让我们来看看一步一步来。

问题1。 如果在这一刻(当高级加载和显示),用户通过软键​​盘编辑一个EditText领域,焦点将丢失,边界采取的灰色的颜色,如果用户继续要在键入文字是没有结果 - 符号不打印(光标的EditText过丢失)。

我认为,任何用户将被惹恼了 - 当你输入文字,并且光标处于非活动状态,因为在后台的一些高级加载,并采取集中进行自我

要解决这个问题,在方法宣告时加载(如图),我尝试手动后焦距来的EditText通过requestFocus的方法。

 公共无效onAdLoaded()
    {
// TODO自动生成方法存根
//添加应用程序特定的code本次活动在这里...
//当广告成功显示在设备称为
    CountEdit1.requestFocus();
}
 

是的,光标返回到的EditText字段,如果软键盘被激活,用户仍可以输入文字,但现场的EditText边境留灰...

注意:其实我不知道重点的EditText的绿色和灰色边框之间的差异。绿色通常是当用户敲击它,和灰色,也许,当我们要手动请求焦点(使用requestFocus的()等)

第2题。(作为溶剂化问题#结果1)。 后软键盘被关闭,如果在可编辑的EditText字段用户水龙头,它需要集中精力,光标出现在里面,但再次显示软键盘没反应!攻丝它不显示软键盘,但看起来像在编辑栏在复​​印模式 - 当用户可以选择文本和剪切/复制到剪贴板

我的目标是容易让第一次看。我只是想保存光标和重点为可编辑的EditText场(CountEdit1),而软键盘显示和用户输入一些文字。 和正常的反应,当用户敲击的EditText - !为通常情况下,只显示我的软键盘

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

希望寻求帮助,解决问题... 在此先感谢。

解决方案

我们的目标是解决了,解决办法是比我想象的更容易。问题#2被固定用的onClick()方法。充分条件,出现软键盘同时使用clearFocus()和requestFocus的()方法的。

  CountEdit1.setOnClickListener(新OnClickListener(){

      @覆盖
      公共无效的onClick(视图v)
      {
              CountEdit1.clearFocus();
        CountEdit1.requestFocus();
      }
    });
 

当用户敲击在现场的EditText软键盘出现。 固定的。

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.

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.

The part of XML that describes it:

  <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 GREEN border 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.

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 GRAY color, and if user continue to typing a text is have no result - symbols are not printed (CURSOR in EditText is too lost).

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.

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

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)

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.

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!

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.

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

解决方案

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.

   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.

这篇关于requestFocus的不能正常进行工作的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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