android 怎样edittext 键盘失去焦点时自动关闭

android 怎样edittext 键盘失去焦点时自动关闭,第1张

 

android 怎样edittext 键盘失去焦点时自动关闭

软键盘的原理

软键盘其实是一个Dialog。InputMethodService为我们的输入法创建了一个Dialog,并且对某些参数进行了设置,使之能够在底部或者全屏显示。当我们点击输入框时,系统会对当前的主窗口进行调整,以便留出相应的空间来显示该Dialog在底部,或者全屏。

2活动主窗口调整

Android定义了一个属性windowSoftInputMode,

用它可以让程序控制活动主窗口调整的方式。我们可以在配置文件AndroidManifetxml中对Activity进行设置。这个属性的设置将会影响两件事情:

1>软键盘的状态——隐藏或显示。

2>活动的主窗口调整——是否减少活动主窗口大小以便腾出空间放软键盘或是否当活动窗口的部分被软键盘覆盖时它的内容的当前焦点是可见的。

故该属性的设置必须是下面列表中的一个值,或一个“state…”值加一个“adjust…”值的组合。在任一组设置多个值,各个值之间用|分开。

"stateUnspecified":The state of the soft keyboard (whether it is hidden or

visible) is not specified The system will choose an appropriate state or rely

on the setting in the theme This is the default setting for the behavior of the

soft keyboard 软键盘的状态(隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设置。这个是软件盘行为的默认设置。

"stateUnchanged":The soft keyboard is kept in whatever state it was last

in, whether visible or hidden, when the activity comes to the fore

软键盘被保持上次的状态。

"stateHidden":The soft keyboard is hidden when the user chooses the

activity — that is, when the user affirmatively navigates forward to the

activity, rather than backs into it because of leaving another activity

当用户选择该Activity时,软键盘被隐藏。

"stateAlwaysHidden":The soft keyboard is always hidden when the activity's

main window has input focus 软键盘总是被隐藏的。

"stateVisible":The soft keyboard is visible when that's normally

appropriate (when the user is navigating forward to the activity's main window)

软键盘是可见的。

"stateAlwaysVisible":The soft keyboard is made visible when the user

chooses the activity — that is, when the user affirmatively navigates forward to

the activity, rather than backs into it because of leaving another activity

当用户选择这个Activity时,软键盘是可见的。

"adjustUnspecified":It is unspecified whether the activity's main window

resizes to make room for the soft keyboard, or whether the contents of the

window pan to make the currentfocus visible on-screen The system will

automatically select one of these modes depending on whether the content of the

window has any layout views that can scroll their contents If there is such a

view, the window will be resized, on the assumption that scrolling can make all

of the window's contents visible within a smaller area This is the default

setting for the behavior of the main window

它不被指定是否该Activity主窗口调整大小以便留出软键盘的空间,或是否窗口上的内容得到屏幕上当前的焦点是可见的。系统将自动选择这些模式中一种主要依赖于是否窗口的内容有任何布局视图能够滚动他们的内容。如果有这样的一个视图,这个窗口将调整大小,这样的假设可以使滚动窗口的内容在一个较小的区域中可见的。这个是主窗口默认的行为设置。也就是说,系统自动决定是采用平移模式还是压缩模式,决定因素在于内容是否可以滚动。

"adjustResize":(压缩模式)The activity's main window is always resized to make

room for the soft keyboard on screen 当软键盘d出时,要对主窗口调整屏幕的大小以便留出软键盘的空间。

"adjustPan":(平移模式:当输入框不会被遮挡时,该模式没有对布局进行调整,然而当输入框将要被遮挡时,窗口就会进行平移。也就是说,该模式始终是保持输入框为可见。)The

activity's main window is not resized to make room for the soft keyboard

Rather, the contents of the window are automatically panned so that the current

focus is never obscured by the keyboard and users can always see what they are

typing This is generally less desirable than resizing, because the user may

need to close the soft keyboard to get at and interact with obscured parts of

the window

该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互 *** 作。。

3侦听软键盘的显示隐藏

有时候,借助系统本身的机制来实现主窗口的调整并非我们想要的结果,我们可能希望在软键盘显示隐藏的时候,手动的对布局进行修改,以便使软键盘d出时更加美观。这时就需要对软键盘的显示隐藏进行侦听。

我们可以借助软键盘显示和隐藏时,对主窗口进行了重新布局这个特性来进行侦听。如果我们设置的模式为压缩模式,那么我们可以对布局的onSizeChanged函数进行跟踪,如果为平移模式,那么该函数可能不会被调用。

4EditText默认不d出软件键盘

方法一:

在AndroidMainfestxml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden

例如:

android:label="@string/app_name"

android:windowSoftInputMode="adjustUnspecified|stateHidden"

android:configChanges="orientation|keyboardHidden">

方法二:

让EditText失去焦点,使用EditText的clearFocus方法

例如:EditText edit=(EditText)findViewById(Ridedit);

editclearFocus();

方法三:

强制隐藏Android输入法窗口

例如:EditText edit=(EditText)findViewById(Ridedit);

InputMethodManager imm =

(InputMethodManager)getSystemService(ContextINPUT_METHOD_SERVICE);

immhideSoftInputFromWindow(editgetWindowToken(),0);

5EditText始终不d出软件键盘

例:EditText edit=(EditText)findViewById(Ridedit);

editsetInputType(InputTypeTYPE_NULL);

//如果对edittext组件设置了editTextsetFocusable(false);

需要重新获取焦点则必须执行:editTextsetFocusable(ture);

editTextsetFocusableInTouchMode(true);

editTextrequestFocus();

在网上看了些例子都不够全面,在这里全面总结下。

一:EditText为什么会默认d出输入法

同样的代码,碰到有EditText控件的界面时有的机子会d出输入法,有的机子不会d出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研究android源码时再搞个清楚。但是我有解决方案。

二:默认d出和默认关闭输入法的解决方案。

1默认关闭,不至于进入Activity就打开输入法,影响界面美观。

①在布局文件中,在EditText前面放置一个看不到的LinearLayout,让他率先获取焦点:

<LinearLayout

android:focusable="true"

android:focusableInTouchMode="true"

android:layout_width="0px"

android:layout_height="0px"/>

②方法二:先看一个属性android:inputType:指定输入法的类型,int类型,可以用|选择多个。取值可以参考:androidtextInputType类。取值包括:text,textUri,

phone,number,等

Android SDK中有这么一句话“If

the given content type is TYPE_NULL

then a soft keyboard will not be displayed for this text view”,

先将EditText的InputType改变为TYPE_NULL,输入法就不会d出然后再设置监听,再重新设置它的InputType

editTextsetOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

int inType = editTextgetInputType(); // backup the input type

editTextsetInputType(InputTypeTYPE_NULL); // disable soft input

editTextonTouchEvent(event); // call native handler

editTextsetInputType(inType); // restore input type

return true;

}

});

