android.view.View#requestFocus ( )源码实例Demo

下面列出了android.view.View#requestFocus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: TvRecyclerView   文件: GridLayoutManager.java
/**
 * In layout, if there is a view that needs to get focus,
 * make sure the view is displayed.
 */
private void scrollToFocusViewInLayout() {
    if (hasFocus() && !isSmoothScrolling() && mFocusPosition != NO_POSITION) {
        View focusView = findViewByPosition(mFocusPosition);
        if (focusView != null) {
            if (getScrollPosition(focusView, mTwoInts)) {
                int maxScrollDistance = getMaxScrollDistance();
                int scrollDistance = mTwoInts[0];
                if (scrollDistance > 0) {
                    if (mHorizontalOffset + scrollDistance > maxScrollDistance) {
                        scrollDistance = maxScrollDistance - mHorizontalOffset;
                    }
                    offsetChildrenPrimary(-scrollDistance);
                }
                if (!focusView.hasFocus()) {
                    focusView.requestFocus();
                    dispatchChildSelected();
                }
            }
        } else if (mFocusPosition != NO_POSITION){
            scrollToSelection(mBaseRecyclerView, mFocusPosition, mSubFocusPosition, true,
                    mPrimaryScrollExtra);
        }
    }
}
 
源代码2 项目: arcusandroid   文件: AlertPopup.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);

    closeBtn.setOnClickListener(this);

    view.setFocusableInTouchMode(true);
    view.requestFocus();
    view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                doClose();
                return true;
            } else {
                return false;
            }
        }
    });

    return view;
}
 
@Override
protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    if (gainFocus) {
        // We favor natural focus if we don't want to remember focus AND if previously focused
        // rectangle is NOT null. Usually latter condition holds true if simple requestFocus()
        // is called and holds false if focus changed during navigation. Overall this is done
        // because of backward compatibility - some code is dependent on the fact that
        // focused position will be restored if requestFocus() is called and it expects
        // to have natural focus when ordinary navigation happens.
        boolean favorNaturalFocus = !mRememberLastFocus && previouslyFocusedRect != null;
        View lastFocusedView = mFocusArchivist.getLastFocus(this);
        if (favorNaturalFocus || lastFocusedView == null) {
            requestNaturalFocus(direction, previouslyFocusedRect);
        } else {
            lastFocusedView.requestFocus();
        }
    }
}
 
