Java Code Examples for android.view.View.requestFocus()

The following are Jave code examples for showing how to use requestFocus() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: letv   File: NestedScrollView.java   Source Code and License Vote up 7 votes
private boolean scrollAndFocus(int direction, int top, int bottom) {
    boolean handled = true;
    int height = getHeight();
    int containerTop = getScrollY();
    int containerBottom = containerTop + height;
    boolean up = direction == 33;
    View newFocused = findFocusableViewInBounds(up, top, bottom);
    if (newFocused == null) {
        newFocused = this;
    }
    if (top < containerTop || bottom > containerBottom) {
        doScrollY(up ? top - containerTop : bottom - containerBottom);
    } else {
        handled = false;
    }
    if (newFocused != findFocus()) {
        newFocused.requestFocus(direction);
    }
    return handled;
}
 
Example 2
Project: MobileAppForPatient   File: MeterPreferenceDialog.java   Source Code and License Vote up 7 votes
@Override
public void onClick(final View v) {
	if (!v.isFocused()) {
		v.setFocusable(true);
		v.setFocusableInTouchMode(true);					
		v.requestFocus();
		GuiUtils.setKeypadVisibility(getActivity(), (EditText)v, View.VISIBLE);
		
		// 這裡會先delay再送出event,要不然softkeyboard不會出現				
		/*(new Handler()).postDelayed(new Runnable() {
            public void run() {			            	
            	v.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
            	v.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
            }
        }, 200);*/
	}
}
 
Example 3
Project: NoteBuddy   File: LoginActivity.java   Source Code and License Vote up 6 votes
/**
 * Attempts to sign in with the login form.
 * If there are form errors, the errors are presented
 * and no actual login attempt is made.
 */
private void attemptLogin() {
    try {
        m_passwordView.setError(null);
        m_password = m_passwordView.getText().toString();
        boolean cancel = false;
        View focusView = null;

        if(TextUtils.isEmpty(m_password)) {
            m_passwordView.setError(getString(R.string.error_field_required));
            focusView = m_passwordView;
            cancel = true;
        }

        String currentHash = LoginHashCreator.getLoginHash(m_context, m_password);
        String verificationHash = KeyValueDB.getVerificationPasswordHash(m_context);
        boolean correctPassword = (currentHash.equals(verificationHash));

        if(!correctPassword) {
            m_passwordView.setError(getString(R.string.error_login_wrong_password));
            focusView = m_passwordView;
            cancel = true;
        }

        if(cancel) {
            focusView.requestFocus();
            m_passwordView.setText(null);
        } else {
            Toast.makeText(getApplicationContext(), getString(R.string.success_login_general)
                    + ". " + getString(R.string.greeting_general) + ", " + KeyValueDB.getUsername(m_context)
                    + "!", Toast.LENGTH_SHORT).show();
            showProgress(true);
            startActivity(NOTES_ACTIVITY, true);
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), getString(R.string.error_login_general) + ". " + getString(R.string.action_try_again) + ".", Toast.LENGTH_SHORT).show();
    }
}
 
Example 4
Project: WebPager   File: CustomViewPager.java   Source Code and License Vote up 6 votes
/**
 * 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;
}
 
Example 5
Project: GitHub   File: ForgotPassActivity.java   Source Code and License Vote up 6 votes
/**
 * Attempts to recover the account specified by the login form.
 * If there are form errors (invalid email, missing fields, etc.), the
 * errors are presented and no actual login attempt is made.
 */
public void attemptRecover() {
    // Store values at the time of the login attempt.
    String email = edit_email.getText().toString();

    boolean cancel = false;
    View focusView = null;

    ValidateUserInfo validate = new ValidateUserInfo();

    // Check for a valid email address.
    if (TextUtils.isEmpty(email)) {
        edit_email.setError(getString(R.string.error_field_required));
        focusView = edit_email;
        cancel = true;
    } else if (!validate.isEmailValid(email)) {
        edit_email.setError(getString(R.string.error_invalid_email));
        focusView = edit_email;
        cancel = true;
    }

    if (cancel) {
        // There was an error; don't attempt login and focus the first
        // form field with an error.
        focusView.requestFocus();
    } else {
        //TODO Recover account logic
        // Show a progress spinner, and kick off a background task to
        // perform the user recover info attempt.
        mForgotTask = new ForgotPassTask(email);
        mForgotTask.execute((Void) null);
    }
}
 
