我需要在我的 WebView 和 WebView 的所有编辑文本中禁用打开的软键盘(我无法访问 thay,因为它在 WebView 中)。
我尝试在我的清单文件中使用 'android:windowSoftInputMode="stateAlwaysHidden"',但是当我单击可编辑字段时会弹出键盘。
在 WebView 中禁用软键盘的正确解决方案是什么?
编辑:
我找到解决方案(感谢这篇文章中的@g00dy 和帖子https://stackoverflow.com/a/9108219/1665964中的@Kachi )在打开后关闭键盘:
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);
}
}
}
当用户长按输入字段时,此代码还会关闭系统消息“编辑文本/输入法”。
但!此代码仅在打开后才会关闭键盘。键盘保持可见几毫秒,用户(快速用户)可以按键盘上的任何键。这不是最好的情况。
也许存在最好的方法来 100% 禁用键盘而不打开它?