private void hideIM(View edt){
// try to hide input_method:
try {
InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
IBinder windowToken = edt.getWindowToken();
if(windowToken != null) {
// always de-activate IM
im.hideSoftInputFromWindow(windowToken, 0);
}
} catch (Exception e) {
Log.e("HideInputMethod", "failed:"+e.getMessage());
}
}
private OnFocusChangeListener focus_listener_noIM = new OnFocusChangeListener(){
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus==true) {
hideIM(v);
}
}
};
private OnTouchListener touch_listener_noIM = new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
hideIM(v);
}
return false; // dispatch the event further!
}
};
// 以下是Activity的onCreate()函数的片断:
public void onCreate(Bundle savedInstanceState) {
EditText edt_url = (EditText)findViewById(R.id.edt_url);
edt_url.setOnFocusChangeListener(focus_listener_noIM);
edt_url.setOnTouchListener(touch_listener_noIM);
}