Example 6
Project: JPush   File: SettingActivity.java   Source Code and License Vote up 6 votes
@Override
public void onClick(View v) {
	switch (v.getId()) {
	case R.id.bu_setTime:
		v.requestFocus();
		v.requestFocusFromTouch();
		setPushTime();
		break;
	}
}
 
Example 7
Project: TextReader   File: DirectionalViewpager.java   Source Code and License Vote up 6 votes
/**
 * 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 && child.requestFocus(direction, previouslyFocusedRect)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 8
Project: WiFiSDCryptoLocker   File: SetupActivity.java   Source Code and License Vote up 6 votes
/**
 * Try to register a new account.
 */
public void checkRegistrationPassword(EditText passwordTextView) {

    if (mRegisterTask != null) {
        return;
    }

    // Reset errors.
    passwordTextView.setError(null);

    // Store values at the time of the login attempt.
    String password = passwordTextView.getText().toString();

    boolean cancel = false;
    View focusView = null;

    // Check for valid password
    try {
        if (!CryptoUtils.isPasswordValid(password)) {
            focusView = passwordTextView;
            cancel = true;
        }
    } catch (InvalidPasswordException e) {
        passwordTextView.setError(e.getMessage());
    }

    if (cancel) {
        // There was an error; don't attempt login and focus the first
        // form field with an error.
        focusView.requestFocus();
    } else {
        temp_password = password;
        //TODO store password in a secure way
    }
}
 
Example 9
Project: WiFiSDCryptoLocker   File: LoginActivity.java   Source Code and License Vote up 6 votes
/**
 * Attempts to sign in the account specified by the login form.
 * If there are form errors (missing fields), the
 * errors are presented and no actual login attempt is made.
 */
private void attemptLogin() {
    if (mAuthTask != null) {
        return;
    }

    // Reset errors.
    mPasswordView.setError(null);

    // Store values at the time of the login attempt.
    String password = mPasswordView.getText().toString();

    boolean cancel = false;
    View focusView = null;

    // Check for empty password
    if (TextUtils.isEmpty(password)) {
        mPasswordView.setError(getString(R.string.error_field_required));
        focusView = mPasswordView;
        cancel = true;
    }

    if (cancel) {
        // There was an error; don't attempt login and focus the first
        // form field with an error.
        focusView.requestFocus();
    } else {
        // Show a progress spinner, and kick off a background task to
        // perform the user login attempt.
        showProgress(true);
        mAuthTask = new UserLoginTask(USER_NAME, password);
        mAuthTask.execute((Void) null);
    }
}
 
Example 10
Project: letv   File: NestedScrollView.java   Source Code and License Vote up 6 votes
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
    if (direction == 2) {
        direction = 130;
    } else if (direction == 1) {
        direction = 33;
    }
    View nextFocus = previouslyFocusedRect == null ? FocusFinder.getInstance().findNextFocus(this, null, direction) : FocusFinder.getInstance().findNextFocusFromRect(this, previouslyFocusedRect, direction);
    if (nextFocus == null || isOffScreen(nextFocus)) {
        return false;
    }
    return nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example 11
Project: AirPanel   File: Util.java   Source Code and License Vote up 6 votes
public static void showKeyboard(final View view) {
    view.requestFocus();
    InputMethodManager inputManager =
            (InputMethodManager) view.getContext().getSystemService(
                    Context.INPUT_METHOD_SERVICE);
    inputManager.showSoftInput(view, 0);
}
 
