diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 19373ea..fdce8b8 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,9 +3,8 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index f53e639..8f18726 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index b780b88..0ef6143 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,10 +2,10 @@ apply plugin: 'com.android.application' android { compileSdkVersion 26 - buildToolsVersion "26.0.2" + buildToolsVersion "26.0.3" defaultConfig { applicationId "com.example.keybord.keyborddemo" - minSdkVersion 19 + minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" @@ -23,6 +23,5 @@ dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support:recyclerview-v7:26.1.0' - implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation project(':easykeyboard') } diff --git a/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardActivity.java b/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardActivity.java index a4962f1..293732f 100644 --- a/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardActivity.java +++ b/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardActivity.java @@ -2,30 +2,31 @@ import android.graphics.Color; import android.graphics.Paint; -import android.inputmethodservice.Keyboard; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; +import android.text.Editable; +import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.jay.easykeyboard.SystemKeyboard; -import com.jay.easykeyboard.function.SystemOnKeyboardActionListener; +import com.jay.easykeyboard.action.IKeyBoardUI; +import com.jay.easykeyboard.action.KeyBoardActionListence; +import com.jay.easykeyboard.util.Util; /** * Created by huangjie on 2018/2/6. * 类名: - * 说明: + * 说明:此Activity实现了键盘固定的情况下的使用 */ public class SystemKeyboardActivity extends AppCompatActivity implements View.OnFocusChangeListener { + private static final String TAG = "SystemKeyboardActivity"; private SystemKeyboard mKeyboard; - private SystemOnKeyboardActionListener listener; - private EditText edit1; - private EditText edit2; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -33,20 +34,30 @@ public void onCreate(@Nullable Bundle savedInstanceState) { setContentView(R.layout.activity_systemkeyboard); mKeyboard = findViewById(R.id.systemkeyboard); Button btn_setkeyui = findViewById(R.id.btn_setkeyui); - edit1 = findViewById(R.id.edit); - edit2 = findViewById(R.id.edit2); - listener = new SystemOnKeyboardActionListener() { + EditText edit1 = findViewById(R.id.edit); + EditText edit2 = findViewById(R.id.edit2); + mKeyboard.setEditText(edit1); //用于绑定EditText,如果切换了EditText,请务必设置此方法 + mKeyboard.setOnKeyboardActionListener(new KeyBoardActionListence() { @Override - public void onKey(int primaryCode, int[] keyCodes) { - //必须实现该方法,其余方法可选择实现,该方法的作用是将输入的值附到EditText上,同时进行键盘按键监听 - super.onKey(primaryCode, keyCodes); - if (primaryCode == Keyboard.KEYCODE_DONE) { - showShortToast("点击了完成按钮"); - } + public void onComplete() { + showShortToast("完成"); } - }; - listener.setEditText(edit1); //用于绑定EditText,如果切换了EditText,请务必设置此方法 - mKeyboard.setOnKeyboardActionListener(listener); + + @Override + public void onTextChange(Editable editable) { + Log.i(TAG,"onTextChange:"+editable.toString()); + } + + @Override + public void onClear() { + showShortToast("onClear"); + } + + @Override + public void onClearAll() { + showShortToast("onClearAll"); + } + }); edit1.setOnFocusChangeListener(this); edit2.setOnFocusChangeListener(this); @@ -54,24 +65,19 @@ public void onKey(int primaryCode, int[] keyCodes) { btn_setkeyui.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - mKeyboard.setKeyboardUI(new SystemKeyboard.KeyUI() { + //改变ui + mKeyboard.setKeyboardUI(new IKeyBoardUI() { @Override - public Paint paintConfig(Paint mPaint) { //更新Ui,可用画笔设置字体大小,颜色等参数 - mPaint.setColor(Color.BLUE); - mPaint.setTextSize(100); - return mPaint; + public Paint setPaint(Paint paint) { + paint.setColor(Color.BLUE); + paint.setTextSize(Util.dpToPx(getApplicationContext(),16)); + return paint; } }); } }); } - @Override - protected void onDestroy() { - super.onDestroy(); - mKeyboard.recycle(); //回收 - } - private void showShortToast(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } @@ -79,8 +85,14 @@ private void showShortToast(String str) { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { - if (v instanceof EditText) - listener.setEditText((EditText) v); + switch (v.getId()){ + case R.id.edit: //绑定EditText并显示自定义键盘 + mKeyboard.setEditText((EditText) v); + break; + case R.id.edit2: //绑定EditText并显示原生键盘 + mKeyboard.setEditText((EditText) v,true); + break; + } } } } diff --git a/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardEidtTextActivity.java b/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardEidtTextActivity.java index 4839966..6caa5e3 100644 --- a/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardEidtTextActivity.java +++ b/app/src/main/java/com/example/keybord/keyborddemo/SystemKeyboardEidtTextActivity.java @@ -4,12 +4,14 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; +import android.text.Editable; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.jay.easykeyboard.SystemKeyBoardEditText; -import com.jay.easykeyboard.function.SystemOnKeyboardActionListener; +import com.jay.easykeyboard.action.KeyBoardActionListence; +import com.jay.easykeyboard.impl.SystemOnKeyboardActionListener; /** * Created by huangjie on 2018/2/6. @@ -17,11 +19,10 @@ * 说明: */ -public class SystemKeyboardEidtTextActivity extends AppCompatActivity implements View.OnFocusChangeListener{ +public class SystemKeyboardEidtTextActivity extends AppCompatActivity implements KeyBoardActionListence { private SystemKeyBoardEditText skb_top; private SystemKeyBoardEditText skb_bottom; - private SystemOnKeyboardActionListener listener; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -29,19 +30,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) { setContentView(R.layout.activity_systemkeyboardedittext); skb_top = findViewById(R.id.skb_top); skb_bottom = findViewById(R.id.skb_bottom); - listener = new SystemOnKeyboardActionListener(skb_top,skb_top.getKeyboardWindow()){ - @Override - public void onKey(int primaryCode, int[] keyCodes) { - super.onKey(primaryCode, keyCodes); - if (primaryCode== Keyboard.KEYCODE_DONE){ - showShortToast("点击了完成按钮"); - } - } - }; - skb_top.setOnKeyboardActionListener(listener); - skb_bottom.setOnKeyboardActionListener(listener); - skb_top.setOnFocusChangeListener(this); - skb_bottom.setOnFocusChangeListener(this); + skb_top.setOnKeyboardActionListener(this); + skb_bottom.setOnKeyboardActionListener(this); +// skb_top.setOnFocusChangeListener(this); +// skb_bottom.setOnFocusChangeListener(this); } @@ -49,13 +41,32 @@ private void showShortToast(String str){ Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } +// @Override +// public void onFocusChange(View v, boolean hasFocus) { +// if (hasFocus){ +// if (v instanceof SystemKeyBoardEditText){ +// ()v.setEditText((EditText) v); +// } +// } +// } + + @Override + public void onComplete() { + + } + + @Override + public void onTextChange(Editable editable) { + + } + @Override - public void onFocusChange(View v, boolean hasFocus) { - if (hasFocus){ - if (v instanceof SystemKeyBoardEditText){ - listener.setEditText((EditText) v); - listener.setPopupWindow(((SystemKeyBoardEditText) v).getKeyboardWindow()); - } - } + public void onClear() { + + } + + @Override + public void onClearAll() { + } } diff --git a/easykeyboard/src/main/res/drawable/btn_keyboard_key.xml b/app/src/main/res/drawable/btn_keyboard_key.xml similarity index 100% rename from easykeyboard/src/main/res/drawable/btn_keyboard_key.xml rename to app/src/main/res/drawable/btn_keyboard_key.xml diff --git a/app/src/main/res/drawable/edit_bg.xml b/app/src/main/res/drawable/edit_bg.xml index f0440f9..649504e 100644 --- a/app/src/main/res/drawable/edit_bg.xml +++ b/app/src/main/res/drawable/edit_bg.xml @@ -1,6 +1,4 @@ - - + + \ No newline at end of file diff --git a/easykeyboard/src/main/res/drawable/no_pre.xml b/app/src/main/res/drawable/no_pre.xml similarity index 100% rename from easykeyboard/src/main/res/drawable/no_pre.xml rename to app/src/main/res/drawable/no_pre.xml diff --git a/easykeyboard/src/main/res/drawable/pre.xml b/app/src/main/res/drawable/pre.xml similarity index 100% rename from easykeyboard/src/main/res/drawable/pre.xml rename to app/src/main/res/drawable/pre.xml diff --git a/app/src/main/res/layout/activity_recycle_view.xml b/app/src/main/res/layout/activity_recycle_view.xml index 92cca80..23a2882 100644 --- a/app/src/main/res/layout/activity_recycle_view.xml +++ b/app/src/main/res/layout/activity_recycle_view.xml @@ -1,13 +1,11 @@ + > + /> diff --git a/app/src/main/res/layout/activity_systemkeyboard.xml b/app/src/main/res/layout/activity_systemkeyboard.xml index 0fb9ff6..b1a0fa0 100644 --- a/app/src/main/res/layout/activity_systemkeyboard.xml +++ b/app/src/main/res/layout/activity_systemkeyboard.xml @@ -35,6 +35,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" - keyboard:keyViewbg="@drawable/edit_bg" + keyboard:keyViewbg="@drawable/btn_keyboard_key" keyboard:xmlLayoutResId="@xml/keyboard_numbers" /> diff --git a/app/src/main/res/layout/activity_systemkeyboardedittext.xml b/app/src/main/res/layout/activity_systemkeyboardedittext.xml index a0cbfbd..5e9750f 100644 --- a/app/src/main/res/layout/activity_systemkeyboardedittext.xml +++ b/app/src/main/res/layout/activity_systemkeyboardedittext.xml @@ -8,10 +8,11 @@ android:layout_width="match_parent" android:layout_height="50dp" keyboard:xmlLayoutResId="@xml/keyboard_numbers" + keyboard:outSideCancel="true" android:id="@+id/skb_top" android:hint="顶部EditText" android:layout_margin="10dp" - android:background="@drawable/edit_bg" + android:background="@drawable/btn_keyboard_key" /> diff --git a/easykeyboard/build.gradle b/easykeyboard/build.gradle index 455bf87..3d57161 100644 --- a/easykeyboard/build.gradle +++ b/easykeyboard/build.gradle @@ -5,7 +5,7 @@ android { compileSdkVersion 26 defaultConfig { - minSdkVersion 19 + minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyBoardEditText.java b/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyBoardEditText.java index 164b279..c731f8f 100644 --- a/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyBoardEditText.java +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyBoardEditText.java @@ -1,17 +1,21 @@ package com.jay.easykeyboard; +import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; +import android.os.Handler; import android.util.AttributeSet; -import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; +import android.widget.EditText; -import com.jay.easykeyboard.base.KeyBoardEditText; -import com.jay.easykeyboard.constant.Util; -import com.jay.easykeyboard.function.FormatTextWatcher; -import com.jay.easykeyboard.function.SystemOnKeyboardActionListener; +import com.jay.easykeyboard.action.KeyBoardActionListence; +import com.jay.easykeyboard.action.OnEditFocusChangeListence; +import com.jay.easykeyboard.widget.KeyBoardEditText; +import com.jay.easykeyboard.util.Util; +import com.jay.easykeyboard.impl.FormatTextWatcher; +import com.jay.easykeyboard.impl.SystemOnKeyboardActionListener; /** * Created by huangjie on 2018/2/4. @@ -22,10 +26,17 @@ public class SystemKeyBoardEditText extends KeyBoardEditText { private boolean enable = true; //是否启用自定义键盘 private boolean focuable = true; //默认获取焦点 - private boolean release = true; //主动回收 + private boolean outSideable = false; //点击外部区域是否隐藏键盘 private SystemKeyboard systemKeyboard; private FormatTextWatcher textWatcher; private SystemOnKeyboardActionListener listener; + private OnEditFocusChangeListence focusChangeListence; + private int FOCUSTAB; + + private static final int READY = 0X110; + private static final int STAR = 0X111; + private int STATUE; + private Handler mHandler = new Handler(); public SystemKeyBoardEditText(Context context) { this(context, null); @@ -38,42 +49,69 @@ public SystemKeyBoardEditText(Context context, AttributeSet attrs) { public SystemKeyBoardEditText(final Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); + initListence(); + } + + private void initListence() { setOnTouchListener(new OnTouchListener() { + @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View v, MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_DOWN) { - if (focuable) { - requestFocus(); - requestFocusFromTouch(); - if (enable) { - Util.closeKeyboard(context, SystemKeyBoardEditText.this); + if (!isShowing()) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + if (focuable) { + requestFocus(); + requestFocusFromTouch(); + if (enable) { + Util.disableShowSoftInput(SystemKeyBoardEditText.this); showKeyboardWindow(); + } + } else { + dismissKeyboardWindow(); + Util.disableShowSoftInput(SystemKeyBoardEditText.this); } - } else { - dismissKeyboardWindow(); - Util.closeKeyboard(context, SystemKeyBoardEditText.this); } + } else { + requestFocus(); + requestFocusFromTouch(); } - return true; + return false; } }); - + STATUE = READY; setOnFocusChangeListener(new OnFocusChangeListener() { @Override - public void onFocusChange(View v, boolean hasFocus) { - if (!hasFocus){ - dismissKeyboardWindow(); + public void onFocusChange(View v, final boolean hasFocus) { + //根据焦点变化判断外部点击区域 + FOCUSTAB++; + if (STATUE == READY) { + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + if (STATUE == STAR) { + if (FOCUSTAB == 1 && !hasFocus && outSideable) { + dismissKeyboardWindow(); + } + STATUE = READY; + FOCUSTAB = 0; + } + } + }, 200); + STATUE = STAR; + } + if (focusChangeListence!=null){ + focusChangeListence.OnFocusChangeListener(v,hasFocus); } } }); } - private void init(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SystemKeyBoardEditText); // boolean randomkeys = a.getBoolean(R.styleable.SystemKeyBoardEditText_randomkeys, false); int xmlLayoutResId = a.getResourceId(R.styleable.SystemKeyBoardEditText_xmlLayoutResId, 0); boolean isSpace = a.getBoolean(R.styleable.SystemKeyBoardEditText_space, false); + outSideable = a.getBoolean(R.styleable.SystemKeyBoardEditText_outSideCancel,false); systemKeyboard = new SystemKeyboard(context); systemKeyboard.setXmlLayoutResId(xmlLayoutResId); // systemKeyboard.isRandomkeys(randomkeys); @@ -82,12 +120,9 @@ private void init(Context context, AttributeSet attrs) { systemKeyboard.setKeybgDrawable(keyViewbg); } initPopWindow(systemKeyboard); - listener = new SystemOnKeyboardActionListener(this, getKeyboardWindow()) { - @Override - public void onKey(int primaryCode, int[] keyCodes) { - super.onKey(primaryCode, keyCodes); - } - }; + listener = new SystemOnKeyboardActionListener(); + listener.setEditText(this); + listener.setPopupWindow(getKeyboardWindow()); systemKeyboard.getKeyboardView().setOnKeyboardActionListener(listener); if (isSpace) { textWatcher = new FormatTextWatcher(this); @@ -97,42 +132,15 @@ public void onKey(int primaryCode, int[] keyCodes) { a.recycle(); } - @Override - protected void onDetachedFromWindow() { - super.onDetachedFromWindow(); - if (release) recycle(); - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK) { - if (null != getKeyboardWindow()) { - if (getKeyboardWindow().isShowing()) { - getKeyboardWindow().dismiss(); - return true; - } - } - } - return super.onKeyDown(keyCode, event); - } - - - public void recycle() { - super.recycle(); - systemKeyboard = null; - if (null != textWatcher) { - removeTextChangedListener(textWatcher); - } - } - - public void setActiveRelease(boolean release) { - this.release = release; - } public SystemKeyboard getSystemKeyboard() { return systemKeyboard; } + /** + * 是否加入4位空格功能 + * @param isSpace true + */ public void setSpaceEnable(boolean isSpace) { if (isSpace) { if (textWatcher == null) { @@ -146,19 +154,19 @@ public void setSpaceEnable(boolean isSpace) { } } -// public void setRandomkeysEnable(boolean randomkeys) { -// systemKeyboard.isRandomkeys(randomkeys); -// } - public void setOnKeyboardActionListener(SystemOnKeyboardActionListener listener) { - this.listener = listener; - systemKeyboard.getKeyboardView().setOnKeyboardActionListener(listener); + /** + * 设置键盘输入监听 + * @param listener listence + */ + public void setOnKeyboardActionListener(KeyBoardActionListence listener) { + this.listener.setKeyActionListence(listener); } /** * 设置焦点 * - * @param focuable + * @param focuable focuable */ public void setFocuable(boolean focuable) { this.focuable = focuable; @@ -167,8 +175,33 @@ public void setFocuable(boolean focuable) { setCursorVisible(focuable); } + + /** + * 焦点监听 + * @param focusChangeListence listence + */ + public void setFocusChangeListence(OnEditFocusChangeListence focusChangeListence) { + this.focusChangeListence = focusChangeListence; + } + + /** + * 设置键盘背景 可以用此背景设置线条粗细 + * @param drawable drawable + */ public void setKeyViewBgDrawable(Drawable drawable) { if (systemKeyboard != null) systemKeyboard.setKeybgDrawable(drawable); } + /** + * 绑定EditText + * + * @param editText editText + */ + public void setEditText(EditText editText) { + listener.setEditText(editText); + if (editText instanceof SystemKeyBoardEditText) { + listener.setPopupWindow(((SystemKeyBoardEditText) editText).getKeyboardWindow()); + } + } + } diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyboard.java b/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyboard.java index f58751d..2fb9e07 100644 --- a/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyboard.java +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/SystemKeyboard.java @@ -1,21 +1,21 @@ package com.jay.easykeyboard; -import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; -import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.inputmethodservice.Keyboard; -import android.inputmethodservice.KeyboardView; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.EditText; import android.widget.FrameLayout; -import com.jay.easykeyboard.constant.Util; -import com.jay.easykeyboard.function.SystemOnKeyboardActionListener; +import com.jay.easykeyboard.action.IKeyBoardUI; +import com.jay.easykeyboard.action.KeyBoardActionListence; +import com.jay.easykeyboard.impl.SystemOnKeyboardActionListener; import com.jay.easykeyboard.keyboard.MyKeyboardView; +import com.jay.easykeyboard.util.Util; /** * Created by huangjie on 2018/2/3. @@ -28,10 +28,7 @@ public class SystemKeyboard extends FrameLayout { private MyKeyboardView keyboardView; private Drawable keybgDrawable; private Keyboard mKeyboard; - - public interface KeyUI { - Paint paintConfig(Paint mPaint); - } + private SystemOnKeyboardActionListener actionListener; public SystemKeyboard(Context context) { this(context, null); @@ -48,18 +45,13 @@ public SystemKeyboard(Context context, @Nullable AttributeSet attrs, int defStyl private void init(Context context, @Nullable AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SystemKeyboard); + if (a.hasValue(R.styleable.SystemKeyboard_keyViewbg)) { + keybgDrawable = a.getDrawable(R.styleable.SystemKeyboard_keyViewbg); + } if (a.hasValue(R.styleable.SystemKeyboard_xmlLayoutResId)) { int xmlLayoutResId = a.getResourceId(R.styleable.SystemKeyboard_xmlLayoutResId, 0); initKeyBoard(context, xmlLayoutResId); } - if (a.hasValue(R.styleable.SystemKeyboard_keyViewbg)) - keybgDrawable = a.getDrawable(R.styleable.SystemKeyboard_keyViewbg); -// if (a.hasValue(R.styleable.SystemKeyboard_randomkeys)) { -// boolean randomkeys = a.getBoolean(R.styleable.SystemKeyboard_randomkeys, false); -// if (randomkeys) { -// randomdigkey(); -// } -// } a.recycle(); } @@ -72,7 +64,8 @@ private void initKeyBoard(Context context, int xmlLayoutResId) { if (null != keybgDrawable) { keyboardView.setKeybgDrawable(keybgDrawable); } - setOnKeyboardActionListener(new SystemOnKeyboardActionListener()); + actionListener = new SystemOnKeyboardActionListener(); + keyboardView.setOnKeyboardActionListener(actionListener); this.addView(keyboardView); } @@ -122,20 +115,6 @@ public Keyboard getKeyboard() { return mKeyboard; } - public void UserNativeKeyboard(boolean isUser) { - if (isUser) setVisibility(GONE); - else setVisibility(VISIBLE); - } - - public void UserNativeKeyboard(boolean isUser, Activity activity, EditText editText) { - if (isUser) { - setVisibility(GONE); - Util.openKeyboard(activity, editText); - } else { - setVisibility(VISIBLE); - Util.hideKeyboard(activity); - } - } public void setXmlLayoutResId(int xmlLayoutResId) { initKeyBoard(getContext(), xmlLayoutResId); @@ -151,21 +130,41 @@ public void setKeybgDrawable(Drawable keybgDrawable) { keyboardView.setKeybgDrawable(keybgDrawable); } - public void setOnKeyboardActionListener(KeyboardView.OnKeyboardActionListener onKeyboardActionListener){ - if (onKeyboardActionListener!=null) - keyboardView.setOnKeyboardActionListener(onKeyboardActionListener); + + /** + * 建立与EditText的绑定关系,用于控制输入值 + * + * @param editText 绑定EditText 默认显示自定义键盘 + */ + public void setEditText(@NonNull EditText editText) { + setEditText(editText, false); } - public void setKeyboardUI(KeyUI ui) { - if (null != ui) { - keyboardView.setPaint(ui.paintConfig(keyboardView.getPaint())); + /** + * 建立与EditText的绑定关系,用于控制输入值 + * + * @param editText 需要绑定的EditText + * @param isOpenNativeKeyBoard 是否打开原生键盘 + */ + public void setEditText(@NonNull EditText editText, boolean isOpenNativeKeyBoard) { + actionListener.setEditText(editText); + if (isOpenNativeKeyBoard) { + Util.showKeyboard(editText); + setVisibility(GONE); + } else { + setVisibility(VISIBLE); + Util.disableShowSoftInput(editText); + Util.hideKeyboard(editText.getContext()); } } + public void setOnKeyboardActionListener(KeyBoardActionListence listener) { + actionListener.setKeyActionListence(listener); + } - public void recycle() { - keyboardView.setOnKeyboardActionListener(null); - keyboardView = null; - mKeyboard = null; + public void setKeyboardUI(IKeyBoardUI ui) { + if (null != ui) { + keyboardView.setPaint(ui.setPaint(keyboardView.getPaint())); + } } } diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/action/IKeyBoardUI.java b/easykeyboard/src/main/java/com/jay/easykeyboard/action/IKeyBoardUI.java new file mode 100644 index 0000000..d30d3d6 --- /dev/null +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/action/IKeyBoardUI.java @@ -0,0 +1,11 @@ +package com.jay.easykeyboard.action; + +import android.graphics.Paint; + +/** + * Created by hj on 2018/12/17. + * 说明:ui样式接口实现 + */ +public interface IKeyBoardUI { + Paint setPaint(Paint paint); +} diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/action/KeyBoardActionListence.java b/easykeyboard/src/main/java/com/jay/easykeyboard/action/KeyBoardActionListence.java new file mode 100644 index 0000000..3887728 --- /dev/null +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/action/KeyBoardActionListence.java @@ -0,0 +1,17 @@ +package com.jay.easykeyboard.action; + +import android.text.Editable; + +/** + * Created by hj on 2018/12/17. + * 说明:键盘输入监听 + */ +public interface KeyBoardActionListence { + void onComplete(); //完成点击 + + void onTextChange(Editable editable); //文本改变 + + void onClear(); //正在删除 + + void onClearAll(); //全部清除 +} diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/action/OnEditFocusChangeListence.java b/easykeyboard/src/main/java/com/jay/easykeyboard/action/OnEditFocusChangeListence.java new file mode 100644 index 0000000..21cdfd6 --- /dev/null +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/action/OnEditFocusChangeListence.java @@ -0,0 +1,11 @@ +package com.jay.easykeyboard.action; + +import android.view.View; + +/** + * Created by hj on 2018/12/17. + * 说明:由于使用了OnFocusChangeListener接口,如果项目中需要使用到可以使用这个接口 + */ +public interface OnEditFocusChangeListence { + void OnFocusChangeListener(View v, boolean hasFocus); +} diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/function/FormatTextWatcher.java b/easykeyboard/src/main/java/com/jay/easykeyboard/impl/FormatTextWatcher.java similarity index 90% rename from easykeyboard/src/main/java/com/jay/easykeyboard/function/FormatTextWatcher.java rename to easykeyboard/src/main/java/com/jay/easykeyboard/impl/FormatTextWatcher.java index 6fe2dc0..939b763 100644 --- a/easykeyboard/src/main/java/com/jay/easykeyboard/function/FormatTextWatcher.java +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/impl/FormatTextWatcher.java @@ -1,4 +1,4 @@ -package com.jay.easykeyboard.function; +package com.jay.easykeyboard.impl; import android.text.Editable; import android.text.TextWatcher; @@ -7,7 +7,7 @@ /** * Created by huangjie on 2018/2/5. * 类名: - * 说明: + * 说明:4位空格实现类 */ public class FormatTextWatcher implements TextWatcher { @@ -22,10 +22,11 @@ public FormatTextWatcher(EditText editText){ private int unit = 4; //间隔符 private String tag = " "; - int beforeTextLength = 0; - int afterTextLength = 0; - int location = 0;//记录光标的位置 - boolean isChanging = false;// 是否更换中 + private int beforeTextLength = 0; + private int afterTextLength = 0; + private int location = 0;//记录光标的位置 + private boolean isChanging = false;// 是否更换中 + @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { beforeTextLength = s.length(); @@ -104,7 +105,7 @@ private String addTag(String str) { * @param str * @return */ - public String replaceTag(String str) { + private String replaceTag(String str) { if (str.contains(tag)) { str = str.replace(tag, ""); } diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/function/SystemOnKeyboardActionListener.java b/easykeyboard/src/main/java/com/jay/easykeyboard/impl/SystemOnKeyboardActionListener.java similarity index 76% rename from easykeyboard/src/main/java/com/jay/easykeyboard/function/SystemOnKeyboardActionListener.java rename to easykeyboard/src/main/java/com/jay/easykeyboard/impl/SystemOnKeyboardActionListener.java index b0f0d6c..c52d66b 100644 --- a/easykeyboard/src/main/java/com/jay/easykeyboard/function/SystemOnKeyboardActionListener.java +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/impl/SystemOnKeyboardActionListener.java @@ -1,32 +1,27 @@ -package com.jay.easykeyboard.function; +package com.jay.easykeyboard.impl; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.text.Editable; +import android.util.Log; import android.widget.EditText; import android.widget.PopupWindow; +import com.jay.easykeyboard.action.KeyBoardActionListence; + +import static android.inputmethodservice.Keyboard.KEYCODE_DONE; + /** * Created by huangjie on 2018/2/3. * 类名: - * 说明:实现该类可单独实现需要的方法,内部实现内容添加 + * 说明:键盘原生接口实现类,可继承重写 */ public class SystemOnKeyboardActionListener implements KeyboardView.OnKeyboardActionListener { private EditText editText; private PopupWindow popupWindow; - - public SystemOnKeyboardActionListener(){}; - - public SystemOnKeyboardActionListener(EditText editText) { - this.editText = editText; - } - - public SystemOnKeyboardActionListener(EditText editText, PopupWindow popupWindow) { - this.editText = editText; - this.popupWindow = popupWindow; - } + private KeyBoardActionListence listence; public void setEditText(EditText editText) { this.editText = editText; @@ -36,14 +31,16 @@ public void setPopupWindow(PopupWindow popupWindow) { this.popupWindow = popupWindow; } + public void setKeyActionListence(KeyBoardActionListence listence){ + this.listence = listence; + } + @Override public void onPress(int primaryCode) { - } @Override public void onRelease(int primaryCode) { - } @Override @@ -55,42 +52,41 @@ public void onKey(int primaryCode, int[] keyCodes) { if (null != popupWindow && popupWindow.isShowing()) { popupWindow.dismiss(); } + listence.onComplete(); } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退 if (editable != null && editable.length() > 0) { if (start > 0) { editable.delete(start - 1, start); } } + listence.onClear(); } else if (primaryCode == 152) { editable.clear(); - } else { + listence.onClearAll(); + }else { editable.insert(start, Character.toString((char) primaryCode)); + listence.onTextChange(editable); } } } @Override public void onText(CharSequence text) { - } @Override public void swipeLeft() { - } @Override public void swipeRight() { - } @Override public void swipeDown() { - } @Override public void swipeUp() { - } } diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/keyboard/MyKeyboardView.java b/easykeyboard/src/main/java/com/jay/easykeyboard/keyboard/MyKeyboardView.java index 588bf11..3a53c5f 100644 --- a/easykeyboard/src/main/java/com/jay/easykeyboard/keyboard/MyKeyboardView.java +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/keyboard/MyKeyboardView.java @@ -9,12 +9,9 @@ import android.inputmethodservice.Keyboard.Key; import android.inputmethodservice.KeyboardView; import android.util.AttributeSet; - -import com.jay.easykeyboard.R; - import java.util.List; - +//核心类,承担绘制工作 public class MyKeyboardView extends KeyboardView { private Drawable mKeybgDrawable; @@ -32,8 +29,7 @@ public MyKeyboardView(Context context, AttributeSet attrs, int defStyleAttr) { } private void initResources(Context context) { - Resources res = context.getResources(); - mKeybgDrawable = res.getDrawable(R.drawable.btn_keyboard_key); + Resources res = context.getResources(); rect = new Rect(); paint = new Paint(); paint.setAntiAlias(true); @@ -53,6 +49,9 @@ public Paint getPaint(){ public void setPaint(Paint paint){ this.paint = paint; + if (paint==null){ + throw new NullPointerException("Paint is not null"); + } invalidate(); } @@ -67,15 +66,19 @@ public void onDraw(Canvas canvas) { offsety = 1; } int initdrawy = key.y + offsety; - rect.left = key.x; - rect.top = initdrawy; - rect.right = key.x+key.width; - rect.bottom = key.y+key.height; - canvas.clipRect(rect); - int[] state = key.getCurrentDrawableState(); - mKeybgDrawable.setState(state); - mKeybgDrawable.setBounds(rect); - mKeybgDrawable.draw(canvas); + if (mKeybgDrawable!=null) { + rect.left = key.x; + rect.top = initdrawy; + rect.right = key.x + key.width; + rect.bottom = key.y + key.height; + canvas.clipRect(rect); + int[] state = key.getCurrentDrawableState(); + //设置按压效果 + mKeybgDrawable.setState(state); + //设置边距 + mKeybgDrawable.setBounds(rect); + mKeybgDrawable.draw(canvas); + } if (key.label != null) { canvas.drawText( key.label.toString(), diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/constant/Util.java b/easykeyboard/src/main/java/com/jay/easykeyboard/util/Util.java similarity index 65% rename from easykeyboard/src/main/java/com/jay/easykeyboard/constant/Util.java rename to easykeyboard/src/main/java/com/jay/easykeyboard/util/Util.java index 51b31c0..5a05332 100644 --- a/easykeyboard/src/main/java/com/jay/easykeyboard/constant/Util.java +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/util/Util.java @@ -1,8 +1,10 @@ -package com.jay.easykeyboard.constant; +package com.jay.easykeyboard.util; import android.app.Activity; import android.content.Context; import android.os.Build; +import android.support.v7.widget.AppCompatEditText; +import android.support.v7.widget.AppCompatImageView; import android.text.InputType; import android.util.DisplayMetrics; import android.view.Display; @@ -51,61 +53,50 @@ public static boolean isNumeric(String str) { } - /** - * 隐藏软键盘 - * @param activity - */ - public static void hideKeyboard(Activity activity){ - View view = activity.getWindow().peekDecorView(); - if (view != null) { - InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0); - } - } - /** - * 关闭软键盘 - * @param context - * @param editText - */ - public static void closeKeyboard(Context context, EditText editText) { - InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } /** - * 打开软键盘 - * @param activity - * @param editText + * 禁止Edittext弹出软件盘,光标依然正常显示,并且能正常选取光标 */ - public static void openKeyboard(Activity activity, EditText editText) { - InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); - imm.showSoftInput(editText, InputMethodManager.RESULT_SHOWN); - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); + public static void disableShowSoftInput(EditText editText) { + Class cls = EditText.class; + Method method; + try { + method = cls.getMethod("setShowSoftInputOnFocus", boolean.class); + method.setAccessible(true); + method.invoke(editText, false); + } catch (Exception e) { + e.printStackTrace(); + } } /* - * 关闭软键盘显示游标 + * 显示键盘 * */ - public static void showCursor(Activity activity, EditText editText){ - if (android.os.Build.VERSION.SDK_INT <= 10) { - editText.setInputType(InputType.TYPE_NULL); - } else { - activity.getWindow().setSoftInputMode( - WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); - try { - Class cls = EditText.class; - Method setShowSoftInputOnFocus; - setShowSoftInputOnFocus = cls.getMethod( - "setShowSoftInputOnFocus", boolean.class); - setShowSoftInputOnFocus.setAccessible(true); - setShowSoftInputOnFocus.invoke(editText, false); - } catch (Exception e) { - e.printStackTrace(); + public static void showKeyboard(View view){ + if (view!=null) { + InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm != null) { + view.requestFocus(); + imm.showSoftInput(view, 0); } } } + /** + * 隐藏软键盘 + */ + public static void hideKeyboard(Context context){ + View view = ((Activity)context).getWindow().peekDecorView(); + if (view != null) { + InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm!=null){ + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } + } + } + + /** * 获取实际内容高度 * @param context diff --git a/easykeyboard/src/main/java/com/jay/easykeyboard/base/KeyBoardEditText.java b/easykeyboard/src/main/java/com/jay/easykeyboard/widget/KeyBoardEditText.java similarity index 83% rename from easykeyboard/src/main/java/com/jay/easykeyboard/base/KeyBoardEditText.java rename to easykeyboard/src/main/java/com/jay/easykeyboard/widget/KeyBoardEditText.java index c06ec8c..917633e 100644 --- a/easykeyboard/src/main/java/com/jay/easykeyboard/base/KeyBoardEditText.java +++ b/easykeyboard/src/main/java/com/jay/easykeyboard/widget/KeyBoardEditText.java @@ -1,4 +1,4 @@ -package com.jay.easykeyboard.base; +package com.jay.easykeyboard.widget; import android.app.Activity; import android.content.Context; @@ -17,30 +17,27 @@ import android.widget.PopupWindow; import com.jay.easykeyboard.R; -import com.jay.easykeyboard.constant.Util; +import com.jay.easykeyboard.util.Util; /** * Created by huangjie on 2018/2/5. - * 类名: - * 说明: + * 类名:能弹出自定义键盘的EditText抽象类 */ - public abstract class KeyBoardEditText extends AppCompatEditText { private Activity activity; - private int real_scontenth; + private int realHeight; //界面实际高度 private PopupWindow mKeyboardWindow; - private Window mWindow; private View mDecorView; private View mContentView; private int scrolldis; public KeyBoardEditText(Context context) { - this(context,null); + this(context, null); } public KeyBoardEditText(Context context, AttributeSet attrs) { - this(context, attrs,0); + this(context, attrs, 0); } public KeyBoardEditText(Context context, AttributeSet attrs, int defStyleAttr) { @@ -50,7 +47,7 @@ public KeyBoardEditText(Context context, AttributeSet attrs, int defStyleAttr) { private void init(Context context) { if (context instanceof Activity) activity = (Activity) context; - real_scontenth = Util.getContentHeight(context); + realHeight = Util.getContentHeight(context); this.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); if (this.getText() != null) { this.setSelection(this.getText().length()); @@ -59,7 +56,7 @@ private void init(Context context) { } protected void initPopWindow(View contentView) { - mKeyboardWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); + mKeyboardWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); mKeyboardWindow.setAnimationStyle(R.style.AnimationFade); mKeyboardWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); mKeyboardWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @@ -82,39 +79,28 @@ public void onDismiss() { protected void onAttachedToWindow() { super.onAttachedToWindow(); if (null != activity) { - mWindow = activity.getWindow(); + Window mWindow = activity.getWindow(); mDecorView = mWindow.getDecorView(); mContentView = mDecorView.findViewById(Window.ID_ANDROID_CONTENT); } - Util.closeKeyboard(getContext(), this); - } - - public void recycle(){ - mKeyboardWindow = null; - mDecorView = null; - mContentView = null; - mWindow = null; - activity = null; + Util.disableShowSoftInput(this); } - - - protected void showKeyboardWindow() { if (null != mKeyboardWindow) { if (!mKeyboardWindow.isShowing()) { mKeyboardWindow.showAtLocation(this.mDecorView, Gravity.BOTTOM, 0, 0); if (null != mDecorView && null != mContentView) { - View popContentView = mKeyboardWindow.getContentView(); + final View popContentView = mKeyboardWindow.getContentView(); popContentView.post(new Runnable() { @Override public void run() { int[] pos = new int[2]; getLocationOnScreen(pos); - float height = mKeyboardWindow.getContentView().getMeasuredHeight(); + float height = popContentView.getMeasuredHeight(); Rect outRect = new Rect(); mDecorView.getWindowVisibleDisplayFrame(outRect); - int screen = real_scontenth; + int screen = realHeight; scrolldis = (int) ((pos[1] + getMeasuredHeight() - outRect.top) - (screen - height)); if (scrolldis > 0) { mContentView.scrollBy(0, scrolldis); @@ -134,6 +120,14 @@ protected void dismissKeyboardWindow() { } } + protected boolean isShowing() { + boolean isShowing = false; + if (null != mKeyboardWindow) { + isShowing = mKeyboardWindow.isShowing(); + } + return isShowing; + } + public PopupWindow getKeyboardWindow() { return mKeyboardWindow; } diff --git a/easykeyboard/src/main/res/drawable/bg_keyboard_selector.xml b/easykeyboard/src/main/res/drawable/bg_keyboard_selector.xml deleted file mode 100644 index 620e677..0000000 --- a/easykeyboard/src/main/res/drawable/bg_keyboard_selector.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/easykeyboard/src/main/res/values/attrs.xml b/easykeyboard/src/main/res/values/attrs.xml index ae4aff9..9ba9308 100644 --- a/easykeyboard/src/main/res/values/attrs.xml +++ b/easykeyboard/src/main/res/values/attrs.xml @@ -10,6 +10,7 @@ + \ No newline at end of file