我需要禁用我的WebView中的软键盘以及所有编辑框中的软键盘(因为它们在WebView中,我无法访问它们)。
我尝试在清单文件中使用“android:windowSoftInputMode="stateAlwaysHidden"”,但是当我点击可编辑字段时,键盘仍然会弹出。
如何在WebView中禁用软键盘?
编辑:
我找到了解决方案(感谢@g00dy在这个帖子和@Kachi在https://dev59.com/WHI95IYBdhLWcg3wyRM0#9108219中的帖子),可以在键盘打开后关闭它:
public class ActivityBrowser extends Activity
{
private static WebView webviewHTML;
private static View viewRootHTML;
private static int iViewRootHTMLHeightDifferent;
public static Context contextBrowser;
{
contextBrowser = this;
}
public class webViewClient extends WebViewClient
{
@Override
public void onPageStarted( WebView view, String url, Bitmap favicon)
{
if( view == webviewHTML) super.onPageStarted( view, url, favicon);
}
@Override
public void onPageFinished( WebView view, String url)
{
if( view == webviewHTML) super.onPageFinished( view, url);
}
@Override
public boolean shouldOverrideUrlLoading( WebView view, String url)
{
if( view == webviewHTML) view.loadUrl( url);
return false;
// return super.shouldOverrideUrlLoading( view, url);
}
@Override
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl)
{
if( view == webviewHTML) ApplicationLeta.fPopup( getString( R.string.sPopupErrorSiteOpen) + " : " + description);
// ActivityBrowser.this.finish();
}
public void onReceivedSslError( WebView view, SslErrorHandler handler, SslError error)
{
if( view == webviewHTML) handler.proceed();
}
}
@Override
public boolean dispatchTouchEvent( MotionEvent motionEvent)
{
super.dispatchTouchEvent( motionEvent);
if( motionEvent.getAction() == MotionEvent.ACTION_MOVE) return true;
if( motionEvent.getAction() == MotionEvent.ACTION_UP)
{
// do something
}
if( motionEvent.getAction() == MotionEvent.ACTION_UP)
{
// do something
}
return false;
}
@Override
public void onBackPressed()
{
}
@Override
public void onWindowFocusChanged( boolean eFocus)
{
super.onWindowFocusChanged( eFocus);
if( eFocus == false)
{
fKeyboardClose();
new Thread( new Runnable()
{
@Override
public void run()
{
try
{
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync( KeyEvent.KEYCODE_BACK);
}
catch( Exception e) {}
}
} ).start();
}
}
private void fKeyboardClose()
{
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService( Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow( getCurrentFocus().getWindowToken(), 0);
}
public OnGlobalLayoutListener onGlobalLayoutListener = new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
Rect rect = new Rect();
viewRootHTML.getWindowVisibleDisplayFrame( rect);
iViewRootHTMLHeightDifferent = viewRootHTML.getRootView().getHeight() - (rect.bottom - rect.top);
if( iViewRootHTMLHeightDifferent > 50) fKeyboardClose();
}
};
@SuppressWarnings( "deprecation")
@SuppressLint( "SetJavaScriptEnabled")
public void onCreate( Bundle savedInstanceState)
{
super.onCreate( savedInstanceState);
setContentView( R.layout.browser);
if( savedInstanceState == null)
{
viewRootHTML = findViewById( R.id.linearLayoutHTML);
viewRootHTML.getViewTreeObserver().addOnGlobalLayoutListener( onGlobalLayoutListener);
webviewHTML = (WebView) findViewById( R.id.webviewHTML);
WebSettings webSettings = webviewHTML.getSettings();
webSettings.setJavaScriptEnabled( true);
webSettings.setJavaScriptCanOpenWindowsAutomatically( true);
webviewHTML.setWebViewClient( new wiewClient());
webviewHTML.loadUrl( ApplicationLeta.sAppInterviewURL);
}
}
}
当用户长按输入字段时,此代码还会关闭系统消息“编辑文本/输入法”。
但是!这段代码仅在键盘打开后才关闭键盘。键盘会保持可见状态几毫秒,用户(快速用户)可以在键盘上按任意键。这不是最佳情况。
也许存在一种更好的方式可以在不打开键盘的情况下完全禁用键盘吗?