2017-08-08 120 views
0

在我的應用程序中有一個webView與默認網站谷歌。我在任何網頁的某些應用程序中看到頂部有一個地址欄,因此用戶可以隨時輸入新的網站地址。我們怎麼做到這一點?Android瀏覽器地址欄

+0

歡迎堆棧溢出,請閱讀https://stackoverflow.com/help/how-to-ask和更新你的問題是更合適,你嘗試過什麼或讀過什麼? –

+0

可能的重複:https://stackoverflow.com/questions/35496549/change-url-at-runtime-in-webview-in-android –

+0

[在android中webview中運行時更改URL]可能的重複(https:// stackoverflow.com/questions/35496549/change-url-at-runtime-in-webview-in-android) –

回答

0

以下是您需要做的最低限度的事情。您必須根據您的需要進行加強。

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; 
     } 
    }); 
} 
} 
相關問題