今天遇到個問題,就是EditView需要顯示光標但是不彈出軟鍵盤,結果要不就是直接都不顯示要不就是都顯示。
網上找到了相關得,但是有點不如意,結合了兩個才如我意。代碼如下
if (android.os.Build.VERSION.SDK_INT >10) {//4.0以上
? ? try {
Class cls = EditText.class;
Method setShowSoftInputOnFocus;
setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",
boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(tv_code,false);
}catch (Exception e) {
e.printStackTrace();
}
}
然后再對editview添加焦點得監(jiān)聽
tv_code.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
? ? public void onFocusChange(View v,boolean hasFocus) {
if(hasFocus){
InputMethodManager imm = (InputMethodManager)tv_code.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm !=null) {
imm.hideSoftInputFromWindow(tv_code.getWindowToken(),0);
}? ? ? ? ? ? ? ? }
}
});
這樣得話,第一次還是會彈出軟鍵盤,然后再在editview得父容器添加
android:focusable="true"
android:focusableInTouchMode="true"
好了,這樣不會彈出軟鍵盤了。