Example 12
Project: JKCloud   File: LoginActivity.java   Source Code and License Vote up 6 votes
private void attemptLogin() {
    // Reset errors.
    txtAccount.setError(null);
    txtPassword.setError(null);
    // Store values at the time of the login attempt.
    String account = txtAccount.getText().toString();
    String password = txtPassword.getText().toString();

    boolean cancel = false;
    View focusView = null;

    // Check for a valid password, if the user entered one.
    if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
        txtPassword.setError(getString(R.string.error_invalid_password));
        focusView = txtPassword;
        cancel = true;
    }
    // Check for a valid email address.
    if (TextUtils.isEmpty(account)) {
        txtAccount.setError(getString(R.string.error_field_required));
        focusView = txtAccount;
        cancel = true;
    }
    if (cancel) {
        focusView.requestFocus();
    } else {
        showLoading();
        hashMap.put("loginName", account);
        hashMap.put("pwd", StringUtil.MD5(password));
        new LoginPresenter(LoginActivity.this);
        presenter.getData(hashMap);
    }
}
 
Example 13
Project: OSchina_resources_android   File: TDevice.java   Source Code and License Vote up 6 votes
public static void showSoftKeyboard(View view) {
    if (view == null) return;
    view.setFocusable(true);
    view.setFocusableInTouchMode(true);
    if (!view.isFocused()) view.requestFocus();

    InputMethodManager inputMethodManager = (InputMethodManager) view.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(view, 0);
}
 
Example 14
Project: letv   File: BasicActivity.java   Source Code and License Vote up 6 votes
public void setViewFocus(View view) {
    if (view != null) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.requestFocusFromTouch();
    }
}
 
Example 15
Project: easyappointments-android-client   File: LoginActivity.java   Source Code and License Vote up 5 votes
/**
 * Attempts to sign in or register the account specified by the login form.
 * If there are form errors (invalid email, missing fields, etc.), the
 * errors are presented and no actual login attempt is made.
 */
private void attemptLogin() {
    if (mAuthTask != null) {
        return;
    }

    // Reset errors.
    mUsernameView.setError(null);
    mPasswordView.setError(null);
    mUrl.setError(null);


    // Store values at the time of the login attempt.
    String username = mUsernameView.getText().toString();
    String password = mPasswordView.getText().toString();
    String url = mUrl.getText().toString();

    boolean cancel = false;
    View focusView = null;

    if(Validator.absoluteUrlIsValid(url) == false){
        mUrl.setError(getString(R.string.error_invalid_url));
        focusView = mUrl;
        cancel = true;
    }else if (TextUtils.isEmpty(password)) {
        mPasswordView.setError(getString(R.string.error_invalid_password));
        focusView = mPasswordView;
        cancel = true;
    }else if (TextUtils.isEmpty(username)) {
        mUsernameView.setError(getString(R.string.error_field_required));
        focusView = mUsernameView;
        cancel = true;
    }

    if (cancel) {
        // There was an error; don't attempt login and focus the first
        // form field with an error.
        focusView.requestFocus();
    } else {
        // Show a progress spinner, and kick off a background task to
        // perform the user login attempt.
        showProgress(true);
        mAuthTask = new UserLoginTask(mLoginFormView, username, password, url);
        mAuthTask.execute((Void) null);
    }
}
 
Example 16
Project: FlickLauncher   File: AllAppsSearchBarController.java   Source Code and License Vote up 5 votes
protected void unfocusSearchField() {
    View nextFocus = mInput.focusSearch(View.FOCUS_DOWN);
    if (nextFocus != null) {
        nextFocus.requestFocus();
    }
}
 
Example 17
Project: letv   File: NestedScrollView.java   Source Code and License Vote up 5 votes
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 18
Project: SMS302   File: LoginActivity.java   Source Code and License Vote up 5 votes
/**
 * Attempts to sign in or register the account specified by the login form.
 * If there are form errors (invalid email, missing fields, etc.), the
 * errors are presented and no actual login attempt is made.
 */
