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

The following are Jave code examples for showing how to use clearFocus() 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: QMUI_Android   File: QMUIDrawableHelper.java   Source Code and License Vote up 7 votes
/**
 * 从一个view创建Bitmap。
 * 注意点:绘制之前要清掉 View 的焦点,因为焦点可能会改变一个 View 的 UI 状态。
 * 来源:https://github.com/tyrantgit/ExplosionField
 *
 * @param view  传入一个 View,会获取这个 View 的内容创建 Bitmap。
 * @param scale 缩放比例,对创建的 Bitmap 进行缩放,数值支持从 0 到 1。
 */
public static Bitmap createBitmapFromView(View view, float scale) {
    if (view instanceof ImageView) {
        Drawable drawable = ((ImageView) view).getDrawable();
        if (drawable != null && drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
    }
    view.clearFocus();
    Bitmap bitmap = createBitmapSafely((int) (view.getWidth() * scale),
            (int) (view.getHeight() * scale), Bitmap.Config.ARGB_8888, 1);
    if (bitmap != null) {
        synchronized (sCanvas) {
            Canvas canvas = sCanvas;
            canvas.setBitmap(bitmap);
            canvas.save();
            canvas.drawColor(Color.WHITE); // 防止 View 上面有些区域空白导致最终 Bitmap 上有些区域变黑
            canvas.scale(scale, scale);
            view.draw(canvas);
            canvas.restore();
            canvas.setBitmap(null);
        }
    }
    return bitmap;
}
 
Example 2
Project: VirtualHook   File: ExplosionField.java   Source Code and License Vote up 7 votes
public static Bitmap createBitmapFromView(View view) {
	if (view instanceof ImageView) {
		Drawable drawable = ((ImageView) view).getDrawable();
		if (drawable != null && drawable instanceof BitmapDrawable) {
			return ((BitmapDrawable) drawable).getBitmap();
		}
	}
	view.clearFocus();
	Bitmap bitmap = createBitmapSafely(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888, 1);
	if (bitmap != null) {
		synchronized (sCanvas) {
			Canvas canvas = sCanvas;
			canvas.setBitmap(bitmap);
			view.draw(canvas);
			canvas.setBitmap(null);
		}
	}
	return bitmap;
}
 
Example 3
Project: Password-Storage   File: RegistrationActivity.java   Source Code and License Vote up 7 votes
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if ( v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return super.dispatchTouchEvent( event );
}
 
Example 4
Project: GongXianSheng   File: PlaceOrderProductAdapter.java   Source Code and License Vote up 6 votes
private void clearFocus(Activity activity) {
    View v = activity.getCurrentFocus();
    if (v != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        v.clearFocus();
    }
}
 
Example 5
Project: Pinview   File: Pinview.java   Source Code and License Vote up 6 votes
@Override
public void onFocusChange(View view, boolean isFocused) {
    if (isFocused && !mCursorVisible) {
        if (mDelPressed) {
            currentFocus = view;
            mDelPressed = false;
            return;
        }
        for (final EditText editText : editTextList) {
            if (editText.length() == 0) {
                if (editText != view) {
                    editText.requestFocus();
                } else {
                    currentFocus = view;
                }
                return;
            }
        }
        if (editTextList.get(editTextList.size() - 1) != view) {
            editTextList.get(editTextList.size() - 1).requestFocus();
        } else {
            currentFocus = view;
        }
    } else if (isFocused && mCursorVisible) {
        currentFocus = view;
    } else {
        view.clearFocus();
    }
}
 
Example 6
Project: SoftKeyboardUtil   File: BaseActivity.java   Source Code and License Vote up 6 votes
/**
 * 清除editText的焦点
 *
 * @param v   焦点所在View
 * @param ids 输入框
 */
public void clearViewFocus(View v, int... ids) {
    if (null != v && null != ids && ids.length > 0) {
        for (int id : ids) {
            if (v.getId() == id) {
                v.clearFocus();
                break;
            }
        }
    }


}
 
Example 7
Project: crockpod   File: SetAlarmActivityTest.java   Source Code and License Vote up 6 votes
@Test
public void theTimePickerIsGoneWhenEditingTheSearchField() {
    SetAlarmActivity activity = Robolectric.buildActivity(SetAlarmActivity.class).create().get();
    View searchField = activity.findViewById(R.id.podcast_rss_feed);
    View timePicker = activity.findViewById(R.id.time_picker);

    searchField.requestFocus();
    assertEquals(View.GONE, timePicker.getVisibility());

    searchField.clearFocus();
    assertEquals(View.VISIBLE, timePicker.getVisibility());
}
 
Example 8
Project: BrotherWeather   File: BaseActivity.java   Source Code and License Vote up 6 votes
@Override public boolean dispatchTouchEvent(MotionEvent ev) {
  if (isAutoHideInputView) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      View v = getCurrentFocus();
      if (v != null) {
        if ((v instanceof EditText)) {
          if (!AppUtils.isEventInVIew(v, ev)) {
            if (AppUtils.hideInput(this, v) && isClearFocusWhileAutoHideInputView) {
              v.clearFocus();
            }
          }
        }
      }
      return super.dispatchTouchEvent(ev);
    }
    // 必不可少,否则所有的组件都不会有TouchEvent了
    if (getWindow().superDispatchTouchEvent(ev)) {
      return true;
    }
  }

  try {
    return super.dispatchTouchEvent(ev);
  } catch (Exception e) {
    // ignored
  }
  return false;
}
 