public void handleMessage(android.os.Message msg) {
    View view = (View) msg.obj;
    // 显示软键盘
    if (msg.what == Constants.INPUT_METHOD_SHOW) {
        boolean result = mInputMan.showSoftInput(view, 0);
        if (!result && totalTime < Constants.LIMIT_TIME) {
            totalTime += Constants.IDLE;
            Message message = Message.obtain(msg);
            mHandler.sendMessageDelayed(message, Constants.IDLE);
        } else if (!isFinish) {
            totalTime = 0;
            result = view.requestFocus();
            isFinish = true;
        }
    } else if (msg.what == Constants.INPUT_METHOD_DISAPPEAR) {
        // 隐藏软键盘
        mInputMan.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

}
 
源代码5 项目: AndroidChromium   文件: TabContentViewParent.java
@Override
public void onContentChanged(Tab tab) {
    // If the tab is frozen, both native page and content view are not ready.
    if (tab.isFrozen()) return;

    View viewToShow = getViewToShow(tab);
    if (isShowing(viewToShow)) return;

    removeCurrentContent();
    LayoutParams lp = (LayoutParams) viewToShow.getLayoutParams();
    if (lp == null) {
        lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }
    // Weirdly enough, if gravity is not top, top_margin is not respected by FrameLayout.
    // Yet for many native pages on tablet, top_margin is necessary to not overlap the tab
    // switcher.
    lp.gravity = Gravity.TOP;
    UiUtils.removeViewFromParent(viewToShow);
    addView(viewToShow, CONTENT_INDEX, lp);
    viewToShow.requestFocus();
}
 
源代码6 项目: JumpGo   文件: LightningView.java
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(@Nullable View view, @NonNull MotionEvent arg1) {
    if (view == null)
        return false;

    if (!view.hasFocus()) {
        view.requestFocus();
    }
    mAction = arg1.getAction();
    mY = arg1.getY();
    if (mAction == MotionEvent.ACTION_DOWN) {
        mLocation = mY;
    } else if (mAction == MotionEvent.ACTION_UP) {
        final float distance = (mY - mLocation);
        if (distance > SCROLL_UP_THRESHOLD && view.getScrollY() < SCROLL_UP_THRESHOLD) {
            mUIController.showActionBar();
        } else if (distance < -SCROLL_UP_THRESHOLD) {
            mUIController.hideActionBar();
        }
        mLocation = 0;
    }
    mGestureDetector.onTouchEvent(arg1);
    return false;
}
 
源代码7 项目: guideshow   文件: ViewPager.java
/**
 * We only want the current page that is being shown to be focusable.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction,
        Rect previouslyFocusedRect) {
    int index;
    int increment;
    int end;
    int count = getChildCount();
    if ((direction & FOCUS_FORWARD) != 0) {
        index = 0;
        increment = 1;
        end = count;
    } else {
        index = count - 1;
        increment = -1;
        end = -1;
    }
    for (int i = index; i != end; i += increment) {
        View child = getChildAt(i);
        if (child.getVisibility() == VISIBLE) {
            ItemInfo ii = infoForChild(child);
            if (ii != null && ii.position == mCurItem) {
                if (child.requestFocus(direction, previouslyFocusedRect)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
源代码8 项目: TurboLauncher   文件: Workspace.java
void moveToCustomContentScreen(boolean animate) {
    if (hasCustomContent()) {
        int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID);
        if (animate) {
            snapToPage(ccIndex);
        } else {
            setCurrentPage(ccIndex);
        }
        View child = getChildAt(ccIndex);
        if (child != null) {
            child.requestFocus();
        }
     }
    exitWidgetResizeMode();
}
 
源代码9 项目: UltimateAndroid   文件: ViewPager.java
/**
 * We only want the current page that is being shown to be focusable.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction,
        Rect previouslyFocusedRect) {
    int index;
    int increment;
    int end;
    int count = getChildCount();
    if ((direction & FOCUS_FORWARD) != 0) {
        index = 0;
        increment = 1;
        end = count;
    } else {
        index = count - 1;
        increment = -1;
        end = -1;
    }
    for (int i = index; i != end; i += increment) {
        View child = getChildAt(i);
        if (child.getVisibility() == VISIBLE) {
            ItemInfo ii = infoForChild(child);
            if (ii != null && ii.position == mCurItem) {
                if (child.requestFocus(direction, previouslyFocusedRect)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
源代码10 项目: zhangshangwuda   文件: CustomScrollView.java
private boolean scrollAndFocusH(int direction, int left, int right) {
	boolean handled = true;

	int width = getWidth();
	int containerLeft = getScrollX();
	int containerRight = containerLeft + width;
	boolean goLeft = direction == View.FOCUS_LEFT;

	View newFocused = findFocusableViewInBoundsH(goLeft, left, right);
	if (newFocused == null) {
		newFocused = this;
	}

	if (left >= containerLeft && right <= containerRight) {
		handled = false;
	} else {
		int delta = goLeft ? (left - containerLeft)
				: (right - containerRight);
		doScrollX(delta);
	}

	if (newFocused != findFocus() && newFocused.requestFocus(direction)) {
		mScrollViewMovedFocus = true;
		mScrollViewMovedFocus = false;
	}

	return handled;
}
 
源代码11 项目: AndroidTreeView   文件: TwoDScrollView.java
/**
 * Fling the scroll view
 *
 * @param velocityY The initial velocity in the Y direction. Positive
 *                  numbers mean that the finger/curor is moving down the screen,
 *                  which means we want to scroll towards the top.
 */
public void fling(int velocityX, int velocityY) {
    if (getChildCount() > 0) {
        int height = getHeight() - getPaddingBottom() - getPaddingTop();
        int bottom = getChildAt(0).getHeight();
        int width = getWidth() - getPaddingRight() - getPaddingLeft();
        int right = getChildAt(0).getWidth();

        mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY, 0, right - width, 0, bottom - height);

        final boolean movingDown = velocityY > 0;
        final boolean movingRight = velocityX > 0;

        View newFocused = findFocusableViewInMyBounds(movingRight, mScroller.getFinalX(), movingDown, mScroller.getFinalY(), findFocus());
        if (newFocused == null) {
            newFocused = this;
        }

        if (newFocused != findFocus() && newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP)) {
            mTwoDScrollViewMovedFocus = true;
            mTwoDScrollViewMovedFocus = false;
        }

        awakenScrollBars(mScroller.getDuration());
        invalidate();
    }
}
 
