下面列出了android.widget.EditText#clearFocus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSharedPreference = getSharedPreferences("htmlnative-demo", MODE_PRIVATE);
String url = mSharedPreference.getString("url", "");
mContainer = (RelativeLayout) findViewById(R.id.relative_view);
mSearch = (EditText) findViewById(R.id.search_editbox);
mSearch.setText(url);
mGo = (ImageButton) findViewById(R.id.search_go_btn);
mGo.setOnClickListener(this);
mLoader = new RemoteViewLoader(this);
mSearch.clearFocus();
}
@NonNull
private View.OnTouchListener getClearListener(final EditText editText) {
return new View.OnTouchListener() {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
int leftEdgeOfRightDrawable = editText.getRight()
- editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width();
// when EditBox has padding, adjust leftEdge like
// leftEdgeOfRightDrawable -= getResources().getDimension(R.dimen.edittext_padding_left_right);
if (event.getRawX() >= leftEdgeOfRightDrawable) {
// clicked on clear icon
editText.setText("");
editText.clearFocus();
return true;
}
}
return false;
}
};
}
@Override
protected void initView()
{
if (!TextUtils.isEmpty(mTitle))
{
setActionBarTitle(mTitle);
}
send_comment = (TextView) findViewById(R.id.send_comment);
et_content = (EditText) findViewById(R.id.et_content);
send_comment.setOnClickListener(this);
et_content.setSelected(false);
et_content.clearFocus();
//ViewUtils.hideSoftInput(this);
//et_content.clearFocus();
}
public void onError(EditText v, int errorCode, String errorMsg) {
X8ToastUtil.showToast(this.mContext, this.contentView.getContext().getString(R.string.x8_fc_exp_error_tip), 0);
double curValue = 0.0d;
int i = v.getId();
if (i == R.id.edt_to_up_down) {
curValue = this.cvToUpDown.getCurValue();
} else if (i == R.id.edt_to_left_right) {
curValue = this.cvToLeftRight.getCurValue();
} else if (i == R.id.edt_to_go_back) {
curValue = this.cvToGoBack.getCurValue();
}
v.setText(String.valueOf(curValue));
v.clearFocus();
}
public void onConfirm(View view) {
String password = mPwdEdit.getText().toString();
if(password.length() < 1) {
Application.showToast(this, R.string.pwd_wrong, Toast.LENGTH_SHORT);
return;
}
if(mStage == SET_PWD) {
EditText et_confirm = findViewById(R.id.confirm);
if(password.equals(et_confirm.getText().toString())) {
et_confirm.clearFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(et_confirm.getWindowToken(), 0);
}
mApp.setPassword(password, true);
new InitTask(this).execute();
}
else {
et_confirm.setText("");
mPwdEdit.setText("");
Application.showToast(this, R.string.pwd_unmatch, Toast.LENGTH_SHORT);
}
}
else {
mApp.setPassword(password, false);
new DecryptTask(mApp.getData(this), mApp.getAppHeaderData(this), this).execute(password);
}
}
public static void onInactive(Context context, EditText et) {
if (et == null) return;
et.clearFocus();
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
/**
* 动态隐藏软键盘
*
* @param context 上下文
* @param edit 输入框
*/
public static void hideSoftInput(Context context, EditText edit) {
edit.clearFocus();
InputMethodManager inputmanger = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
/**
* 动态隐藏软键盘
*/
public static void hideSoftInput(Context context, EditText edit) {
edit.clearFocus();
InputMethodManager inputmanger = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
private void showSoftKeyboard(final EditText view, ViewGroup keypadarea, String defaultInput) {
view.clearFocus();
if (defaultInput != null) {
view.setText(defaultInput);
view.setSelection(defaultInput.length());
}
boolean isnumeric = ((view.getInputType() & InputType.TYPE_CLASS_NUMBER) == InputType.TYPE_CLASS_NUMBER)
|| ((view.getInputType() & InputType.TYPE_CLASS_DATETIME) == InputType.TYPE_CLASS_DATETIME);
int whichkeyboard = Integer.parseInt(appPreferences.getString("keyboard_preference", "1"));
int whichkeypad = Integer.parseInt(appPreferences.getString("keypad_preference", "1"));
boolean gkeyboardAvail = getResources().getBoolean(R.bool.game_keyboard_available);
boolean gkeypadAvail = getResources().getBoolean(R.bool.game_keypad_available);
if (isnumeric && gkeypadAvail && whichkeypad == 1 && keypadarea != null) {
Keyboard.showNumberpad(this, view, keypadarea, mNumpadkeyMap);
} else if (!isnumeric && gkeyboardAvail && whichkeyboard == 1 && keypadarea != null) {
Keyboard.showKeyboard(this, view, keypadarea, mKeyboardkeyMap);
} else if (whichkeypad == 2 || whichkeyboard == 2 || (isnumeric && !gkeypadAvail) || (!isnumeric && !gkeyboardAvail)) {
showSystemKeyboard(view);
}
}
public static void onInactive(Context context, EditText et) {
if (et == null)
return;
et.clearFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
/**
* requests focus on a {@link EditText} allowing to see its title too. It is used to set focus
* on the first question with error.
*
* @param et field we want to set focus in.
* @param tv_id id of the {@link TextView} which is the title of et.
*/
private void focusFirstError(EditText et, int tv_id) {
et.clearFocus(); // requestRectangle does not work properly if et is already focused
et.requestFocus();
TextView title = (TextView) findViewById(tv_id);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) title.getLayoutParams();
Rect rect = new Rect(0, -lp.topMargin, title.getWidth(), title.getHeight());
title.requestRectangleOnScreen(rect);
}
/**
* requests focus on a {@link EditText} allowing to see its title too. It is used to set focus on the
* first question with error.
*
* @param et field we want to set focus in.
* @param tv_id id of the {@link TextView} which is the title of et.
*/
private void focusFirstError(EditText et, int tv_id) {
et.clearFocus(); // requestRectangle does not work properly if et is focused
et.requestFocus();
TextView title = (TextView) findViewById(tv_id);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) title.getLayoutParams();
Rect rect = new Rect(0, -lp.topMargin, title.getWidth(), title.getHeight());
title.requestRectangleOnScreen(rect);
}
private void updateRegistrationID(String value) {
EditText editRegistrationID = findViewById(R.id.editRegistrationID);
editRegistrationID.setText(value);
editRegistrationID.clearFocus();
}
private void updatePushToken(String value) {
EditText editPushToken = findViewById(R.id.editPushToken);
editPushToken.setText(value);
editPushToken.clearFocus();
}
@Override
protected void onCreate(Bundle arg0) {
setTheme(App.theme.theme);
super.onCreate(arg0);
setContentView(R.layout.activity_file_editor);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Bundle b = getIntent().getExtras();
if (b == null) {
finish();
return;
}
Picasso.with(this).load(b.<Uri>getParcelable(PreferencesFragment.ARG_ICON_URI)).error(R.drawable.ic_launcher).into((android.widget.ImageView) findViewById(android.R.id.home));
mFile = b.getString(PreferencesFragment.ARG_FILE);
mTitle = Utils.extractFileName(mFile);
mPackageName = b.getString(PreferencesFragment.ARG_PACKAGE_NAME);
mEditText = (EditText) findViewById(R.id.editText);
//Hack to prevent EditText to request focus when the Activity is created
mEditText.post(new Runnable() {
@Override
public void run() {
mEditText.setFocusable(true);
mEditText.setFocusableInTouchMode(true);
}
});
if (arg0 == null) {
mEditText.setText(Utils.readFile(mFile));
mColorTheme = ColorThemeEnum.valueOf(PreferenceManager.getDefaultSharedPreferences(this).getString(KEY_COLOR_THEME, ColorThemeEnum.ECLIPSE.name()));
setXmlFontSize(XmlFontSize.generateSize(PreferenceManager.getDefaultSharedPreferences(this).getInt(KEY_FONT_SIZE, XmlFontSize.MEDIUM.getSize())));
} else {
mHasContentChanged = arg0.getBoolean(KEY_HAS_CONTENT_CHANGED, false);
mNeedUpdateOnActivityFinish = arg0.getBoolean(KEY_NEED_UPDATE_ON_ACTIVITY_FINISH, false);
if (mNeedUpdateOnActivityFinish) {
setResult(RESULT_OK);
}
mColorTheme = ColorThemeEnum.valueOf(arg0.getString(KEY_COLOR_THEME));
setXmlFontSize(XmlFontSize.generateSize(arg0.getInt(KEY_FONT_SIZE)));
}
mXmlColorTheme = XmlColorTheme.createTheme(getResources(), mColorTheme);
updateTitle();
invalidateOptionsMenu();
highlightXMLText(mEditText.getText());
mEditText.clearFocus();
}
/**
* Hide the keyboard
* @param context
* @param editText
*/
public static void hideKeyBoard(Context context,EditText editText){
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
editText.clearFocus();
}
public void closeDash ()
{
if (! this.isDashOpened) {
return;
}
if (modeCustomise)
{
Intent intent = this.getIntent ();
intent.putExtra ("customise", false);
this.finish ();
this.startActivity (intent);
return;
}
EditText etDashSearch = this.viewFinder.get(R.id.etDashSearch);
this.llDash.setVisibility (View.GONE);
this.wpWallpaper.unblur ();
etDashSearch.setText ("");
etDashSearch.clearFocus ();
if (this.getResources ().getInteger (HomeActivity.theme.panel_close_location) != -1)
this.ibPanelDashClose.setVisibility (View.INVISIBLE);
SharedPreferences prefs = this.getSharedPreferences ();
this.llPanel.setAlpha ((float) prefs.getInt (Preference.PANEL_OPACITY.getName(), 100) / 100F);
if (this.getResources ().getBoolean (HomeActivity.theme.panel_background_dynamic_when_dash_opened))
{
this.llPanel.setBackgroundResource (HomeActivity.theme.panel_background);
if (Build.VERSION.SDK_INT >= 19)
{
LinearLayout llStatusBar = this.viewFinder.get(R.id.llStatusBar);
llStatusBar.setBackgroundColor (this.getResources ().getColor (android.R.color.black));
}
}
InputMethodManager imm = (InputMethodManager) this.getSystemService (Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow (this.getWindow ().getDecorView ().getRootView ().getWindowToken (), 0);
this.flWallpaperOverlay.setVisibility (View.VISIBLE);
this.flWallpaperOverlayWhenDashOpened.setVisibility (View.INVISIBLE);
this.isDashOpened = false;
}
public static void dismissSoftKeyboard(Context context, EditText editText) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
editText.clearFocus();
}
/**
* 针对于EditText ,失去焦点,隐藏软件盘
*
* @param edit
*/
public void hideKeyboard(EditText edit) {
edit.clearFocus();
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
/**
* 关闭软键盘
*
* @param mEditText 输入框
* @param context 上下文
*/
public static void closeKeybord(EditText mEditText, Context context) {
mEditText.clearFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}