編輯:關於Android編程
繼承於InputMethodService類的服務代碼如下:
int keyCode = sKey.getKeyCode();
KeyEvent eDown = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD); KeyEvent eUp = new KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD); onKeyDown(keyCode, eDown); onKeyUp(keyCode, eUp);
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (processKey(event, 0 != event.getRepeatCount())) return true;
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (processKey(event, true)) return true;
return super.onKeyUp(keyCode, event);
}if (processKey(event, true)) return true;
if (processFunctionKeys(keyCode, realAction)) {
return true;
} if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (!realAction){
Log.d(TAG,"processFunctionKeys call KEYCODE_ENTER return");
return true;
}
sendKeyChar('\n');
return true;
}sendKeyChar('\n'); public void sendKeyChar(char charCode) {
switch (charCode) {
case '\n': // Apps may be listening to an enter key to perform an action
if (!sendDefaultEditorAction(true)) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
}
break;
default:
// Make sure that digits go through any text watcher on the client side.
if (charCode >= '0' && charCode <= '9') {
sendDownUpKeyEvents(charCode - '0' + KeyEvent.KEYCODE_0);
} else {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.commitText(String.valueOf((char) charCode), 1);
}
}
break;
}
}case '\n': // Apps may be listening to an enter key to perform an action
if (!sendDefaultEditorAction(true)) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
}
break; public boolean sendDefaultEditorAction(boolean fromEnterKey) {
EditorInfo ei = getCurrentInputEditorInfo();
if (ei != null &&
(!fromEnterKey || (ei.imeOptions &
EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) &&
(ei.imeOptions & EditorInfo.IME_MASK_ACTION) !=
EditorInfo.IME_ACTION_NONE) {
// If the enter key was pressed, and the editor has a default
// action associated with pressing enter, then send it that
// explicit action instead of the key event.
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.performEditorAction(ei.imeOptions&EditorInfo.IME_MASK_ACTION);
}
return true;
}
return false;
} /**
* Have the editor perform an action it has said it can do.
*/
public boolean performEditorAction(int editorAction); /**
* Set of bits in {@link #imeOptions} that provide alternative actions
* associated with the "enter" key. This both helps the IME provide
* better feedback about what the enter key will do, and also allows it
* to provide alternative mechanisms for providing that command.
*/
public static final int IME_MASK_ACTION = 0x000000ff; @Override
public boolean performEditorAction(int actionCode) {
if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
mTextView.onEditorAction(actionCode);
return true;
}edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
/*判斷是否是“GO”鍵*/
if(actionId == EditorInfo.IME_ACTION_GO){
edittext.setText("success");
return true;
}
return false;
}
});
/**
* Interface definition for a callback to be invoked when an action is
* performed on the editor.
*/
public interface OnEditorActionListener {
/**
* Called when an action is being performed.
*
* @param v The view that was clicked.
* @param actionId Identifier of the action. This will be either the
* identifier you supplied, or {@link EditorInfo#IME_NULL
* EditorInfo.IME_NULL} if being called due to the enter key
* being pressed.
* @param event If triggered by an enter key, this is the event;
* otherwise, this is null.
* @return Return true if you have consumed the action, else false.
*/
boolean onEditorAction(TextView v, int actionId, KeyEvent event);
} /**
* Set a special listener to be called when an action is performed
* on the text view. This will be called when the enter key is pressed,
* or when an action supplied to the IME is selected by the user. Setting
* this means that the normal hard key event will not insert a newline
* into the text view, even if it is multi-line; holding down the ALT
* modifier will, however, allow the user to insert a newline character.
*/
public void setOnEditorActionListener(OnEditorActionListener l) {
if (mInputContentType == null) {
mInputContentType = new InputContentType();
}
mInputContentType.onEditorActionListener = l;
}那麼我們再回到上面EditableInputConnection中的方法:
@Override
public boolean performEditorAction(int actionCode) {
if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
mTextView.onEditorAction(actionCode);
return true;
}mTextView.onEditorAction(actionCode);
public void onEditorAction(int actionCode) {
final InputContentType ict = mInputContentType;
if (ict != null) {
if (ict.onEditorActionListener != null) {
if (ict.onEditorActionListener.onEditorAction(this,
actionCode, null)) {
return;
}
}ict.onEditorActionListener.onEditorAction(this,
actionCode, null)
Android開發筆記(一百零六)支付繳費SDK
第三方支付第三方支付指的是第三方平台與各銀行簽約,在買方與賣方之間實現中介擔保,從而增強了支付交易的安全性。國內常用的支付平台主要是支付寶和微信支付,其中支付寶的市場份額
通過Android trace文件分析死鎖ANR實例過程
對於從事Android開發的人來說,遇到ANR(Application Not Responding)是比較常見的問題。一般情況下,如果有ANR發生,系統都會在/data
Android 快速簡單實現夜間模式
ChangeMode項目地址:ChangeMode Implementation of night mode for Android. 用最簡單的方式實現夜間模式,支持L
JSON解析和XML解析區別對比
JSON解析和XML解析是較為普遍的兩種解析方式,其中JSON解析的市場分額更大。本文系統的分析兩種解析方式的區別,為更好地處理數據作准備。由於目前階段主要是做移動開發,