Java Code Examples for android.view.View#isFocused()

The following examples show how to use android.view.View#isFocused() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InputLayout.java    From Carbon with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: MainActivity.java    From android-tv-launcher with MIT License 6 votes vote down vote up
/**
 * 顶部焦点获取
 *
 * @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);
}
 
Example 3
Source File: ViewUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取是否当前 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;
}
 
Example 4
Source File: DocumentView.java    From ViewPrinter with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 5
Source File: LabelLayout.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: ActivityServiceUtils.java    From HgLauncher with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 7
Source File: ActivityServiceUtils.java    From HgLauncher with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 8
Source File: RecyclerView.java    From kripton with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: AndroidTester.java    From aedict with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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");
	}
}
 
Example 10
Source File: NestedScrollView.java    From letv with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 11
Source File: ViewMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matchesSafely(View view) {
  return view.isFocused() == isFocused;
}