源代码12 项目: letv   文件: BasicActivity.java
public void setViewFocus(View view) {
    if (view != null) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.requestFocusFromTouch();
    }
}
 
/**
 * Request natural focus.
 *
 * @param direction             direction in which focus is changing
 * @param previouslyFocusedRect previously focus rectangle
 */
private void requestNaturalFocus(int direction, Rect previouslyFocusedRect) {
    FocusFinder ff = FocusFinder.getInstance();
    previouslyFocusedRect = previouslyFocusedRect == null
            ? new Rect(0, 0, 0, 0) : previouslyFocusedRect;
    View toFocus = ff.findNextFocusFromRect(this, previouslyFocusedRect, direction);
    toFocus = toFocus == null ? getChildAt(0) : toFocus;
    if (toFocus != null) {
        toFocus.requestFocus();
    }
}
 
源代码14 项目: DKVideoPlayer   文件: VerticalViewPager.java
/**
 * We only want the current page that is being shown to be focusable.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction,
                                              Rect previouslyFocusedRect) {
    int index;
    int increment;
    int end;
    int count = getChildCount();
    if ((direction & FOCUS_FORWARD) != 0) {
        index = 0;
        increment = 1;
        end = count;
    } else {
        index = count - 1;
        increment = -1;
        end = -1;
    }
    for (int i = index; i != end; i += increment) {
        View child = getChildAt(i);
        if (child.getVisibility() == VISIBLE) {
            ItemInfo ii = infoForChild(child);
            if (ii != null && ii.position == mCurItem) {
                if (child.requestFocus(direction, previouslyFocusedRect)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
源代码15 项目: appcan-android   文件: CustomViewAbove.java
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) currentFocused = null;

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
            direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            handled = nextFocused.requestFocus();
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}
 
源代码16 项目: LaunchEnr   文件: DragLayer.java
@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
    View topView = AbstractFloatingView.getTopOpenView(mLauncher);
    if (topView != null) {
        return topView.requestFocus(direction, previouslyFocusedRect);
    } else {
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }
}
 
源代码17 项目: android-utils   文件: FocusBinding.java
@Override
public void run(Boolean focus, View view) {
	if (view == null)
		return;
	if (focus) {
		view.setFocusable(true);
		view.requestFocus();
	}
	else {
		view.clearFocus();
		view.setFocusable(false);
	}
}
 
源代码18 项目: SnapchatClone   文件: RegisterActivity.java
private void requestFocus(View view) {
    if (view.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
 
@Override
public boolean onTouch(View v, MotionEvent event) {
    v.getParent().requestDisallowInterceptTouchEvent(true);

    if(!isInside(event)) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (mTouched) {
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    if (mClickListener != null) {
                        mClickListener.onClick(v);
                    }
                }
                v.setPressed(false);
                mTouched = false;
        }

        return true;

    } else {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.setPressed(true);
                mTouched = true;
                return true;

            case MotionEvent.ACTION_UP:
                if (mTouched && ViewUtils.isInsideView(v, (int)event.getRawX(), (int)event.getRawY())) {
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    if (mClickListener != null) {
                        mClickListener.onClick(v);
                    }
                }
                v.setPressed(false);
                mTouched = false;
                return true;

            case MotionEvent.ACTION_MOVE:
                return true;

            case MotionEvent.ACTION_CANCEL:
                v.setPressed(false);
                mTouched = false;
                return true;
        }

        return false;
    }
}
 
源代码20 项目: zidoorecorder   文件: PipService.java
/**
 * 初始化控件
 */
