【问题标题】:Webview get focus automatically when removing URL from Edit Text从编辑文本中删除 URL 时 Webview 自动获得焦点
【发布时间】:2017-05-01 04:48:30
【问题描述】:

我有一个编辑文本、5 个按钮和一个 Web 视图。当用户在编辑文本中输入 url 并点击 Go 按钮或在软键盘上按 Enter 时,Webview 加载 url。 我这里有三个问题。

1-WebView 加载网站,例如“www.google.com”,但软键盘不会自行隐藏。我想像其他浏览器一样隐藏它。

2-当用户打开一个特定的 url 然后尝试从编辑文本中删除它以打开不同的 url 时,webview 开始加载该不完整的 url 并自动关注每个删除的字母。例如,用户加载“www.google.com”,然后他尝试从末尾删除 url,当他删除“m”时,webview 尝试加载“www.google.co”,然后编辑文本失焦,webview 从然后用户必须再次点击编辑文本以删除 url,但它会在删除每个字母后尝试加载。只有当用户点击 Go 按钮或在软键盘上按 Enter 时,我才需要 Webview 加载 url。

更新(忘了补充第三个问题,这里是)

3-我在哪里可以检查 WebView CanGoBack 和 CanGoForward 的时间,因为我想在启动时将 Back 和 Forward 按钮的可见性设置为 false,并在 CanGoBack 和 CanGoForward 时启用这两个按钮。我应该把我的代码放在哪里来检查这些条件?

我希望你们能明白我想说什么。对不起我糟糕的英语。

这是我的 xml 代码。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="13"
    tools:context="com.hbss.chatapp.rashidfaheem.hybridsoftwaresolutions.rashidfaheem.youseebrowser.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="4"
        >


        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/etUrl"
            android:layout_weight="3"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="GO"
            android:id="@+id/btnGo"
            />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="4"
        >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Back"
            android:id="@+id/btnBack"
            />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Forward"
            android:id="@+id/btnForward"
            />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Clear"
            android:id="@+id/btnClear"
            />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Reload"
            android:id="@+id/btnReload"
            />


    </LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="11"
    >
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wb"
        />


</LinearLayout>


</LinearLayout>

这是我的 java 代码。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText etUrl;
Button btnGo, btnBack, btnForward, btnClear, btnReload;
WebView wb;
String url;
View v;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wb = (WebView) findViewById(R.id.wb);
    wb.getSettings().setJavaScriptEnabled(true);
    wb.getSettings().setUseWideViewPort(true);
    wb.getSettings().setLoadWithOverviewMode(true);
    wb.setWebViewClient(new ourClient());



    etUrl = (EditText) findViewById(R.id.etUrl);
    btnGo = (Button) findViewById(R.id.btnGo);
    btnBack = (Button) findViewById(R.id.btnBack);
    btnForward = (Button) findViewById(R.id.btnForward);
    btnClear = (Button) findViewById(R.id.btnClear);
    btnReload = (Button) findViewById(R.id.btnReload);

    btnGo.setOnClickListener(this);
    btnReload.setOnClickListener(this);
    btnBack.setOnClickListener(this);
    btnForward.setOnClickListener(this);
    btnClear.setOnClickListener(this);

    etUrl.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (keyEvent.getAction()==KeyEvent.ACTION_DOWN && i==KeyEvent.KEYCODE_ENTER) {
                return true;
            }
            load();
            return false;
        }
    });
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnGo:
            load();
            break;
        case R.id.btnBack:
            if (wb.canGoBack())
                wb.goBack();
            break;
        case R.id.btnForward:
            if (wb.canGoForward())
                wb.goForward();
            break;
        case R.id.btnReload:
            wb.reload();
            break;
        case R.id.btnClear:
            wb.clearHistory();
            break;
    }
}

    public void load(){
    url = etUrl.getText().toString();
    if (!url.startsWith("http://")) {
        url = "http://" + url;
    }
    try {
        wb.loadUrl(url);
        wb.requestFocus();

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), " " + e, Toast.LENGTH_LONG).show();
    }

}

}

【问题讨论】:

  • 对不起,我忘了添加第三个问题,现在添加到我的问题中。

标签: android webview


【解决方案1】:

使用此方法隐藏键盘:

 public static void hideSoftKeyboard(Activity context) {
    InputMethodManager inputManager = (InputMethodManager) context
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputManager != null)
        inputManager.hideSoftInputFromWindow(context.getWindow()
                .getDecorView().getApplicationWindowToken(), 0);
    context.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}

在您的 load() 方法中调用它,例如:

hideSoftKeyboard(MainActivity.this);

对于您的第二个问题,您需要在以下条件中调用您的方法:

etUrl.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if ((keyEvent.getAction()==KeyEvent.ACTION_DOWN) && (i==KeyEvent.KEYCODE_ENTER)){
            load();  //add it here
            return true;
        }
        return false;
    }
});

编辑

对于 webview 后退功能:在您的活动中覆盖 onKeyDown 并检查以下条件:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wb.canGoBack()) {
        wb.goBack();
        //show your back button here
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    //show other buttons
    return super.onKeyDown(keyCode, event);
}

您也可以在onResume()load() 中添加支票并相应地显示您的按钮..

@Override
protected void onResume() {
    super.onResume();
     if(wb.canGoBack()){
        //show back button}
    else{
        //other button
        }
}

【讨论】:

  • 谢谢兄弟。顺便说一句,使用这三行代码隐藏键盘可以吗?查看视图 = this.getCurrentFocus(); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(),0);
  • 非常感谢兄弟。你能回答我之前忘记添加的第三个问题吗,刚刚更新了我的答案。
  • Rafsan Ahmad 兄弟,我刚刚按照您所说的 GoBack 方法执行了此操作,但此 onKeyDown 方法仅在用户按下后退按钮(其单元格上的硬按钮)时才会执行。当可以返回时,我想启用我的返回按钮。作为 Mozilla firefox 提供的。用户打开 Firefox 并打开一个 URL,例如“www.google.com”,当谷歌页面加载时,地址栏附近的 Firefox 后退按钮被启用。我应该在哪里检查这种情况?
  • 是的,我知道您可以在 load()onResume()..method 中添加支票:if(wb.canGoBack()){ //show back button} else{ //other button }
  • 嘿兄弟,很抱歉打扰你,但又提出了一个无辜的问题。如何在这个 WebView 项目中添加进度条?我见过很多关于在 Webview 中包含进度条的示例,几乎所有示例都在 MainActivity 类中创建了一个 webviewclient 类(扩展 webviewclient 的类),但是当我这样做时,它说“MyClient 类是公共的,应该在名为”MyClient.java 的文件中声明" 这就是为什么我需要在另一个文件中创建它,这样我就无法访问在 MainActivity 中声明和初始化的进度条。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多