从编辑文本中删除 URL 时,Webview 会自动获得焦点 [英] Webview get focus automatically when removing URL from Edit Text

查看:22
本文介绍了从编辑文本中删除 URL 时,Webview 会自动获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one Edit Text, 5 buttons and a Webview. Webview load url when user type url in edit text and tap Go button or hit Enter on soft keyboard. I have three problems here.

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

1-WebView loads website for example "www.google.com" but soft keyboard does not hide itself. I want to hide it like other browser do.

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

2-When user open a specific url and then try to remove it from Edit Text to open a different url, webview start loading that incomplete url and get focus automatically on every letter removing. For example user loads "www.google.com" then he try to remove url from end and as he remove "m", webview try to load "www.google.co" then edit text is out of focus and webview stole focus from it then user have to tap in edit text again to remove url but it tries to load after every letter removing. I need Webview to load url only when user tap on Go button or hit Enter on soft keyboard.

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

Update (forgot to add third problem, here it is)

3-我可以在哪里检查 WebView CanGoBack 和 CanGoForward,因为我想在启动时将后退和前进按钮的可见性设置为 false,并在它们 CanGoBack 和 CanGoForward 时启用这两个按钮.我应该把我的一段代码放在哪里来检查这些条件?

3-Where can i check when WebView CanGoBack and CanGoForward because i want to set visibility of Back and Forward buttons to false at startup and enabled these two buttons while they CanGoBack and CanGoForward. Where should i put my piece of code to check these conditions?

我希望你们能理解我想说的话.对不起,我的英语不好.

I hope you guys can understand what i am trying to say. Sorry for my poor english.

这是我的 xml 代码.

Here is my xml code.

    <?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代码.

Here is my java code.

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();
    }

}

}

推荐答案

使用此方法隐藏键盘:

 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() 方法中调用它,例如:

call it in your load() method like:

hideSoftKeyboard(MainActivity.this);

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

For your second question you need to call your method inside the condition like:

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 并检查如下条件:

For the webview go back feature: override onKeyDown in your activity and check condition like:

 @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() 中添加检查并相应地显示您的按钮..

You can add the check in your onResume() and load() also and show your button accordingly..

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

这篇关于从编辑文本中删除 URL 时,Webview 会自动获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