Example 9
Project: OSchina_resources_android   File: TDevice.java   Source Code and License Vote up 6 votes
public static void hideSoftKeyboard(View view) {
    if (view == null) return;
    View mFocusView = view;

    Context context = view.getContext();
    if (context != null && context instanceof Activity) {
        Activity activity = ((Activity) context);
        mFocusView = activity.getCurrentFocus();
    }
    if (mFocusView == null) return;
    mFocusView.clearFocus();
    InputMethodManager manager = (InputMethodManager) mFocusView.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    manager.hideSoftInputFromWindow(mFocusView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
 
Example 10
Project: GCSApp   File: MyBitmapUtil.java   Source Code and License Vote up 6 votes
public static Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    // Reset the drawing cache background color to fully transparent
    // for the duration of this operation
    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
        v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
        Log.e("Folder", "failed getViewBitmap(" + v + ")", new RuntimeException());
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

    // Restore the view
    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);

    return bitmap;
}
 
Example 11
Project: GCSApp   File: MyBitmapUtil.java   Source Code and License Vote up 6 votes
public static Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    // Reset the drawing cache background color to fully transparent
    // for the duration of this operation
    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
        v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
        Log.e("Folder", "failed getViewBitmap(" + v + ")", new RuntimeException());
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

    // Restore the view
    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);

    return bitmap;
}
 
Example 12
Project: Hotspot-master-devp   File: BitmapCommonUtils.java   Source Code and License Vote up 6 votes
public static Bitmap getViewBitmap(View comBitmap, int width, int height) {
    Bitmap bitmap = null;
    if (comBitmap != null) {
        comBitmap.clearFocus();
        comBitmap.setPressed(false);

        boolean willNotCache = comBitmap.willNotCacheDrawing();
        comBitmap.setWillNotCacheDrawing(false);

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        int color = comBitmap.getDrawingCacheBackgroundColor();
        comBitmap.setDrawingCacheBackgroundColor(0xffffff);
        float alpha = comBitmap.getAlpha();
        comBitmap.setAlpha(1.0f);

        if (color != 0) {
            comBitmap.destroyDrawingCache();
        }

        int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
        comBitmap.measure(widthSpec, heightSpec);
        comBitmap.layout(comBitmap.getLeft(), comBitmap.getTop(), comBitmap.getLeft()+width, comBitmap.getTop()+height);

        comBitmap.buildDrawingCache();
        Bitmap cacheBitmap = comBitmap.getDrawingCache();
        if (cacheBitmap == null) {
            Log.e("view.ProcessImageToBlur", "failed getViewBitmap(" + comBitmap);
            return null;
        }
        bitmap = Bitmap.createBitmap(cacheBitmap);
        // Restore the view
        comBitmap.setAlpha(alpha);
        comBitmap.destroyDrawingCache();
        comBitmap.setWillNotCacheDrawing(willNotCache);
        comBitmap.setDrawingCacheBackgroundColor(color);
    }
    return bitmap;
}
 
Example 13
Project: boohee_v5.6   File: KPSwitchConflictUtil.java   Source Code and License Vote up 6 votes
public static void hidePanelAndKeyboard(View panelLayout) {
    Activity activity = (Activity) panelLayout.getContext();
    View focusView = activity.getCurrentFocus();
    if (focusView != null) {
        KeyboardUtil.hideKeyboard(activity.getCurrentFocus());
        focusView.clearFocus();
    }
    panelLayout.setVisibility(8);
}
 
