Android 在Fragment下控制輸入鍵盤
在Fragment下控制實體返回鍵
在EditTextOverWrite(客制的EditText) 狀態下,會有鍵盤彈出,在這個時候硬體控制權會被系統搶走,因此要加判斷
若該頁面沒有EditText,則無須理會IsKeyboardHidden以及其function
Java部分
- private EditTextOverWrite editEmailLoginAccount,editEmailLoginPassword;
- editEmailLoginAccount = (EditTextOverWrite) view.findViewById(R.id.editEmailLoginAccount);
- editEmailLoginPassword = (EditTextOverWrite) view.findViewById(R.id.editEmailLoginPassword);
- editEmailLoginAccount.setOnKeyboardHidden(onKeyboardHidden);
- editEmailLoginPassword.setOnKeyboardHidden(onKeyboardHidden);
- 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 && KeyEvent.ACTION_UP == event.getAction()) {
- if (IsKeyboardHidden) {
- IsKeyboardHidden = false;
- }else {
- imgEmailLoginBack.performClick();
- }
- return true;
- }
- return false;
- }
- });
- private EditTextOverWrite.OnKeyboardHidden onKeyboardHidden = new EditTextOverWrite.OnKeyboardHidden() {
- @Override
- public void onKeyboardHidden() {
- IsKeyboardHidden = true;
- view.requestFocus();
- }
- };
Xml部分
- <android2017.box.com.sports.Tools.EditTextOverWrite
- android:id="@+id/editEmailLoginAccount"
- style="@style/EditTextStyle"
- android:layout_width="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/textView2"
- android:ems="10"
- android:hint="@string/Enter_the_email_used_when_registering"
- android:inputType="textEmailAddress" />
針對EditTextOverWrite客製化的function
- public class EditTextOverWrite extends EditText {
- private OnKeyboardHidden mOnKeyboardHidden;
- public EditTextOverWrite(Context context) {
- super(context);
- }
- public EditTextOverWrite(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public EditTextOverWrite(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- public EditTextOverWrite(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
- public interface OnKeyboardHidden {
- public void onKeyboardHidden();
- }
- public void setOnKeyboardHidden(OnKeyboardHidden action) {
- mOnKeyboardHidden = action;
- }
- @Override
- public boolean onKeyPreIme(int keyCode, KeyEvent event) {
- // Log.e("keyCode", String.valueOf(keyCode));
- // Log.e("event", String.valueOf(event));
- if (keyCode == KeyEvent.KEYCODE_BACK && KeyEvent.ACTION_UP == event.getAction()) {
- // Log.e("TEST", "OK");
- InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
- clearFocus();
- mOnKeyboardHidden.onKeyboardHidden();
- }
- return super.onKeyPreIme(keyCode, event);
- }
- }
Slot Review: Slot Demo & Casinos at Cobra Casino Web Site
回覆刪除Read our in-depth review of Cobra Casino web site for 강릉 출장안마 more 하남 출장안마 details 밀양 출장마사지 and get instant withdrawal. Click 부천 출장마사지 here for 바카라 details!