gpt4 book ai didi

javascript - 如何在 EditText 中显示当前 url - android studio

转载 作者:行者123 更新时间:2023-12-02 11:55:32 25 4
gpt4 key购买 nike

我正在尝试制作一个网络浏览器,但我面临一个问题,即我无法在 EditText 中显示当前 url。.从 Webview 显示当前 url。我怎样才能做到这一点。请告诉我应该在哪里放置代码以及需要放置什么代码。请帮助我。

这是我的代码:

public class MainActivity extends AppCompatActivity {

WebView browser;
EditText urlbox;
Button go,forward,backward,refresh,clear;
ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser= (WebView)findViewById(R.id.browser);
urlbox = (EditText)findViewById(R.id.urlbox);
go = (Button)findViewById(R.id.go);
forward = (Button)findViewById(R.id.f);
backward = (Button)findViewById(R.id.b);
refresh = (Button)findViewById(R.id.r);
clear = (Button)findViewById(R.id.c);
progressBar = (ProgressBar) findViewById(R.id.progressbar);

browser.setWebViewClient(new ourviewclient());


browser.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
if(newProgress == 100){
progressBar.setVisibility(view.GONE);
}else {
progressBar.setVisibility(view.VISIBLE);
}
}
});

WebSettings javascript = browser.getSettings();
browser.getSettings().setJavaScriptEnabled(true);



go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String editextvalue = urlbox.getText().toString();

if(!editextvalue.startsWith("http://"))
editextvalue = "http://" + editextvalue;

String url = editextvalue;

browser.loadUrl(url);

//for keyboard hide
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(urlbox.getWindowToken(),0);
}
});

forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (browser.canGoForward())
browser.goForward();
}
});

backward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (browser.canGoBack())
browser.goBack();
}
});

refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
browser.reload();
}
});

clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
browser.clearHistory();
}
});

}
}

最佳答案

试试这个

browser.setWebViewClient(new WebViewClient() {    
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);

// set here your URL in Your EditText
urlbox.setText(url);

}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}

@Override
public void onPageFinished(WebView view, String url) {

}
});

关于javascript - 如何在 EditText 中显示当前 url - android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47628857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com