android失去焦点
⑴ android 怎样edittext 键盘失去焦点时自动关闭
android 怎样edittext 键盘失去焦点时自动关闭
软键盘的原理
软键盘其实是一个Dialog。InputMethodService为我们的输入法创建了一个Dialog,并且对某些参数进行了设置,使之能够在底部或者全屏显示。当我们点击输入框时,系统会对当前的主窗口进行调整,以便留出相应的空间来显示该Dialog在底部,或者全屏。
2.活动主窗口调整
Android定义了一个属性windowSoftInputMode,
用它可以让程序控制活动主窗口调整的方式。我们可以在配置文件AndroidManifet.xml中对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. 当软键盘弹出时,要对主窗口调整屏幕的大小以便留出软键盘的空间。
"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.侦听软键盘的显示隐藏
有时候,借助系统本身的机制来实现主窗口的调整并非我们想要的结果,我们可能希望在软键盘显示隐藏的时候,手动的对布局进行修改,以便使软键盘弹出时更加美观。这时就需要对软键盘的显示隐藏进行侦听。
我们可以借助软键盘显示和隐藏时,对主窗口进行了重新布局这个特性来进行侦听。如果我们设置的模式为压缩模式,那么我们可以对布局的onSizeChanged函数进行跟踪,如果为平移模式,那么该函数可能不会被调用。
4.EditText默认不弹出软件键盘
方法一:
在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden
例如:
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:configChanges="orientation|keyboardHidden">
方法二:
让EditText失去焦点,使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
5.EditText始终不弹出软件键盘
例:EditText edit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
⑵ android 怎么让edittext失去焦点
1、在EditText的父布局中的布局文件中设置这两行代码
android:focusable="true"
android:focusableInTouchMode="true"
2、在代码中设置EditText焦点通过setFocusable()和setFocusableInTouchMode() 不过这个好像不怎么管用
3、如果你是不是想跳转到有EditText的界面时自动弹出软键盘的话 可以在功能清单文件(AndroidManifest.xml) 中的相对应activity节点中设置软键盘弹出模式
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
⑶ android代码如何 让 edittext失去焦点
EditText初始化时候失去焦点:只需要在布局文件中设置属性
java">android:focusable="false"
也可以在代码中由开发者根据需求设置一定的条件,当条件满足后,动态的设置EditText失去焦点。
示例代码:
EditTextet=(EditText)findViewById(R.id.et);
et.clearFocus();
et.setFocusable(false);
这种控制EditText的操作是Android程序中不经常用到的。多出现于EditText的输入监听回调方法中。
⑷ 怎么让android 页面失去焦点
在网上找了好久,有点监听软键盘事件,有点调用 clearFouse()方法,但是测试了都没有!xml中也找不到相应的属性可以关闭这个默认行为
1 解决之道:在EditText的父级控件中找一个,设置成
Android:focusable="true"
android:focusableInTouchMode="true"
这样,就把EditText默认的行为截断了!
<LinearLayout
style="@style/FillWrapWidgetStyle"
android:orientation="vertical"
android:background="@color/black"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:id="@+id/logo"
style="@style/WrapContentWidgetStyle"
android:background="@drawable/dream_dictionary_logo"
/>
<RelativeLayout
style="@style/FillWrapWidgetStyle"
android:background="@drawable/searchbar_bg"
android:gravity="center_vertical"
>
<EditText
android:id="@+id/searchEditText"
style="@style/WrapContentWidgetStyle"
android:background="@null"
android:hint="Search"
android:layout_marginLeft="40dp"
android:singleLine="true"
/>
</RelativeLayout>
</LinearLayout>
2 还有一个方法也可以非常简单的实现这个功能:
EditText对象的clearFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);(关闭软键盘。。。)
3更多问题解决办法请参考android学习手册,例子、源码、文档全部搞定,采用androidstudo的目录结构,360手机助手中下载。下面是截图。
⑸ android 输入框失去焦点,怎样让键盘不自
1. 在activity layout xml文件中,EditText的前面定义:
<LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px" />
2. 在EditText中添加nextFocusUp与nextFocusLeft
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:nextFocusUp="@id/editText" android:nextFocusLeft="@id/editText" />
⑹ Android中EditText获得焦点后马上又失去了,可能是什么造成的呢
有以下几种情况:
EditText处在一个可以滚动的控件中,例如ListView等,当得到焦点后软件键盘会弹出,这个时候滚动控件会重绘,因此会失去焦点。
EditText注册了焦点事件,得到焦点后将焦点转向了其它控件。
EditText禁止了获取焦点。
⑺ Android小知识点: 音频焦点AudioFocus使用
背景: 之前云阅读570解决了 ,有声书播放时候,短信中断的问题。 前几天采薇也有个类似的问题, 播放音频中,收到干扰时候的处理。(在后台播放音频的时候,这个时候另外的软件也播放音频、短信铃声 怎么处理)
解决: 实现AudioManager.OnAudioFocusChangeListener接口实现onAudioFocusChange(int focusChange)方法
AUDIOFOCUS_GAIN:你已经获得音频焦点;
AUDIOFOCUS_LOSS:你已经失去音频焦点很长时间了,必须终止所有的音频播放。因为长时间的失去焦点后,不应该在期望有焦点返回,这是一个尽可能清除不用资源的好位置。例如,应该在此时释放MediaPlayer对象;
AUDIOFOCUS_LOSS_TRANSIENT:这说明你临时失去了音频焦点,但是在不久就会再返回来。 此时,你必须终止所有的音频播放,但是保留你的播放资源,因为可能不久就会返回来。
AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:这说明你已经临时失去了音频焦点,但允许你安静的播放音频(低音量) ,而不是完全的终止音频播放。
主要就是 对于3 与 4的处理。
⑻ android开发 如何让Activity或者布局失去焦点,像弹出Dialog一样。
你可以在整个布局文件中嵌套一个空的布局,让这个空布局去获取焦点,这样你另一个布局中的控件就不会获取焦点了,这个我以前用过,肯定好用。
⑼ Android中EditText获得焦点后马上又失去了,可能是什么造成的呢
中EditText获得焦点后马上又失去了有以下几种情况:
EditText处在一个可以滚动的控件中,例如ListView等,当得到焦点后软件键盘会弹出,这个时候滚动控件会重绘,因此会失去焦点。
EditText注册了焦点事件,得到焦点后将焦点转向了其它控件。
EditText禁止了获取焦点。
⑽ android编程中,Activity默认的背景色是白色还是黑色啊
android编程中,Activity默认的背景色是白色,主要是因为其变成软件默认的是白色。要想改变其背景颜色,只需在Background Color选项里进行更改即可。
当一个 Activity 实例被创建、销毁或者启动另外一个 Activity 时,它在这四种状态之间进行转换,这种转换的发生依赖于用户程序的动作。
(10)android失去焦点扩展阅读
在android 中,Activity 拥有四种基本状态:
1、Active/Running
一个新 Activity 启动入栈后,它显示在屏幕最前端,处理是处于栈的最顶端(Activity栈顶),此时它处于可见并可和用户交互的激活状态,叫做活动状态或者运行状态(active or running)。
2、Paused
当 Activity失去焦点, 被一个新的非全屏的Activity
或者一个透明的Activity
被放置在栈顶,此时的状态叫做暂停状态(Paused)。
此时它依然与窗口管理器保持连接,Activity依然保持活力(保持所有的状态,成员信息,和窗口管理器保持连接),但是在系统内存极端低下的时候将被强行终止掉。所以它仍然可见,但已经失去了焦点故不可与用户进行交互。
3、Stopped
如果一个Activity被另外的Activity完全覆盖掉,叫做停止状态(Stopped)。它依然保持所有状态和成员信息,但是它不再可见,所以它的窗口被隐藏,当系统内存需要被用在其他地方的时候,Stopped的Activity将被强行终止掉。
4、Killed
如果一个Activity是Paused或者Stopped状态,系统可以将该Activity从内存中删除,Android系统采用两种方式进行删除,要么要求该Activity结束,要么直接终止它的进程。当该Activity再次显示给用户时,它必须重新开始和重置前面的状态。
参考资料来源:网络—android编程
参考资料来源:网络—android开发
参考资料来源:网络—activity