码迷,mamicode.com
首页 > 其他好文 > 详细

焦点问题总结

时间:2015-05-24 21:56:57      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:android   输入法   布局   焦点   

  
  

一、什么是焦点
button获取焦点的话就是橙色,edittext获取焦点是竖线,等等这些是焦点。
二、关于焦点的说明
触摸事件可能要触发多次,但点击事件可能就一次。触摸事件在先,点击事件在后。
在触摸事件中如果return false那么点击事件也没有。



三、焦点顺序及切换的问题。
默认情况下,默认情况下,按而已自上而下获取焦点,但是我们也可以自定义来控制获取焦点的顺序
强制性的获取焦点就是三句话,缺一不可:获取焦点
btnSearch.setFocusable(true)
btnSearch.setFocusableInTouchMode(true);在Touch模式下获取焦点需要设置focusableInTouchMode为true。
btnSearch.requestFocus();

setFocusable( false);肯定就是没有获取焦点的资格。
所以不管怎么弄,都不会有焦点,但是触摸事件与点击事件正常使用。只不过焦点不显示而已。
四、关于editext获取焦点的问题
默认情况下,默认情况下,按而已自上而下获取焦点,edittext具有较高的获取焦点的优先权,即使它在下面,它也可能先获取焦点,那么如果不让其获取焦点,则setfoucable(false),永远也获取不到焦点资格,触摸,点击都不获取焦点。那么只是让默认不获取焦点,而点击或触摸获取焦点,怎么办?
在EditText前面放置一个看不到的LinearLayout,让它率先获取焦点,代码如下:
<LinearLayout android:focusable="true" android:focusableInTouchMode="true" 
当我们键盘有上下左右键的时候
android:nextFocusUp- 定义当点up键时,哪个控件将获得焦点

android:nextFocusDown-定义当点down键时,哪个控件将获得焦点

android:nextFocusLeft-定义当点left键时,哪个控件将获得焦点

android:nextFocusRight--定义当点right键时,哪个控件将获得焦点
触摸事件发生时,edittext如果没有焦点会获取焦点。如果设置了 setfoucable(false),永远也获取不到焦点资格
多个edittext竖排处理:意在表单中想设置某一个如EditText获取焦点,光设置这个是不行的,需要将这个EditText前面的focusable都设置为false才行。

editext.clearfocus方法不好使

五、弹出键盘的问题
       焦点与键盘没有必然的关系,与触摸事件有关系。
      当触摸事件发生时,如果editext没有焦点,则会获取焦点,
      当触摸事件发生时,会弹出键盘。
     那么如何默认强制弹出软键盘呢?
使用不受限制, 即使editext没有焦点   androidmainfest中activity属性里边
android:windowSoftInputMode="stateVisible"进来就显示
adjustResize该Activity总是调整屏幕的大小以便留出软键盘的空间,这个也很常用。也是弹出软键盘的

各值的含义:

【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置

【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示

【C】stateHidden:用户选择activity时,软键盘总是被隐藏

【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的

【E】stateVisible:软键盘通常是可见的,一进来就显示键盘

【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态

【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示

【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间

【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

"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函数进行跟踪,如果为平移模式,那么该函数可能不会被调用。

如何默认不弹出软件键盘
方法一:
在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden

方法二:
强制隐藏Android输入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);  
           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
           imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

方法三EditText始终不弹出软件键盘(不太用,你始终不弹出来,你用它干嘛)
例:EditText edit=(EditText)findViewById(R.id.edit);
       edit.setInputType(InputType.TYPE_NULL);



在应用键盘时,需要考虑这么几个问题:

【一】当有焦点产生时,软键盘是隐藏还是显示

【二】是否减少活动主窗口大小以便腾出空间放软键盘



键盘中的显示什么可以自己定义:android:imeOptions="actionDone"后将设置为Done点击是将关闭软盘

,send,search等。默认的软盘显示的action是“Next”,点击会到下一个输入框,如下:



六、焦点变化监听onWindowFocusChanged()    /**  * onWindowFocusChanged方法用于监听一个activity是否加载完毕,
Activity生命周期中,     * onStart, onResume, onCreate都不是真正visible的时间点,真正的visible时间点是onWindowFocusChanged()函数被执行
七、相关类的说明 
例子:
setFocusable()   设置view接受焦点的资格    
isFocusable()    view是否具有接受焦点的资格   
setFocusInTouchMode()      对应在触摸模式下,设置是否有焦点来响应点触的资格          
isFocusableInTouchMode()  对应在触摸模式下,view是否具有焦点的资格 
requestFocus()
requestFocus(int direction)当用户在某个界面聚集焦点,参数为下面的4个
View.FOCUS_LEFT     Move focus to the left 
View.FOCUS_UP       Move focus up 
View.FOCUS_RIGHT    Move focus to the right 
View.FOCUS_DOWN     Move focus down             
requestFocusFromTouch()    触摸模式下 
requestChildFocus (View child, View focused)   ------viewGroup 
1 父元素调用此方法 
2 child  将要获取焦点的子元素 
3 focused 现在拥有焦点的子元素 
isInTouchMode()    触摸模式 

焦点问题总结

标签:android   输入法   布局   焦点   

原文地址:http://blog.csdn.net/yclfdn2004/article/details/45955573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!