判断搜索按键

This commit is contained in:
litingting 2024-08-26 18:23:37 +08:00
parent c1abe2137f
commit 4a4e36184f
2 changed files with 39 additions and 12 deletions

View File

@ -15,10 +15,13 @@ public class KeyboardService extends InputMethodService implements KeyboardView.
private int[] ViewXmls = new int[4]; private int[] ViewXmls = new int[4];
private MyKeyboard myKeyboard; private MyKeyboard myKeyboard;
private int curImeAction;
public KeyboardService() { public KeyboardService() {
} }
@Override @Override
public View onCreateInputView() { public View onCreateInputView() {
ViewXmls[0] = R.xml.view_1; ViewXmls[0] = R.xml.view_1;
@ -37,10 +40,16 @@ public class KeyboardService extends InputMethodService implements KeyboardView.
@Override @Override
public void onWindowShown() { public void onWindowShown() {
super.onWindowShown(); super.onWindowShown();
myKeyboard.updateConfigView(this); curImeAction = getImeAction(getCurrentInputEditorInfo().imeOptions);
myKeyboard.updateConfigView(this,curImeAction);
myKeyboard.invalidate(); myKeyboard.invalidate();
} }
public int getImeAction(int imeOptions) {
int i = imeOptions & EditorInfo.IME_MASK_ACTION;
return i ;
}
@Override @Override
public void onPress(int primaryCode) { public void onPress(int primaryCode) {
@ -81,7 +90,7 @@ public class KeyboardService extends InputMethodService implements KeyboardView.
getCurrentInputConnection().deleteSurroundingText(1, 0); getCurrentInputConnection().deleteSurroundingText(1, 0);
break; break;
case MyKeyboard.KeyBoard.KEYCODE_DONE: case MyKeyboard.KeyBoard.KEYCODE_DONE:
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_DONE); getCurrentInputConnection().performEditorAction(curImeAction);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myKeyboard.getWindowToken(), 0); imm.hideSoftInputFromWindow(myKeyboard.getWindowToken(), 0);
break; break;

View File

@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard; import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView; import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import com.key.coolkeyboard.tool.Mytool; import com.key.coolkeyboard.tool.Mytool;
@ -19,7 +20,7 @@ public class MyKeyboard extends KeyboardView {
private int viewType = 0; private int viewType = 0;
private int shiftType = 0; private int shiftType = 0;
private CustomViewConfig config; private CustomViewConfig config;
private int curImeAction;
public MyKeyboard(Context context, AttributeSet attrs) { public MyKeyboard(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
initView(); initView();
@ -62,30 +63,36 @@ public class MyKeyboard extends KeyboardView {
if (code == KeyBoard.KEYCODE_MODE_CHANGE) { if (code == KeyBoard.KEYCODE_MODE_CHANGE) {
onDrawKeyBackground(key, config.getBgActionDraw(), canvas); onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
onDrawLabel(key, canvas); onDrawLabel(key, canvas,"");
} else if (code == KeyBoard.KEYCODE_SHIFT) { } else if (code == KeyBoard.KEYCODE_SHIFT) {
onDrawKeyBackground(key, config.getBgActionDraw(), canvas); onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
drawIcon.onDrawKeyIcon(key, getShiftDraw(), canvas, this); drawIcon.onDrawKeyIcon(key, getShiftDraw(), canvas, this);
} else if (code == KeyBoard.KEYCODE_SHIFT_123) { } else if (code == KeyBoard.KEYCODE_SHIFT_123) {
onDrawKeyBackground(key, config.getBgActionDraw(), canvas); onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
onDrawLabel(key, canvas); onDrawLabel(key, canvas,"");
} else if (code == KeyBoard.KEYCODE_SHIFT_MORE) { } else if (code == KeyBoard.KEYCODE_SHIFT_MORE) {
onDrawKeyBackground(key, config.getBgActionDraw(), canvas); onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
onDrawLabel(key, canvas); onDrawLabel(key, canvas,"");
} else if (code == KeyBoard.KEYCODE_DONE) { } else if (code == KeyBoard.KEYCODE_DONE) {
onDrawKeyBackground(key, config.getBgActionDraw(), canvas); onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
onDrawLabel(key, canvas); if(curImeAction == EditorInfo.IME_ACTION_SEARCH){
onDrawLabel(key, canvas,"Search");
}else {
onDrawLabel(key, canvas,"");
}
} else if (code == KeyBoard.KEYCODE_DELETE) { } else if (code == KeyBoard.KEYCODE_DELETE) {
onDrawKeyBackground(key, config.getBgActionDraw(), canvas); onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
drawIcon.onDrawKeyIcon(key, config.getIconDel(), canvas, this); drawIcon.onDrawKeyIcon(key, config.getIconDel(), canvas, this);
onDrawLabel(key, canvas); onDrawLabel(key, canvas,"");
} else { } else {
mPaint.setColor(config.getKeyNoramlcolor()); mPaint.setColor(config.getKeyNoramlcolor());
onDrawKeyBackground(key, config.getBgNormalDraw(), canvas); onDrawKeyBackground(key, config.getBgNormalDraw(), canvas);
onDrawLabel(key, canvas); onDrawLabel(key, canvas,"");
} }
} }
@ -108,7 +115,8 @@ public class MyKeyboard extends KeyboardView {
setPreviewEnabled(false); setPreviewEnabled(false);
} }
public void updateConfigView(Context con) { public void updateConfigView(Context con,int ime) {
curImeAction = ime;
config.updateConfig(con); config.updateConfig(con);
setBackground(config.getBG()); setBackground(config.getBG());
invalidateAllKeys(); invalidateAllKeys();
@ -132,13 +140,23 @@ public class MyKeyboard extends KeyboardView {
private void onDrawLabel( private void onDrawLabel(
Keyboard.Key myKey, Keyboard.Key myKey,
Canvas canvas) { Canvas canvas,String custLabel) {
boolean b = myKey.label == null || myKey.label == ""; boolean b = myKey.label == null || myKey.label == "";
if (!b) { if (!b) {
float y1 = myKey.y + myKey.height / 2f - (mPaint.descent() + mPaint.ascent()) / 2f; float y1 = myKey.y + myKey.height / 2f - (mPaint.descent() + mPaint.ascent()) / 2f;
float x1 = myKey.x + getPaddingLeft() + ((myKey.width / 2f)); float x1 = myKey.x + getPaddingLeft() + ((myKey.width / 2f));
x1 -= mPaint.measureText(myKey.label.toString()) / 2f; x1 -= mPaint.measureText(myKey.label.toString()) / 2f;
if(!custLabel.isEmpty()){
float texsize = Mytool.spToPpx(14f, this.getContext());
mPaint.setTextSize(texsize);
canvas.drawText(custLabel, x1, y1, mPaint);
}else {
float texsize = Mytool.spToPpx(18f, this.getContext());
mPaint.setTextSize(texsize);
canvas.drawText(myKey.label.toString(), x1, y1, mPaint); canvas.drawText(myKey.label.toString(), x1, y1, mPaint);
}
} }
} }