package mobile.android.demo.input.method;
import java.util.ArrayList;
import java.util.List;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.method.MetaKeyKeyListener;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
public class SoftKeyboard extends InputMethodService implements
KeyboardView.OnKeyboardActionListener
{
static final boolean DEBUG = false;
static final boolean PROCESS_HARD_KEYS = true;
private KeyboardView mInputView;
private CandidateView mCandidateView;
private CompletionInfo[] mCompletions;
private StringBuilder mComposing = new StringBuilder();
private boolean mPredictionOn;
private boolean mCompletionOn;
private int mLastDisplayWidth;
private boolean mCapsLock;
private long mLastShiftTime;
private long mMetaState;
private LatinKeyboard mSymbolsKeyboard;
private LatinKeyboard mSymbolsShiftedKeyboard;
private LatinKeyboard mQwertyKeyboard;
private LatinKeyboard mCurKeyboard;
private String mWordSeparators;
@Override
public void onCreate()
{
super.onCreate();
mWordSeparators = getResources().getString(R.string.word_separators);
}
@Override
public void onInitializeInterface()
{
if (mQwertyKeyboard != null)
{
int displayWidth = getMaxWidth();
if (displayWidth == mLastDisplayWidth)
return;
mLastDisplayWidth = displayWidth;
}
mQwertyKeyboard = new LatinKeyboard(this, R.xml.qwerty);
mSymbolsKeyboard = new LatinKeyboard(this, R.xml.symbols);
mSymbolsShiftedKeyboard = new LatinKeyboard(this, R.xml.symbols_shift);
}
@Override
public View onCreateInputView()
{
mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.input,
null);
mInputView.setOnKeyboardActionListener(this);
mInputView.setKeyboard(mQwertyKeyboard);
return mInputView;
}
@Override
public View onCreateCandidatesView()
{
mCandidateView = new CandidateView(this);
mCandidateView.setService(this);
return mCandidateView;
}
@Override
public void onStartInput(EditorInfo attribute, boolean restarting)
{
super.onStartInput(attribute, restarting);
mComposing.setLength(0);
updateCandidates();
if (!restarting)
{
mMetaState = 0;
}
mPredictionOn = false;
mCompletionOn = false;
mCompletions = null;
switch (attribute.inputType & EditorInfo.TYPE_MASK_CLASS)
{
case EditorInfo.TYPE_CLASS_NUMBER:
case EditorInfo.TYPE_CLASS_DATETIME:
mCurKeyboard = mSymbolsKeyboard;
break;
case EditorInfo.TYPE_CLASS_PHONE:
mCurKeyboard = mSymbolsKeyboard;
break;
case EditorInfo.TYPE_CLASS_TEXT:
mCurKeyboard = mQwertyKeyboard;
mPredictionOn = true;
int variation = attribute.inputType
& EditorInfo.TYPE_MASK_VARIATION;
if (variation == EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
|| variation == EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)
{
mPredictionOn = false;
}
if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| variation == EditorInfo.TYPE_TEXT_VARIATION_URI
|| variation == EditorInfo.TYPE_TEXT_VARIATION_FILTER)
{
mPredictionOn = false;
}
if ((attribute.inputType & EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0)
{
mPredictionOn = false;
mCompletionOn = isFullscreenMode();
}
updateShiftKeyState(attribute);
break;
default:
mCurKeyboard = mQwertyKeyboard;
updateShiftKeyState(attribute);
}
mCurKeyboard.setImeOptions(getResources(), attribute.imeOptions);
}
@Override
public void onFinishInput()
{
super.onFinishInput();
mComposing.setLength(0);
updateCandidates();
setCandidatesViewShown(false);
mCurKeyboard = mQwertyKeyboard;
if (mInputView != null)
{
mInputView.closing();
}
}
@Override
public void onStartInputView(EditorInfo attribute, boolean restarting)
{
super.onStartInputView(attribute, restarting);
mInputView.setKeyboard(mCurKeyboard);
mInputView.closing();
}
@Override
public void onUpdateSelection(int oldSelStart, int oldSelEnd,
int newSelStart, int newSelEnd, int candidatesStart,
int candidatesEnd)
{
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
candidatesStart, candidatesEnd);
if (mComposing.length() > 0
&& (newSelStart != candidatesEnd || newSelEnd != candidatesEnd))
{
mComposing.setLength(0);
updateCandidates();
InputConnection ic = getCurrentInputConnection();
if (ic != null)
{
ic.finishComposingText();
}
}
}
@Override
public void onDisplayCompletions(CompletionInfo[] completions)
{
if (mCompletionOn)
{
mCompletions = completions;
if (completions == null)
{
setSuggestions(null, false, false);
return;
}
List<String> stringList = new ArrayList<String>();
for (int i = 0; i < (completions != null ? completions.length : 0); i++)
{
CompletionInfo ci = completions[i];
if (ci != null)
stringList.add(ci.getText().toString());
}
setSuggestions(stringList, true, true);
}
}
private boolean translateKeyDown(int keyCode, KeyEvent event)
{
mMetaState = MetaKeyKeyListener.handleKeyDown(mMetaState, keyCode,
event);
int c = event.getUnicodeChar(MetaKeyKeyListener
.getMetaState(mMetaState));
mMetaState = MetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState);
InputConnection ic = getCurrentInputConnection();
if (c == 0 || ic == null)
{
return false;
}
boolean dead = false;
if ((c & KeyCharacterMap.COMBINING_ACCENT) != 0)
{
dead = true;
c = c & KeyCharacterMap.COMBINING_ACCENT_MASK;
}
if (mComposing.length() > 0)
{
char accent = mComposing.charAt(mComposing.length() - 1);
int composed = KeyEvent.getDeadChar(accent, c);
if (composed != 0)
{
c = composed;
mComposing.setLength(mComposing.length() - 1);
}
}
onKey(c, null);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK:
if (event.getRepeatCount() == 0 && mInputView != null)
{
if (mInputView.handleBack())
{
return true;
}
}
break;
case KeyEvent.KEYCODE_DEL:
if (mComposing.length() > 0)
{
onKey(Keyboard.KEYCODE_DELETE, null);
return true;
}
break;
case KeyEvent.KEYCODE_ENTER:
return false;
default:
if (PROCESS_HARD_KEYS)
{
if (keyCode == KeyEvent.KEYCODE_SPACE
&& (event.getMetaState() & KeyEvent.META_ALT_ON) != 0)
{
InputConnection ic = getCurrentInputConnection();
if (ic != null)
{
ic.clearMetaKeyStates(KeyEvent.META_ALT_ON);
keyDownUp(KeyEvent.KEYCODE_A);
keyDownUp(KeyEvent.KEYCODE_N);
keyDownUp(KeyEvent.KEYCODE_D);
keyDownUp(KeyEvent.KEYCODE_R);
keyDownUp(KeyEvent.KEYCODE_O);
keyDownUp(KeyEvent.KEYCODE_I);
keyDownUp(KeyEvent.KEYCODE_D);
return true;
}
}
if (mPredictionOn && translateKeyDown(keyCode, event))
{
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (PROCESS_HARD_KEYS)
{
if (mPredictionOn)
{
mMetaState = MetaKeyKeyListener.handleKeyUp(mMetaState,
keyCode, event);
}
}
return super.onKeyUp(keyCode, event);
}
private void commitTyped(InputConnection inputConnection)
{
if (mComposing.length() > 0)
{
inputConnection.commitText(mComposing, mComposing.length());
mComposing.setLength(0);
updateCandidates();
}
}
private void updateShiftKeyState(EditorInfo attr)
{
if (attr != null && mInputView != null
&& mQwertyKeyboard == mInputView.getKeyboard())
{
int caps = 0;
EditorInfo e
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
input_method.rar (48个子文件)
input_method
bin
classes.dex 21KB
resources.ap_ 21KB
mobile
android
demo
input
method
LatinKeyboard$LatinKey.class 1KB
R$layout.class 437B
CandidateView$1.class 1KB
R$id.class 428B
R$drawable.class 668B
LatinKeyboard.class 2KB
R$dimen.class 534B
R$attr.class 382B
R.class 811B
SoftKeyboard.class 12KB
CandidateView.class 7KB
R$string.class 591B
R$xml.class 527B
LatinKeyboardView.class 756B
R$color.class 573B
input_method.apk 34KB
res
drawable-ldpi
values-land
dimens.xml 108B
drawable-hdpi
sym_keyboard_done.png 2KB
sym_keyboard_search.png 2KB
sym_keyboard_delete.png 885B
sym_keyboard_space.png 859B
sym_keyboard_shift.png 1KB
sym_keyboard_return.png 536B
values
colors.xml 284B
strings.xml 562B
dimens.xml 216B
drawable-mdpi
sym_keyboard_done.png 771B
sym_keyboard_search.png 1KB
sym_keyboard_delete.png 465B
sym_keyboard_space.png 436B
sym_keyboard_shift.png 1KB
sym_keyboard_return.png 337B
layout
input.xml 342B
xml
symbols.xml 3KB
symbols_shift.xml 3KB
method.xml 115B
qwerty.xml 3KB
assets
default.properties 362B
gen
mobile
android
demo
input
method
R.java 2KB
src
mobile
android
demo
input
method
SoftKeyboard.java 14KB
LatinKeyboard.java 2KB
LatinKeyboardView.java 472B
CandidateView.java 7KB
.project 848B
.classpath 280B
AndroidManifest.xml 556B
共 48 条
- 1
crazychengzi
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页