-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
##与视图焦点相关的属性及方法 | ||
###对于视图 | ||
方法: | ||
|
||
方法描述|在普通模式下|在触摸模式下 | ||
-------|-----------|---------- | ||
设置view接受焦点的资格|`setFocusable()`|`setFocusableInTouchMode()` | ||
获取view是否有接受焦点的资格|`isFocusable()`|`isFocusableInTouchMode()` | ||
请求获取焦点|`requestFocus()`|`requestFocusFromTouch()` | ||
|
||
对应的xml属性: | ||
`android:focusable=""` true或false | ||
`android:focusableInTouchMode=""` true或false | ||
|
||
###对于视图组控件 | ||
还有一个属性和方法用来控制焦点 | ||
xml中的属性: | ||
|
||
```xml | ||
android:descendantFocusability="" | ||
``` | ||
|
||
属性参数|意义 | ||
-------|---- | ||
beforeDescendants| viewgroup会优先其子类控件而获取到焦点 | ||
afterDescendants | viewgroup只有当其子类控件不需要获取焦点时才获取焦点 | ||
blocksDescendants | viewgroup会覆盖子类控件而直接获得焦点即**禁止子控件获取焦点** | ||
|
||
方法: | ||
|
||
```java | ||
view.setDescendantFocusability() | ||
参数:ViewGroup.FOCUS_AFTER_DESCENDANTS | ||
ViewGroup.FOCUS_BEFORE_DESCENDANTS | ||
ViewGroup.FOCUS_BLOCK_DESCENDANTS | ||
``` | ||
|
||
|
||
##对于上面的了解我们解决遇到的问题 | ||
###解决ListView中CheckBox抢焦点的问题 | ||
|
||
**问题描述:** | ||
>ListView item中包含CheckBox、Button等之类(EditText除外,这个复杂点,在下面有解决),这些子控件默认有获取焦点的能力,视图组控件中如果包含有获取焦点能力的子控件,那么焦点会自动转移到该子控件上。并且由于每一个Window只拥有一个焦点,点击事件的触发,前提是该控件有焦点,所以视图组控件的点击将不会被触发。 | ||
**解决方法:** | ||
第一种方案: | ||
给视图组控件设置属性: | ||
|
||
```xml | ||
android:descendantFocusability="blocksDescendants" | ||
``` | ||
|
||
第二种方案: | ||
给具有抢夺焦点能力的子控件设置属性: | ||
```xml | ||
android:focusable="false" | ||
android:focusableInTouchMode="false" | ||
``` | ||
|
||
###解决ListView中EditText获取不到焦点的问题 | ||
为了使listView的点击事件和EditText的点击编辑输入都有效我们不能简单的设置EditText获取焦点等方法解决 | ||
需要在ListView的item选择事件中处理 | ||
|
||
首先在listView的xml文件中配置 | ||
|
||
```xml | ||
android:descendantFocusability="beforeDescendants" | ||
``` | ||
|
||
其次在监听事件中处理焦点: | ||
|
||
```java | ||
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | ||
@Override | ||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | ||
//设置视图组ListView让出焦点 | ||
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); | ||
EditText editText = (EditText) view.findViewById(R.id.cart_total_text); | ||
//EditText 请求获取焦点 | ||
editText.requestFocus(); | ||
|
||
} | ||
|
||
@Override | ||
public void onNothingSelected(AdapterView<?> parent) { | ||
//设置视图组ListView获取焦点 | ||
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); | ||
} | ||
}); | ||
``` |