2013-08-23 193 views
1

我有清除按鈕來清除EditText。按下按鈕後顯示鍵盤

<Button 
     android:id="@+id/bClearText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|center_vertical" 
     android:layout_marginRight="10dp" 
     android:onClick="clearEtSearch" 
     android:background="@drawable/delete" /> 

此方法清除的EditText:

public void clearEtSearch(View v) { 
    etSearch.setText(""); 
    etSearch.requestFocus(); 
    showKeyboard(etSearch); 
} 

我已經採取了以下代碼隱藏和改變顯示的鍵盤,但它不工作

private void showKeyboard(View view) { 
    InputMethodManager manager = (InputMethodManager) view.getContext() 
      .getSystemService(INPUT_METHOD_SERVICE); 
    if (manager != null) 
     manager.showSoftInputFromInputMethod(view.getWindowToken(), 0); 
} 

我做錯了嗎?請給出建議來糾正我的代碼。

回答

2

我不確定,但您可以嘗試使用Context.INPUT_METHOD_SERVICE而不是INPUT_METHOD_SERVICE。有些是Forcing the Soft Keyboard open問題更多。

您還可以看到How to show soft-keyboard when edittext is focused 看看以下工作:

public void clearEtSearch(View v) { 
    etSearch.setText(""); 
    etSearch.requestFocus(); 

    InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
    manager.showSoftInputFromInputMethod(etSearch.getWindowToken(), 0); 

} 

根據你的需要,你可以嘗試的InputMethodManager不同常數像以下:

public void clearEtSearch(View v) { 
    etSearch.setText(""); 
    etSearch.requestFocus(); 
    InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
    manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 
} 

public void clearEtSearch(View v) { 
    etSearch.setText(""); 
    etSearch.requestFocus(); 
    InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    manager.showSoftInput(etSearch, InputMethodManager.SHOW_FORCED); 
} 

我還沒有試過代碼r ight不太確定哪一個可以工作。看到有很多相關的問題。希望它適合你。

0

使用此方法,並享受

public void showSoftKeyboard() { 
     try { 
      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); 
      inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    }