Example 14
Project: CXJPadProject   File: ConvertUtils.java   Source Code and License Vote up 5 votes
/**
 * 把view转化为bitmap(截图)
 * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
 */
public static Bitmap toBitmap(View view) {
    int width = view.getWidth();
    int height = view.getHeight();
    if (view instanceof ListView) {
        height = 0;
        // 获取listView实际高度
        ListView listView = (ListView) view;
        for (int i = 0; i < listView.getChildCount(); i++) {
            height += listView.getChildAt(i).getHeight();
        }
    } else if (view instanceof ScrollView) {
        height = 0;
        // 获取scrollView实际高度
        ScrollView scrollView = (ScrollView) view;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            height += scrollView.getChildAt(i).getHeight();
        }
    }
    view.setDrawingCacheEnabled(true);
    view.clearFocus();
    view.setPressed(false);
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    // Reset the drawing cache background color to fully transparent for the duration of this operation
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
    if (color != Color.WHITE) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(cacheBitmap, 0, 0, null);
    canvas.save(Canvas.ALL_SAVE_FLAG);
    canvas.restore();
    if (!bitmap.isRecycled()) {
        LogUtils.verbose("recycle bitmap: " + bitmap.toString());
        bitmap.recycle();
    }
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 15
Project: Renrentou   File: ConvertUtils.java   Source Code and License Vote up 5 votes
/**
 * 把view转化为bitmap(截图)
 * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
 */
public static Bitmap toBitmap(View view) {
    int width = view.getWidth();
    int height = view.getHeight();
    if (view instanceof ListView) {
        height = 0;
        // 获取listView实际高度
        ListView listView = (ListView) view;
        for (int i = 0; i < listView.getChildCount(); i++) {
            height += listView.getChildAt(i).getHeight();
        }
    } else if (view instanceof ScrollView) {
        height = 0;
        // 获取scrollView实际高度
        ScrollView scrollView = (ScrollView) view;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            height += scrollView.getChildAt(i).getHeight();
        }
    }
    view.setDrawingCacheEnabled(true);
    view.clearFocus();
    view.setPressed(false);
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    // Reset the drawing cache background color to fully transparent for the duration of this operation
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
    if (color != Color.WHITE) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(cacheBitmap, 0, 0, null);
    canvas.save(Canvas.ALL_SAVE_FLAG);
    canvas.restore();
    if (!bitmap.isRecycled()) {
       // LogUtils.verbose("recycle bitmap: " + bitmap.toString());
        bitmap.recycle();
    }
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 16
Project: letv   File: KPSwitchFSPanelLayoutHandler.java   Source Code and License Vote up 5 votes
private void saveFocusView(View focusView) {
    this.recordedFocusView = focusView;
    focusView.clearFocus();
    this.panelLayout.setVisibility(8);
}
 
