下面列出了android.view.inputmethod.InputConnection#getSelectedText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @hide
*/
public void onExtractedSetSpan(Object span, int start, int end, int flags) {
InputConnection conn = getCurrentInputConnection();
if (conn != null) {
if (!conn.setSelection(start, end)) return;
CharSequence text = conn.getSelectedText(InputConnection.GET_TEXT_WITH_STYLES);
if (text instanceof Spannable) {
((Spannable) text).setSpan(span, 0, text.length(), flags);
conn.setComposingRegion(start, end);
conn.commitText(text, 1);
}
}
}
private void updateStatus() {
InputConnection ic = getCurrentIC();
if (ic == null) {
return;
}
CharSequence cs = ic.getSelectedText(0);
boolean hasSelection = !TextUtils.isEmpty(cs);
boolean hasClipData = ClipManager.getInstance().hasClipData();
updateSelectAllStatus(hasSelection);
updateCopyStatus(hasSelection);
updatePasteStatus(hasClipData);
updateDeleteStatus(hasSelection || !TextUtils.isEmpty(ic.getTextBeforeCursor(1, 0)));
}
/**
* use ROT13 to scramble the contents of the editor
*/
private void scramble(InputConnection con) {
char[] buffer = null;
CharSequence selected = con.getSelectedText(0);
if (selected != null) {
buffer = selected.toString().toCharArray();
}
else {
ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
if (txt == null) {
return;
}
buffer = txt.text.toString().toCharArray();
if (buffer.length == 0)
return;
}
// char[] buffer = con.getSelectedText(0).toString().toCharArray();
// //con.getExtractedText(new
// ExtractedTextRequest(),0).text.toString().toCharArray();
for (int i = 0; i < buffer.length; i++) {
if (buffer[i] >= 'a' && buffer[i] <= 'm')
buffer[i] += 13;
else if (buffer[i] >= 'A' && buffer[i] <= 'M')
buffer[i] += 13;
else if (buffer[i] >= 'n' && buffer[i] <= 'z')
buffer[i] -= 13;
else if (buffer[i] >= 'N' && buffer[i] <= 'Z')
buffer[i] -= 13;
}
if (selected == null) {
con.setComposingRegion(0, buffer.length);
}
con.setComposingText(new String(buffer), 1);
con.finishComposingText();
}
private boolean hasSelection() {
InputConnection ic = getInputConnection();
CharSequence selection = ic.getSelectedText(0);
return selection != null && selection.length() > 0;
}
@Override
public void onPress(int primaryCode) {
// SEE: res/xml/keyboarddef.xml for the definitions.
switch (primaryCode) {
case 0: {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
break;
}
case 1: {
/*
* Intent intent = new Intent(this, SettingsActivity.class);
* intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
* startActivity(intent);
*/
break;
}
case 2: {
try {
InputConnection con = getCurrentInputConnection();
CharSequence txt = con.getSelectedText(0);
if (txt == null) {
txt = getCurrentInputConnection().getExtractedText(
new ExtractedTextRequest(), 0).text;
}
TelnetEditorShell.self.showText(txt + "");
Toast.makeText(this, R.string.msg_sent, Toast.LENGTH_SHORT).show();
}
catch (Exception exp) {
Toast.makeText(this, R.string.err_noclient, Toast.LENGTH_SHORT)
.show();
}
break;
}
case 3: {
try {
if (TelnetEditorShell.self != null) {
TelnetEditorShell.self.disconnect();
Toast.makeText(this, R.string.msg_client_disconnected,
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, R.string.err_noclient, Toast.LENGTH_SHORT)
.show();
}
}
catch (Exception e) {
}
}
}
}