Android EditText獲取焦點,彈起輸入法
阿新 • • 發佈:2019-01-29
/** * 得到輸入框的文字 * @return */ public String getKeywordText(EditText edt) { return edt.getText().toString().trim(); } /** * 將焦點移到輸入框,彈起輸入法 */ public void focusKeywordView(EditText edt) { if (edt != null) { edt.requestFocus(); edt.setSelection(getKeywordText(edt).length()); showInputMethod(edt, true, 500); } } /** * 彈起輸入法 * @param edit * @param delay * @param delayTime */ private void showInputMethod(final EditText edit, boolean delay, int delayTime) { if (delay) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { InputMethodManager imm = (InputMethodManager) SogouMapApplication.getInstance().getSystemService( Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.showSoftInput(edit, 0); } } }, delayTime); } else { InputMethodManager imm = (InputMethodManager) SogouMapApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(edit, 0); } }
getKeyWordText(...)是為了得到輸入框中的字串,然後可以將游標定位到最後
<span style="font-size:18px;">showInputMethod(...)是使用TimerTask將輸入法彈起,以免阻礙主執行緒的執行</span>
<span style="font-size:18px;">最後呼叫focusKeyWordView(...)即可。。。</span>
<span style="font-size:18px;"><img alt="奮鬥" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif" />, 很簡單,繼續努力。。。 </span>
<span style="font-size:18px;">
</span>