Created
September 14, 2017 03:21
-
-
Save mr-wind/8d5e94a74f66c110cdf40b1dd426e0e7 to your computer and use it in GitHub Desktop.
自定义键盘组装类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.PixelFormat; | |
import android.inputmethodservice.Keyboard; | |
import android.inputmethodservice.KeyboardView; | |
import android.text.Editable; | |
import android.text.InputType; | |
import android.view.ActionMode; | |
import android.view.Gravity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.WindowManager; | |
import android.view.inputmethod.InputMethodManager; | |
import android.widget.EditText; | |
/** | |
* Created by quhaofeng on 17/2/22. | |
* 自定义键盘组装类 | |
*/ | |
public class CustomKeyboard { | |
public final static int CodeGrab = -10; // | |
public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE | |
public final static int CodeCancel = -3; // Keyboard.KEYCODE_CANCEL | |
public final static int CodePrev = 55000; | |
public final static int CodeAllLeft = 55001; | |
public final static int CodeLeft = 55002; | |
public final static int CodeRight = 55003; | |
public final static int CodeAllRight = 55004; | |
public final static int CodeNext = 55005; | |
public final static int CodeClear = 55006; | |
public final static int CodeCellUp = 1001; | |
public final static int CodeCellDown = 1002; | |
public final static int CodeCellLeft = 1003; | |
public final static int CodeCellRight = 1004; | |
public final static int CodeDecimalpoint = 46; | |
public final static int CodeZero = 48; | |
public final static int dd = Keyboard.KEYCODE_DONE; | |
private KeyboardView mKeyboardView; | |
private Activity mHostActicity; | |
private EditText mEditText; | |
private OnDoneClickListener mListener; | |
private WindowManager mWindowManager; | |
/** | |
* 使用自定义键盘 | |
* | |
* @param acticity 宿主Activity,用于隐藏系统自带键盘 | |
* @param keyboard keyboard布局,注意Keyboard的第一个参数,尽量用ApplicationContext | |
* @param keyboardView 在layout文件中的布局对象 | |
*/ | |
public CustomKeyboard(Activity acticity, Keyboard keyboard, KeyboardView keyboardView) { | |
mHostActicity = acticity; | |
mKeyboardView = keyboardView; | |
mKeyboardView.setKeyboard(keyboard); | |
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener); | |
mHostActicity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); | |
} | |
private KeyboardView.OnKeyboardActionListener mOnKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() { | |
@Override | |
public void onPress(int primaryCode) { | |
} | |
@Override | |
public void onRelease(int primaryCode) { | |
} | |
@Override | |
public void onKey(int primaryCode, int[] keyCodes) { | |
if (!mEditText.isFocused()) { | |
return; | |
} | |
EditText edittext = mEditText; | |
Editable editable = edittext.getText(); | |
int start = edittext.getSelectionStart(); | |
int end = edittext.getSelectionEnd(); | |
// Apply the key to the edittext | |
// 根据primaryCode的值进行功能处理 | |
if (primaryCode == CodeCancel) { | |
hideKeyboard(); | |
} else if (primaryCode == CodeDelete) { | |
if (editable != null && start > 0) { | |
editable.delete(start - 1, start); | |
} else if (editable != null && start != end) { // delete selection | |
editable.delete(start, end); | |
} | |
} else if (primaryCode == CodeClear) { | |
if (editable != null) editable.clear(); | |
} else if (primaryCode == CodeLeft) { | |
if (start > 0) edittext.setSelection(start - 1); | |
} else if (primaryCode == CodeRight) { | |
if (start < edittext.length()) edittext.setSelection(start + 1); | |
} else if (primaryCode == CodeAllLeft) { | |
edittext.setSelection(0); | |
} else if (primaryCode == CodeAllRight) { | |
edittext.setSelection(edittext.length()); | |
} else if (primaryCode == CodePrev) { | |
View focusNew = edittext.focusSearch(View.FOCUS_BACKWARD); | |
if (focusNew != null) focusNew.requestFocus(); | |
} else if (primaryCode == CodeNext) { | |
View focusNew = edittext.focusSearch(View.FOCUS_FORWARD); | |
if (focusNew != null) focusNew.requestFocus(); | |
else if (primaryCode == CodeCellUp || primaryCode == CodeCellDown || primaryCode == CodeCellLeft || primaryCode == CodeCellRight) { | |
// TODO | |
} else if (primaryCode == CodeGrab) { | |
} | |
} else if (primaryCode == Keyboard.KEYCODE_DONE) { | |
mListener.onDoneClick(); | |
hideKeyboard(); | |
} else if (primaryCode == -100) {//自定义code | |
if (start != end) { | |
editable.delete(start, end); | |
} | |
editable.insert(start, "00"); | |
} else { // insert character | |
/////////////////////////////////////////////////////////////////////// | |
//////////////////////////////////////////////////////////////////// | |
///////////////////////////// | |
if (start != end) { | |
editable.delete(start, end); | |
} | |
editable.insert(start, Character.toString((char) primaryCode)); | |
} | |
} | |
@Override | |
public void onText(CharSequence text) { | |
} | |
@Override | |
public void swipeLeft() { | |
} | |
@Override | |
public void swipeRight() { | |
} | |
@Override | |
public void swipeDown() { | |
} | |
@Override | |
public void swipeUp() { | |
} | |
}; | |
public boolean isTestKeyboardVisible() { | |
return mKeyboardView.getVisibility() == View.VISIBLE; | |
} | |
public void showKeyboard(View v) { | |
mKeyboardView.setVisibility(View.VISIBLE); | |
mKeyboardView.setEnabled(true); | |
if (v != null) | |
((InputMethodManager) mHostActicity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0); | |
} | |
public void hideKeyboard() { | |
mKeyboardView.setVisibility(View.GONE); | |
mKeyboardView.setEnabled(false); | |
} | |
public void registerEditText(EditText edittext) { | |
// Find the EditText 'resid' | |
//EditText edittext = (EditText) mHostActicity.findViewById(resid); | |
// Make the custom keyboard appear | |
mEditText = edittext; | |
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() { | |
@Override | |
public void onFocusChange(View v, boolean hasFocus) { | |
if (hasFocus) showKeyboard(v); | |
else hideKeyboard(); | |
} | |
}); | |
edittext.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
showKeyboard(v); | |
} | |
}); | |
// Disable standard keyboard hard way | |
edittext.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
EditText edittext = (EditText) v; | |
int inType = edittext.getInputType(); // Backup the input type | |
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard | |
edittext.onTouchEvent(event); // Call native handler | |
edittext.setInputType(inType); // Restore input type | |
return true; // Consume touch event | |
} | |
}); | |
//去掉选择粘贴的操作 | |
edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() { | |
@Override | |
public boolean onCreateActionMode(ActionMode mode, Menu menu) { | |
return false; | |
} | |
@Override | |
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { | |
return false; | |
} | |
@Override | |
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { | |
return false; | |
} | |
@Override | |
public void onDestroyActionMode(ActionMode mode) { | |
} | |
}); | |
// Disable spell check (hex strings look like words to Android) | |
edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); | |
} | |
/** | |
* 设置移动位置 | |
* | |
* @param x | |
* @param y | |
*/ | |
public void moveTo(int x, int y) { | |
int[] edLocation = {0, 0}; | |
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mKeyboardView.getLayoutParams(); | |
params.leftMargin = x;// inScreenCoordinates.left; | |
params.topMargin = y;//inScreenCoordinates.top; | |
mKeyboardView.setLayoutParams(params); | |
} | |
public void setOnDoneClickListener(OnDoneClickListener listener) { | |
mListener = listener; | |
} | |
public interface OnDoneClickListener { | |
void onDoneClick(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment