如何设置的WebView客户端? [英] How to set webview client?
问题描述
我在你采用Android文档看出
I seen in android documentation where you use
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
要处理时,项目一个web视图中点击。
To handle when items are clicked within a webview.
唯一的问题是我的,是即时通讯设置URL的另一种方法。
The only problem is with me, is that im setting the url in another method.
该HelloWebViewClient将覆盖并且不使用,用户可以从选择的网址。它只是返回null..How可我在骑这种方法使用的URL由用户设置?
The HelloWebViewClient overrides that and doesnt use the url that the user can chose from. It just returns null..How could i over ride this method to use the url set by the user?
当我用它在一个普通的方法与的WebView浏览器的URL被加载;
然后 browser.loadUrl(字符串URL)
The URL is loaded when i use it in a regular method with the WebView browser;
and then browser.loadUrl(String url)
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.shopping);
findIT = (Button)findViewById(R.id.findIT);
edittext = (EditText)findViewById(R.id.item);
type = (RadioGroup)findViewById(R.id.console);
site = (RadioGroup)findViewById(R.id.shopping_group);
findIT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item = edittext.getText().toString();
lookUp();
}
});
}
public void lookUp(){
browser = (WebView) findViewById(R.id.shoppingBrowser);
browser.getSettings().setJavaScriptEnabled(true);
Log.v(item, item);
getUserPreference();
browser.setWebViewClient(new HelloWebViewClient());
browser.loadUrl(url);
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String notuse) {
Log.v("shopping", url+" loaded");
return true;
}
}
public void getUserPreference(){
switch(type.getCheckedRadioButtonId()){
case R.id.item:
console = "item";
break;
case R.id.PS3:
console = "item";
break;
case R.id.item:
console = "item";
break;
}Log.v("item", console);
switch(site.getCheckedRadioButtonId()){
case R.id.store:
url = "http://www.gamestop.com/browse?nav=16k- "+ item +" " + console;
break;
case R.id.store:
url = "http://www.google.com/search?q="+item + " " + console+"&tbm=shop&hl=en&aq=0&oq=where+";
break;
case R.id.store:
url = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dvideogames&field-keywords="+item + " "+ console+"&x=0&y=0";
Log.v("shopping", url);
}
}
}
如果你看到什么即时试图做的用户开始选择他们想从开店哪个网站。并从那里我将它的网址。
If you see what im trying to do the user gets to select what site they want to shop from. and from there i set it to the url.
推荐答案
如果用户是从同一个活动选择的网址,你可以只从成员变量,而不是从参数中的URL引用网址:
If the user is choosing the URL from the same activity you can just reference the URL from the member variable instead of the URL from the parameter:
// Member variable stored to reflect user's choice
private String mUserUrl = "http://stackoverflow.com";
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This line right here is what you're missing.
// Use the url provided in the method. It will match the member URL!
view.loadUrl(url);
return true;
}
}
这告诉你超载的URL装载WebviewClient(事实上导致其加载您希望提供的网址,而不是URL)。
This tells the WebviewClient that you've overloaded the URL loading (and in fact caused it to load the URL that you wish instead of the url supplied).
下面是一些我嘲笑了一个完整的例子:
Here is a complete example of something I mocked up:
public class HelloWebViewActivity extends Activity {
private WebView mWebView = null;
private EditText mInputUrl = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInputUrl = (EditText)findViewById(R.id.input_url);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = mInputUrl.getText().toString();
mWebView.loadUrl(url);
}
});
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
希望这有助于。如果这对你的作品请标明的答案被接受。
Hope this helps. If this works for you please mark the answer as accepted.
这篇关于如何设置的WebView客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!