2017-02-04 116 views
0

我在android中制作webview。有一个webview和编辑文本和一个去按钮。我希望当webview改变编辑文本自动改变与当前的网址。例如:当我键入一个网址“www.google.com”我去谷歌,如果我去到YouTube的谷歌,但我的编辑文本站作为“www.google.com”。但我想我的编辑文本程序自动更改为当前的WebView 这是我的Java代码Android的webview浏览器编辑文本自动更改与webview更改

package com.example.ashraful.userinterface; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.Window; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 

public class Main2Activity extends Activity { 

WebView web1; 
EditText ed1; 
Button bt1; 
String Address; 
String add; 
ProgressBar pbar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main2); web1 =(WebView)findViewById(R.id.webView1); 
    ed1 = (EditText)findViewById(R.id.editText1); 
    bt1 = (Button)findViewById(R.id.button1); 
    pbar = (ProgressBar)findViewById(R.id.progressBar1); 
    pbar.setVisibility(View.GONE); 

    bt1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Address = "http://" + ed1.getText().toString(); 
      WebSettings webSetting = web1.getSettings(); 
      webSetting.setBuiltInZoomControls(true); 
      webSetting.setJavaScriptEnabled(true); 
      webSetting.setLoadsImagesAutomatically(true); 


      web1.setWebViewClient(new WebViewClient()); 

      web1.loadUrl(Address); 

     } 
    }); 
} 

public class WebViewClient extends android.webkit.WebViewClient 
{ 
    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 

     // TODO Auto-generated method stub 
     super.onPageStarted(view, url, favicon); 
     pbar.setVisibility(View.VISIBLE); 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 

     // TODO Auto-generated method stub 
     view.loadUrl(url); 
     return true; 
    } 
    @Override 
    public void onPageFinished(WebView view, String url) { 

     // TODO Auto-generated method stub 

     super.onPageFinished(view, url); 
     pbar.setVisibility(View.GONE); 
    } 

} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) { 
     web1.goBack(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
} 

的,这是XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main2" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.ashraful.userinterface.Main2Activity"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:text="GO" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_alignParentLeft="true" 
    android:ems="10" 
    android:hint="Type Your URL Here" 
    android:layout_toLeftOf="@+id/button1" 
    android:layout_toStartOf="@+id/button1" /> 

<View 
    android:id="@+id/view12" 
    android:layout_width="wrap_content" 
    android:layout_height="3dp" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_alignParentLeft="true" 
    android:background="#3bbdfa" /> 

<WebView 
    android:id="@+id/webView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/editText1" 
    android:layout_centerHorizontal="true" /> 

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" /> 

</RelativeLayout> 

回答

0

尝试设置EDITTEXT在onPagefinished或/和onPageStarted方法:

ed1.setText(""); // clear 
ed1.setText(view.getUrl()); // Set current url 
+0

可以请你告诉我怎么supporte HTML 5视频我的意思是YouTube视频 –

+0

@AshrafulIslam:那你到底是“支持”是什么意思? –

+0

从上面的代码我做一个浏览器,当我去YouTube和播放视频的视频只显示黑屏。音频播放清晰但视频是黑色的。那么我该如何解决这个问题,你能帮我吗?并感谢您以前的答案是解决我以前的问题.. –