private void initView()
{
	mVPip = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_pip, null);
	View rlPip = (RelativeLayout) mVPip.findViewById(R.id.rl_pip);
	mFmPip = (FrameLayout) rlPip.findViewById(R.id.fm_pip);
	mLnFrameBg = (LinearLayout) rlPip.findViewById(R.id.ln_bg);
	mPgbLoading = (ProgressBar) mFmPip.findViewById(R.id.pgb_loading);
	mRlFrame = (RelativeLayout) mFmPip.findViewById(R.id.rl_frame);
	mTvNoSingle = (TextView) mFmPip.findViewById(R.id.tv_no_single);

	mRlController = (RelativeLayout) mVPip.findViewById(R.id.rl_control);
	mLnController = (LinearLayout) mRlController.findViewById(R.id.ln_controller);
	mLnScale = (LinearLayout) mRlController.findViewById(R.id.ln_scale);
	mLnAudio = (LinearLayout) mRlController.findViewById(R.id.ln_audio);
	mImgSwitch = (ImageView) mLnAudio.findViewById(R.id.img_switch);
	mTvReminds = (TextView) mRlController.findViewById(R.id.tv_reminds);

	mSfPip = (SurfaceView) mFmPip.findViewById(R.id.sf_pip);
	SurfaceHolder holder = mSfPip.getHolder();
	holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	holder.addCallback(this);

	View move = mLnController.findViewById(R.id.tv_move);
	mTvScale = (TextView) mLnController.findViewById(R.id.tv_scale);
	mTvAudio = (TextView) mLnController.findViewById(R.id.tv_audio);
	View exit = mLnController.findViewById(R.id.tv_exit);
	View audioSwitch = mLnAudio.findViewById(R.id.rl_audio);

	mScales = new TextView[4];
	mScales[0] = mLnScale.findViewById(R.id.tv_scale_0);
	mScales[1] = mLnScale.findViewById(R.id.tv_scale_1);
	mScales[2] = mLnScale.findViewById(R.id.tv_scale_2);
	mScales[3] = mLnScale.findViewById(R.id.tv_scale_3);

	for (int i = 0; i < mScales.length; i++)
	{
		View v = mScales[i];
		v.setTag(i);
		v.setOnClickListener(this);
		v.setOnKeyListener(this);
		v.setOnFocusChangeListener(this);
	}
	mScales[mScaleMode].setSelected(true);

	move.setOnClickListener(this);
	mTvScale.setOnClickListener(this);
	mTvAudio.setOnClickListener(this);
	exit.setOnClickListener(this);
	audioSwitch.setOnClickListener(this);

	move.setOnFocusChangeListener(this);
	mTvScale.setOnFocusChangeListener(this);
	mTvAudio.setOnFocusChangeListener(this);
	exit.setOnFocusChangeListener(this);
	audioSwitch.setOnFocusChangeListener(this);

	move.setOnKeyListener(this);
	mTvScale.setOnKeyListener(this);
	mTvAudio.setOnKeyListener(this);
	exit.setOnKeyListener(this);
	audioSwitch.setOnKeyListener(this);
	mFmPip.setOnKeyListener(this);

	adjustPipFrame(mWindowType.x, mWindowType.y, mWindowType.width, mWindowType.height);

	WindowManager.LayoutParams params = new WindowManager.LayoutParams();
	params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
	params.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
	params.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
	params.width = WindowManager.LayoutParams.MATCH_PARENT;
	params.height = WindowManager.LayoutParams.MATCH_PARENT;

	((WindowManager) getSystemService(WINDOW_SERVICE)).addView(mVPip, params);
	move.requestFocus();
}
 
 方法所在类
 同类方法