第二个类名为 ViewBrowser.java 此文件使用WebViewClient扩展。
通过在此java文件中使用此客户端服务,有2个功能Overrode
1 - shouldOverrideUrlLoading(WebView视图,String url)
2 - onPageFinished(WebView视图,字符串网址)
我使用了 view.getTitle(); 我想在MainActivity类中将此标题设置为TextView。任何人都可以帮助我。
这是我的MainActivity.java类:
public class MainActivity extends Activity implements OnClickListener {
Button bttnSearch;
EditText txttInput;
TextView txttView;
WebView webbBrowser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
bttnSearch = (Button) findViewById(R.id.btnGo);
txttInput = (EditText) findViewById(R.id.txtInput);
txttView = (TextView) findViewById(R.id.txtView);
webbBrowser = (WebView) findViewById(R.id.broWebView);
webbBrowser.setWebViewClient(new ViewBrowser());
webbBrowser.getSettings().setLoadsImagesAutomatically(true);
webbBrowser.getSettings().setJavaScriptEnabled(true);
webbBrowser.getSettings().setLoadWithOverviewMode(true);
webbBrowser.getSettings().setUseWideViewPort(true);
webbBrowser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webbBrowser.setScrollbarFadingEnabled(true);
webbBrowser.loadUrl("http:/www.google.com");
bttnSearch.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String urlText;
urlText = txttInput.getText().toString();
switch(v.getId()){
case R.id.btnGo:
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
webbBrowser.loadUrl(urlText);
break;
}
} }
这是我的ViewBrowser.java:
public class QaziViewBrowser extends WebViewClient {
//private Toast txttView;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// i want to get Title text of the web page and set to txttView
}
}
答案 0 :(得分:1)
在你的活动中创建注册这样的广播
registerReceiver(updateText, new IntentFilter("UPDATE_TEXT"));
onDestroy取消注册
unregisterReceiver(updateText);
制作广播接收器
BroadcastReceiver updateText = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive()");
// check if intent is not null and other verification if needed
TextView.setText(intent.getStringExtra("title"));
}
};
在你的webview课程中,你要做的就是将标题置于意图
中 Intent intentToBroadcast = new Intent("UPDATE_TEXT");
intentToBroadcast.putExtra(title, webview.getTitle());
mContext.sendBroadcast(intentToBroadcast);
我没有测试运行此代码,但这是可行的逻辑,希望它有所帮助。
答案 1 :(得分:1)
非常简单。
在回调onPageFinished中,您可以获取已加载页面的标题。
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// i want to get Title text of the web page and set to txttView
String title = view.getTitle();
// Use title where ever and which ever way you want.
}
答案 2 :(得分:0)
您可以使用Jsoup库(下载链接:http://jsoup.org/)。
和onPageFinished()方法使用下面的代码
onPageFinished(WebView view, String url){
new ContentDownloader(url).execute();
}
private class ContentDownloader extends AsyncTask<Void, Void, Void>
{
Document document;
String url;
public ContentDownloader(String p_url)
{
url = p_url;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
m_progressDialog = ProgressDialog.show(MainActivity.this, "Wait", "Content Downloading..");
}
@Override
protected Void doInBackground(Void... params)
{
try
{
document = Jsoup.connect(url).post();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
if(document != null) {
m_tvTitle.setText(m_document.title());
}
m_progressDialog.dismiss();
}