gpt4 book ai didi

java - 如何在 WebView 中禁用软键盘

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:41 25 4
gpt4 key购买 nike

我需要在我的 WebView 和 WebView 中的所有编辑文本中禁用打开的软键盘(我无法访问它,因为它在 WebView 中)。

我尝试在我的 list 文件中使用 'android:windowSoftInputMode="stateAlwaysHidden"',但是当我点击可编辑字段时键盘弹出。

在 WebView 中禁用软键盘的正确解决方案是什么?

编辑:

我找到了打开后关闭键盘的解决方案(感谢这篇文章中的@g00dy 和文章中的@Kachi https://stackoverflow.com/a/9108219/1665964):

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% 禁用键盘的最佳方法?

最佳答案

这个答案对我有用(由 Android Weblineindia 提供):https://stackoverflow.com/a/29409478/4813198

Add following code in main (parent) layout in your layout.xml:

>
> android:descendantFocusability="blocksDescendants"
>

and set following properties in your webview:

>    android:focusable="false"
> android:focusableInTouchMode="true"

关于java - 如何在 WebView 中禁用软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17672116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com