Example 17
Project: PlusGram   File: PasscodeView.java   Source Code and License Vote up 5 votes
public void onShow() {
    Activity parentActivity = (Activity) getContext();
    if (UserConfig.passcodeType == 1) {
        if (passwordEditText != null) {
            passwordEditText.requestFocus();
            AndroidUtilities.showKeyboard(passwordEditText);
        }
    } else {
        if (parentActivity != null) {
            View currentFocus = parentActivity.getCurrentFocus();
            if (currentFocus != null) {
                currentFocus.clearFocus();
                AndroidUtilities.hideKeyboard(((Activity) getContext()).getCurrentFocus());
            }
        }
    }
    checkFingerprint();
    if (getVisibility() == View.VISIBLE) {
        return;
    }
    setAlpha(1.0f);
    setTranslationY(0);
    SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
    int selectedBackground = preferences.getInt("selectedBackground", 1000001);
    if (selectedBackground == 1000001) {
        backgroundFrameLayout.setBackgroundColor(0xff517c9e);
    } else {
        backgroundDrawable = ApplicationLoader.getCachedWallpaper();
        if (backgroundDrawable != null) {
            backgroundFrameLayout.setBackgroundColor(0xbf000000);
        } else {
            backgroundFrameLayout.setBackgroundColor(0xff517c9e);
        }
    }

    passcodeTextView.setText(LocaleController.getString("EnterYourPasscode", R.string.EnterYourPasscode));

    if (UserConfig.passcodeType == 0) {
        //InputFilter[] filterArray = new InputFilter[1];
        //filterArray[0] = new InputFilter.LengthFilter(4);
        //passwordEditText.setFilters(filterArray);
        //passwordEditText.setInputType(InputType.TYPE_CLASS_PHONE);
        //passwordEditText.setFocusable(false);
        //passwordEditText.setFocusableInTouchMode(false);
        numbersFrameLayout.setVisibility(VISIBLE);
        passwordEditText.setVisibility(GONE);
        passwordEditText2.setVisibility(VISIBLE);
        checkImage.setVisibility(GONE);
    } else if (UserConfig.passcodeType == 1) {
        passwordEditText.setFilters(new InputFilter[0]);
        passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        numbersFrameLayout.setVisibility(GONE);
        passwordEditText.setFocusable(true);
        passwordEditText.setFocusableInTouchMode(true);
        passwordEditText.setVisibility(VISIBLE);
        passwordEditText2.setVisibility(GONE);
        checkImage.setVisibility(VISIBLE);
    }
    setVisibility(VISIBLE);
    passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
    passwordEditText.setText("");
    passwordEditText2.eraseAllCharacters(false);

    setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
}
 
Example 18
Project: letv   File: HListView.java   Source Code and License Vote up 5 votes
private boolean arrowScrollImpl(int direction) {
    if (getChildCount() <= 0) {
        return false;
    }
    boolean needToRedraw;
    View focused;
    View selectedView = getSelectedView();
    int selectedPos = this.mSelectedPosition;
    int nextSelectedPosition = lookForSelectablePositionOnScreen(direction);
    int amountToScroll = amountToScroll(direction, nextSelectedPosition);
    ArrowScrollFocusResult focusResult = this.mItemsCanFocus ? arrowScrollFocused(direction) : null;
    if (focusResult != null) {
        nextSelectedPosition = focusResult.getSelectedPosition();
        amountToScroll = focusResult.getAmountToScroll();
    }
    if (focusResult != null) {
        needToRedraw = true;
    } else {
        needToRedraw = false;
    }
    if (nextSelectedPosition != -1) {
        boolean z;
        if (focusResult != null) {
            z = true;
        } else {
            z = false;
        }
        handleNewSelectionChange(selectedView, direction, nextSelectedPosition, z);
        setSelectedPositionInt(nextSelectedPosition);
        setNextSelectedPositionInt(nextSelectedPosition);
        selectedView = getSelectedView();
        selectedPos = nextSelectedPosition;
        if (this.mItemsCanFocus && focusResult == null) {
            focused = getFocusedChild();
            if (focused != null) {
                focused.clearFocus();
            }
        }
        needToRedraw = true;
        checkSelectionChanged();
    }
    if (amountToScroll > 0) {
        if (direction != 33) {
            amountToScroll = -amountToScroll;
        }
        scrollListItemsBy(amountToScroll);
        needToRedraw = true;
    }
    if (this.mItemsCanFocus && focusResult == null && selectedView != null && selectedView.hasFocus()) {
        focused = selectedView.findFocus();
        if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
            focused.clearFocus();
        }
    }
    if (!(nextSelectedPosition != -1 || selectedView == null || isViewAncestorOf(selectedView, this))) {
        selectedView = null;
        hideSelector();
        this.mResurrectToPosition = -1;
    }
    if (!needToRedraw) {
        return false;
    }
    if (selectedView != null) {
        positionSelector(selectedPos, selectedView);
        this.mSelectedLeft = selectedView.getLeft();
    }
    if (!awakenScrollBars()) {
        invalidate();
    }
    invokeOnItemScrollListener();
    return true;
}
 
Example 19
Project: boohee_v5.6   File: KPSwitchFSPanelLayoutHandler.java   Source Code and License Vote up 5 votes
private void saveFocusView(View focusView) {
    this.recordedFocusView = focusView;
    focusView.clearFocus();
    this.panelLayout.setVisibility(8);
}
 
Example 20
Project: EasyAndroid   File: ViewTool.java   Source Code and License Vote up 3 votes
/**
 * 取消view焦点
 *
 * @param view 目标View
 */
public static void clearFocus(View view)
{
    view.clearFocus();
}