【问题标题】:Focus issue in android webview cordovaandroid webview cordova中的焦点问题
【发布时间】:2016-03-10 13:09:17
【问题描述】:

大家好,我是科尔多瓦的新手,我在我的 ionic 应用程序中集成了 webview,所以下面是我的代码

Hello.java

public class Hello extends CordovaPlugin {


 @Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {

    if (action.equals("greet")) {



         Context context = cordova.getActivity().getApplicationContext();

          Intent intent = new Intent(context,Main.class);

          cordova.startActivityForResult((CordovaPlugin)this,intent,100);

          callbackContext.success();


        return true;

    } else {

        return false;

       }
    }
  }

Main.java

public class Main extends CordovaActivity {


 WebView mWebView;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_webview);
    mWebView = (WebView) findViewById(R.id.web_view);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDisplayZoomControls(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.requestFocus(View.FOCUS_DOWN);


    mWebView.loadUrl("https://www.google.co.in/");

}

}

编辑 layout_webview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:descendantFocusability = "blocksDescendants"
    android:orientation="vertical"
    android:layout_height="match_parent">


    <WebView
        android:id="@+id/web_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="true"
        />
</LinearLayout>

当我在上面运行代码时,我无法在我的 web 视图中专注于谷歌搜索 edittext,所以知道如何解决这个问题吗?你的所有建议都很有价值

编辑 2

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDisplayZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
mWebView.getSettings().setLightTouchEnabled(true);
mWebView.requestFocus();
mWebView.loadUrl("https://www.google.co.in/");

编辑 3

mWebView = (WebView) findViewById(R.id.web_view);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDisplayZoomControls(true);
    mWebView.getSettings().setSupportZoom(true);

    mWebView.getSettings().setLightTouchEnabled(true);
    mWebView.requestFocus();
    mWebView.loadUrl("https://www.google.co.in/");

    mWebView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                    break;
            }
            return false;
        }
    });

【问题讨论】:

  • jaydroider:检查我的问题中的编辑部分

标签: android cordova webview ionic-framework


【解决方案1】:

试试这个。

删除 android:descendantFocusability = "blocksDescendants" 阻止子布局获得焦点。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">


  <WebView
    android:id="@+id/web_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    />

</LinearLayout>

添加这个。

可能是你启用了敏感触摸。

Webview.requestFocus(View.FOCUS_DOWN|View.FOCUS_UP);

用于灵敏的触摸。

Webview.getSettings().setLightTouchEnabled(true);

备选方案 2。

mWebView.setOnTouchListener(new View.OnTouchListener() { 
@Override
public boolean onTouch(View v, MotionEvent event) {
       switch (event.getAction()) { 
           case MotionEvent.ACTION_DOWN: 
           case MotionEvent.ACTION_UP: 
               if (!v.hasFocus()) { 
                   v.requestFocus(); 
               } 
               break; 
       } 
       return false; 
    }
}); 

备选方案 3

mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
            v.requestFocusFromTouch();  
            break;
    }               
    return false;
 }

});

希望这会有所帮助。

【讨论】:

  • jaydroider:不工作我不能点击按钮,也不能聚焦在 webview 中提供的edittext
  • jaydroider :我做出与你写的一样的改变,但仍然无法集中在 webview 控件内
  • 只尝试添加 webView.requestFocus()。
  • jaydroider:请参阅我的问题中的编辑 2 部分,我按照您的建议进行了更改,但我仍然无法专注于 webview 控件
  • 删除请求焦点代码,焦点向下和向上并检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多