2013-08-23 191 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(); 
     } 
    }