2默认d出。有时候按照需求可能要求默认d出输入法。方案如下:

EditText titleInput = (EditText) findViewById(Ridcreate_edit_title);titleInputsetFocusable(true);

 titleInputrequestFocus();

onFocusChange(titleInputisFocused());

private void onFocusChange(boolean hasFocus)

{

final boolean isFocus = hasFocus;

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

public void run() {

InputMethodManager imm = (InputMethodManager)

titleInputgetContext()getSystemService(ContextINPUT_METHOD_SERVICE);

if(isFocus)

{

immtoggleSoftInput(0, InputMethodManagerHIDE_NOT_ALWAYS);

}

else

{

immhideSoftInputFromWindow(titleInputgetWindowToken(),0);

}

}

}, 100);

}

转载

简单一点理解,在移动应用中,焦点就是当前正在处理事件的位置。在手机应用中,最有可能用到焦点的就是EditText,如果同一个界面中有多个EditText,通常情况下同一时间只有一个能够输入内容,此时,这个EditText就获取了焦点。

在Android中,对焦点的设置分为两种情况,TouchMode和非TouchMode。现在的手机基本都是触摸屏,我们用手指触摸屏幕来 *** 作Android应用时,处于TouchMode。除了TouchMode之外,还有非TouchMode,利用外接设备来 *** 作应用。比如键盘。使用Genymotion模拟器的时候,一个界面上有多个控件时,可以用电脑tab键来进行移动,被选中的控件会高亮显示,这时候就是非TouchMode,被选中的控件获得了焦点。

在手机应用中,用到焦点的时候并不多,但是TV应用中,需要用遥控器来 *** 作选中控件,这时候就需要对焦点进行处理了。关于焦点,常用方法如下:

在View类中, isFocusable() 和 isFocusableInTouchMode() 获取到的结果都是false,也就是说,直接继承自View的控件是不能获取焦点的。我们常用控件中对这两个方法进行了改写,比如EditText,这两个方法都是true,而Button则只有 isFocusable() 返回true。这也就是为什么我们用tab键选取Button的时候能够高亮显示,而鼠标点击(模拟触控)的时候不能高亮显示的原因了。如果想在点击的时候也能高亮显示Button,需要手动设置 setFocusableInTouchMode(true) ,就可以了。

如果想对控件的焦点状态进行监听,需要设置 setOnFocusChangeListener() ,只要控件的焦点状态发生变化(获得或者失去焦点),都会调用 onFocusChange 方法

关于焦点的移动,默认的算法会寻找指定方向上最近的可以获取焦点的元素(非TouchMode)。另外在创建控件的时候,也可以指定寻找焦点的方向,设置nextFocusDown、nextFocusLeft、nextFocusRight 和 nextFocusUp的值为指定元素就可以了。看以下例子:

这里指定了上面的button向上寻找焦点时,下一个元素是id为bottom的元素,也就是说,上面的Button在获取了焦点之后,继续按向上键,系统会将焦点移动到id为bottom的元素上,而不是继续向上。

在开发手机应用的过程中,对焦点的处理并不多,它与事件是两个不同的体系,通常情况下焦点和事件是相互独立并不冲突。但是在Button的点击事件中会有一点问题。如果我们队一个button设置了 setFocusableInTouchMode(true) ,使他可以获取焦点,那么我们点击这个button的时候,第一次点击并不会执行 onClick() 方法,而是执行 onFocusChange() 。第二次点击的时候才会执行 onClick() 方法。看起来好像 onFocusChange() 消耗了点击事件,实际上并不是的。

这个问题我们看一下源码就清楚了:

onClick() 方法是在onTouchEvent的ACTION_UP里调用的,看一下View的onTouchEvent方法:

可以看到,只有当focusTaken为false的时候才会执行onClick,focusTaken的值默认是false的,但是在 isFocusable() && isFocusableInTouchMode() && !isFocused() 为true的时候,会去 requestFocus 获取焦点,并将值赋给focusTaken。

关键在于 isFocused() ,如果当前Button没有获取焦点, isFocused() 返回false, !isFocused() 值为ture,Button就会去获取焦点,从而导致 focusTaken 为true, onClick 方法就不会执行了,只有Button已经获取了焦点的时候才会执行onClick方法。

以上就是关于android 怎样edittext 键盘失去焦点时自动关闭全部的内容,包括:android 怎样edittext 键盘失去焦点时自动关闭、如何让Android下的多行edittext焦点光标默认在第一行而不是中间、Android中EditTex焦点设置和d不d出输入法的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9354660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存