private void attemptLogin() {
    if (mAuthTask != null) {
        return;
    }

    // Reset errors.
    mEmailView.setError(null);
    mPasswordView.setError(null);

    // Store values at the time of the login attempt.
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();

    boolean cancel = false;
    View focusView = null;

    // Check for a valid password, if the user entered one.
    if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
        mPasswordView.setError(getString(R.string.error_invalid_password));
        focusView = mPasswordView;
        cancel = true;
    }

    // Check for a valid email address.
    if (TextUtils.isEmpty(email)) {
        mEmailView.setError(getString(R.string.error_field_required));
        focusView = mEmailView;
        cancel = true;
    } else if (!isEmailValid(email)) {
        mEmailView.setError(getString(R.string.error_invalid_email));
        focusView = mEmailView;
        cancel = true;
    }

    if (cancel) {
        // There was an error; don't attempt login and focus the first
        // form field with an error.
        focusView.requestFocus();
    } else {
        // Show a progress spinner, and kick off a background task to
        // perform the user login attempt.
        showProgress(true);
        mAuthTask = new UserLoginTask(email, password);
        mAuthTask.execute((Void) null);
    }
}
 
Example 19
Project: javaide   File: TwoDScrollView.java   Source Code and License Vote up 5 votes
/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();
    if (!canScroll()) {
        if (isFocused()) {
            View currentFocused = findFocus();
            if (currentFocused == this) currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_UP:
                if (!event.isAltPressed()) {
                    handled = arrowScroll(View.FOCUS_UP, false);
                } else {
                    handled = fullScroll(View.FOCUS_UP, false);
                }
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                if (!event.isAltPressed()) {
                    handled = arrowScroll(View.FOCUS_DOWN, false);
                } else {
                    handled = fullScroll(View.FOCUS_DOWN, false);
                }
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                if (!event.isAltPressed()) {
                    handled = arrowScroll(View.FOCUS_LEFT, true);
                } else {
                    handled = fullScroll(View.FOCUS_LEFT, true);
                }
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (!event.isAltPressed()) {
                    handled = arrowScroll(View.FOCUS_RIGHT, true);
                } else {
                    handled = fullScroll(View.FOCUS_RIGHT, true);
                }
                break;
        }
    }
    return handled;
}
 
Example 20
Project: Protestr   File: SignUpActivity.java   Source Code and License Vote up 5 votes
@Override
public boolean checkParametersErrors() {
    final String email = etEmail.getText().toString().trim();
    final String userName = etUsername.getText().toString().trim();
    final String password = etPassword.getText().toString().trim();
    final String passwordRepeat = etRepeatPassword.getText().toString().trim();

    boolean error = false;
    View focusViewOnError = null;

    if (TextUtils.isEmpty(email)) {
        etEmail.setError(getString(R.string.empty_field));
        focusViewOnError = etEmail;
        error = true;
    }
    if (TextUtils.isEmpty(userName)) {
        etUsername.setError(getString(R.string.empty_field));
        focusViewOnError = etUsername;
        error = true;
    }
    if (TextUtils.isEmpty(password)) {
        passwordInputLayout.setPasswordVisibilityToggleEnabled(false);
        etPassword.setError(getString(R.string.empty_field));
        focusViewOnError = etPassword;
        error = true;
    }
    if (TextUtils.isEmpty(passwordRepeat)) {
        repeatPasswordInputLayout.setPasswordVisibilityToggleEnabled(false);
        etRepeatPassword.setError(getString(R.string.empty_field));
        focusViewOnError = etRepeatPassword;
        error = true;
    }

    if (!TextUtils.isEmpty(email) && !FormatUtils.isValidEmailFormat(email)) {
        passwordInputLayout.setPasswordVisibilityToggleEnabled(false);
        etEmail.setError(getString(R.string.email_not_valid_error));
        focusViewOnError = etEmail;
        error = true;
    }

    if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(passwordRepeat) &&
            !password.equals(passwordRepeat)) {
        repeatPasswordInputLayout.setPasswordVisibilityToggleEnabled(false);
        etRepeatPassword.setError(getString(R.string.password_doesnt_match));
        focusViewOnError = etRepeatPassword;
        error = true;
    }

    if (error) {
        focusViewOnError.requestFocus();
    }

    return error;
}