纠结了很久,始终没有找到想要的效果,在网上也查找了很久,最终还是找到了一片文章,简单描述了思想。利用setShowSoftInputOnFocus方法,通过反射机制实现软键盘的隐藏。
package com.example.input;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText text = (EditText) findViewById(R.id.input_editText1);
if (android.os.Build.VERSION.SDK_INT <= 10) {//android3.0版本以上才能使用setShowSoftInputOnFocus
text.setInputType(InputType.TYPE_NULL);//强制关闭软键盘,但是编辑框没有闪烁的光标
} else {
getWindow().setSoftInputMode(
//设置输入模式,窗体获得焦点,始终隐藏软键盘
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",
boolean.class);
//设置是可访问,为true,表示禁止访问
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(text, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
片段:
public static void hideSoftKeyborad(Activity paramActivity){
View view = paramActivity.getWindow().peekDecorView();
if(view!=null && view.getWindowToken()!=null){
((InputMethodManager)paramActivity.getSystemService("input_method")).hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}