本文實例講述了Android編程實現(xiàn)點擊EditText之外的控件隱藏軟鍵盤功能。分享給大家供大家參考,具體如下:
工具類
...public static void hideKeyboard(Context ctx) { if (ctx != null) { View view = ((Activity) ctx).getCurrentFocus(); if (view != null) { InputMethodManager inputManager = (InputMethodManager) ctx .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }}點擊除EDITTEXT之外的控件隱藏軟鍵盤,如果是viewgroup控件,遞歸執(zhí)行
public static void setupUI(View view, final Context ctx) { //Set up touch listener for non-text box views to hide keyboard. if(!(view instanceof EditText)) { view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { hideKeyboard(ctx); return false; } }); } //If a layout container, iterate over children and seed recursion. if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { View innerView = ((ViewGroup) view).getChildAt(i); setupUI(innerView, ctx); } } }...}調(diào)用時只需要傳遞最外層的layout即可。
UtilApp.setupUI((RelativeLayout) findViewById(R.id.login_parent), mContext);
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
新聞熱點
疑難解答