【问题标题】:Address bar in android browser安卓浏览器地址栏
【发布时间】:2017-08-08 13:26:34
【问题描述】:

在我的应用程序中有一个webView 与默认网站谷歌。我在任何网页上的某些应用程序中看到顶部都有一个地址栏,因此用户可以随时输入新的网站地址。我们怎么能做到这一点?

【问题讨论】:

标签: java android address-bar


【解决方案1】:

以下是您需要做的最低限度的事情。当然,您必须根据需要进行改进。

activity_webview.xml

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

<EditText
    android:id="@+id/et_web_address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textWebEditText"
    android:imeOptions="actionGo"
    android:hint="Enter Url" />

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

WebViewActivity.java

public class WebviewActivity extends AppCompatActivity {

private EditText webAddressView;
private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    webAddressView = (EditText) findViewById(R.id.et_web_address);
    webView = (WebView) findViewById(R.id.webview);

    webAddressView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                String url = webAddressView.getText().toString();
                if (!url.isEmpty())
                    webView.loadUrl(url);
                return true;
            }
            return false;
        }
    });
}
}

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 2012-01-05
    • 2013-10-25
    • 2012-08-31
    相关资源
    最近更新 更多