我用我的应用程序的WebView,我有一个要求改变基于页面的用户应用程序标题上。我怎样才能做到这在Android中的WebView?
我通过以下行iphone这样做
self.title = [网页stringByEvaluatingJavaScriptFromString:@document.title时]
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//添加Progrss栏支持
。this.getWindow()requestFeature(Window.FEATURE_PROGRESS);
的setContentView(R.layout.webview);
//使进度条可见
。getWindow()setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);
mWebView =(web视图)findViewById(R.id.webview); //这是你给的ID
//在main.xml中的web视图
mWebView.getSettings()setJavaScriptEnabled(真)。
。mWebView.getSettings()setSupportZoom(真正的); //缩放控制对网络(不需要这个
//如果ROM支持多点触控
。mWebView.getSettings()setBuiltInZoomControls(真正的); //如果由ROM支持启用多点触摸
//装载网址
叠B = getIntent()getExtras()。
字符串URL = b.getString(URL);
Log.d(TAG,URL+网址);
mWebView.loadUrl(URL);
//设置浏览器客户端,并定义onProgressChanged
//这使得进度条进行更新。
mWebView.setWebChromeClient(新WebChromeClient(){
公共无效onProgressChanged(web视图来看,INT进度){
//使加载URL后杠消失,并改变字符串载入中...
myActivity.setTitle(载入中...);
myActivity.setProgress(进度* 100); //使酒吧后消失网址是装
完成加载后//返回应用程序名称
如果(进度== 100)
myActivity.setTitle(R.string.app_name);
}
});
mWebView.setWebViewClient(新WebViewClient(){
@覆盖
公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
view.loadUrl(URL);
返回true;
//返回super.shouldOverrideUrlLoading(查看,网址);
}
@覆盖
公共无效onLoadResource(web视图查看,字符串URL){
// TODO自动生成方法存根
super.onLoadResource(查看,网址);
}
@覆盖
公共无效onPageFinished(web视图查看,字符串URL){
super.onPageFinished(查看,网址);
ImageView的logoImageView =(ImageView的)findViewById(R.id.logoimage);
logoImageView.setVisibility(View.GONE);
Log.d(TAG,view.getTitle()+ view.getTitle());
myActivity.setTitle(view.getTitle());
}
});
}
解决方案
您将不得不使用自定义WebViewClient来完成这件事。 您将覆盖onPageFinished()方法,因此,当一个新的页面加载完成,你可以设置web视图到适当的标题。
以下是上面的示例实现:
的WebView mWebView =(web视图)findViewById(R.id.mwebview);
mWebView.setWebViewClient(新WebViewClient(){
@覆盖
公共无效onPageFinished(web视图查看,字符串URL){
ExperimentingActivity.this.setTitle(view.getTitle());
}
});
您是打算这样做,你正在初始化你的web视图。 更换ExperimentingActivity,无论你活动的名字。
如果你已经覆盖WebViewClient,只需添加此功能,或者里面的已经存在的功能code。
您可以在类和函数我使用的是这里的详细信息:
Android开发人员:活动 - 的setTitle()
Android开发者:WebViewClient
Android开发者:的WebView
I'm using a WebView in my application, I have a requirement to change the app title based on the page user is on. How can I do this in Android WebView?
I did this in iphone by following line
self.title = [webPage stringByEvaluatingJavaScriptFromString:@"document.title"]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webview );
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById( R.id.webview ); //This is the id you gave
//to the WebView in the main.xml
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true); //Zoom Control on web (You don't need this
//if ROM supports Multi-Touch
mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
// Load URL
Bundle b = getIntent().getExtras();
String url = b.getString("url");
Log.d(TAG, "url " + url);
mWebView.loadUrl(url);
// Sets the Chrome Client, and defines the onProgressChanged
// This makes the Progress bar be updated.
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
//Make the bar disappear after URL is loaded, and changes string to Loading...
myActivity.setTitle("Loading...");
myActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
myActivity.setTitle(R.string.app_name);
}
});
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
// return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
ImageView logoImageView = (ImageView)findViewById(R.id.logoimage);
logoImageView.setVisibility(View.GONE);
Log.d(TAG, "view.getTitle() " + view.getTitle());
myActivity.setTitle(view.getTitle());
}
});
}
解决方案
You'll have to use a custom WebViewClient to get this done. You will override the onPageFinished() method so when a new page finishes loading you can set the webview to the appropriate title.
Below is a sample implementation of the above:
WebView mWebView = (WebView) findViewById(R.id.mwebview);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
ExperimentingActivity.this.setTitle(view.getTitle());
}
});
You're a going to do that where you're initializing your webview. Replace the "ExperimentingActivity" to whatever you activity's name is.
If you're already overriding the WebViewClient, just add this function or the code inside to your already existing function.
You can get more info on the classes and functions I'm using here at:
Android Developers: Activity - setTitle()
Android Developers: WebViewClient
Android Developers: WebView
相关推荐
最新文章