下面列出了android.view.View#isFocused ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateHint(View child) {
if (labelTextView == null)
return;
if (child == null) {
labelTextView.setVisibility(GONE);
return;
}
if (labelMode == LabelMode.Persistent || labelMode == LabelMode.Floating && child.isFocused() ||
labelMode == LabelMode.IfNotEmpty && (child.isFocused() || child instanceof android.widget.TextView && ((android.widget.TextView) child).getText().length() > 0)) {
labelTextView.animateVisibility(VISIBLE);
if (child instanceof EditText)
((EditText) child).setHint(null);
} else if (labelMode != LabelMode.Hint) {
labelTextView.animateVisibility(INVISIBLE);
if (child instanceof EditText)
((EditText) child).setHint(label + (((EditText) child).isRequired() ? " *" : ""));
} else {
labelTextView.setVisibility(GONE);
}
}
/**
* 顶部焦点获取
*
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean focusFlag = false;
for (View v : mViews) {
if (v.isFocused()) {
focusFlag = true;
}
}
Log.d(TAG, "code:" + keyCode + " flag:" + focusFlag);
if (focusFlag) {
if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode) {
if (mCurrentIndex > 0) {
mViews[--mCurrentIndex].requestFocus();
}
return true;
} else if (KeyEvent.KEYCODE_DPAD_RIGHT == keyCode) {
if (mCurrentIndex < 2) {
mViews[++mCurrentIndex].requestFocus();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
/**
* 获取是否当前 View 就是焦点 View
* @param view {@link View}
* @return {@code true} yes, {@code false} no
*/
public static boolean isFocused(final View view) {
if (view != null) {
return view.isFocused();
}
return false;
}
@Override
public void onLayoutChange(View view, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (view.isFocused() && view instanceof TextView) {
// A focused view changed its bounds. Follow it?
int height = bottom - top;
int oldHeight = oldBottom - oldTop;
if (oldHeight != height) {
zoomToView(view, false);
}
} else {
view.removeOnLayoutChangeListener(this);
}
}
private static boolean hasFocusedChild(View view) {
if (view.isFocused())
return true;
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
if (hasFocusedChild(group.getChildAt(i)))
return true;
}
}
return false;
}
/**
* Show the software keyboard and request focus to a certain input field.
*
* @param activity The activity hosting the view to be in focus.
* @param view The view requesting focus.
*/
public static void showSoftKeyboard(Activity activity, View view) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
if (!view.isFocused()) {
view.requestFocus();
}
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
}
/**
* Show the software keyboard and request focus to a certain input field.
*
* @param activity The activity hosting the view to be in focus.
* @param view The view requesting focus.
*/
public static void showSoftKeyboard(Activity activity, View view) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
if (!view.isFocused()) {
view.requestFocus();
}
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
}
private int getDeepestFocusedViewWithId(View view) {
int lastKnownId = view.getId();
while (!view.isFocused() && view instanceof ViewGroup && view.hasFocus()) {
view = ((ViewGroup) view).getFocusedChild();
final int id = view.getId();
if (id != View.NO_ID) {
lastKnownId = view.getId();
}
}
return lastKnownId;
}
/**
* Sets focus on given view.
*
* @param view
* the view to focus.
*/
public void focus(final View view) {
if (view.isFocused()) {
return;
}
if (!view.isFocusable()) {
throw new AssertionError("The view " + view.getId() + " is not focusable");
}
if (!view.requestFocus()) {
throw new AssertionError("The view " + view.getId() + " did not took the focus");
}
}
public boolean arrowScroll(int direction) {
View currentFocused = findFocus();
if (currentFocused == this) {
currentFocused = null;
}
View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
int maxJump = getMaxScrollAmount();
if (nextFocused == null || !isWithinDeltaOfScreen(nextFocused, maxJump, getHeight())) {
int scrollDelta = maxJump;
if (direction == 33 && getScrollY() < scrollDelta) {
scrollDelta = getScrollY();
} else if (direction == 130 && getChildCount() > 0) {
int daBottom = getChildAt(0).getBottom();
int screenBottom = (getScrollY() + getHeight()) - getPaddingBottom();
if (daBottom - screenBottom < maxJump) {
scrollDelta = daBottom - screenBottom;
}
}
if (scrollDelta == 0) {
return false;
}
int i;
if (direction == 130) {
i = scrollDelta;
} else {
i = -scrollDelta;
}
doScrollY(i);
} else {
nextFocused.getDrawingRect(this.mTempRect);
offsetDescendantRectToMyCoords(nextFocused, this.mTempRect);
doScrollY(computeScrollDeltaToGetChildRectOnScreen(this.mTempRect));
nextFocused.requestFocus(direction);
}
if (currentFocused != null && currentFocused.isFocused() && isOffScreen(currentFocused)) {
int descendantFocusability = getDescendantFocusability();
setDescendantFocusability(131072);
requestFocus();
setDescendantFocusability(descendantFocusability);
}
return true;
}
@Override
public boolean matchesSafely(View view) {
return view.isFocused() == isFocused;
}