Android Interface Webview
I want to create an interface for my project that has a button and when the button is clicked it begins the activity from another java file which is a library but am having some difficulty. The referenced java file opens a webview. Here is what I have
package com.dcu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.news.*;
public class DCU extends Activity
{
private Button somebutton;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
protected void initControls()
{
somebutton = (Button)findViewById(R.id.News);
somebutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
News n = new News();
n.onCreate(null);
}
});
}
}
Then I have a main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="news" android:id="@+id/News"></Button>
</LinearLayout>
And news.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Here is the News.java
package com.news;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class News extends Activity {
WebView mWebView;
String test2 = "<html><body><table border='0'>";
Document docs;
Document writing;
String text(String link)
{
String full ="<html><body><table border='0'><tr><td align ='center'>";;
try {
writing = Jsoup.connect(link).get();
} catch (IOException e) {
e.printStackTrace();
}
Elements c开发者_StackOverflowlassname = writing.getElementsByClass("news");
Element heading = classname.select("h2").first();
Elements items = classname.select("p");
full = full + "<h1>" + heading.text()+ "</h1>" + "<h2>" + items.get(0).toString() + "</h1>" + "</td></tr>";
Element imgs2 = writing.select("div.News img").first();
String picture = imgs2.absUrl("src");
String newImg = "<img src=\"" + picture + "\" width = '450' align = 'center'>";
full = full + "<tr><td align='center'>" + newImg + "</td></tr>";
full = full + "<tr><td><h1>";
for (int i = 1; i< items.size(); i++)
{
full = full + items.get(i).toString();
}
full = full + "</h1></td></tr></table></body></html>";
return full;
}
public void main(String... args)
{
try
{
docs = Jsoup.connect("http://www.dcu.ie/news/index.shtml").get();
}
catch (IOException e)
{
e.printStackTrace();
}
Elements imgs = docs.select("div.news-feature img");
Elements txt = docs.select("h2");
Elements article = docs.getElementsByClass("date");
Elements links = article.select("a[href]");
for (int i = 0; i < imgs.size(); i++){
String url = imgs.get(i).absUrl("src");
String temp = links.get(i).absUrl("href");
String temp2 = "<a href=\"" + temp + "\">";
String newImg = temp2 + "<img src=\"" + url + "\" align = 'center' width = '300'>"+ "</a>";
test2 = test2 + "<tr>";
test2 = test2 + "<td>";
test2 = test2 + " " + newImg + " ";
test2 = test2 + "</td>";
test2 = test2 + "<td>";
test2 = test2 + "<h1>" + txt.get(i).text() + "</h1>";
test2 = test2 + "</td>";
test2 = test2 + "</tr>";
}
test2 = test2 + "</table>";
test2 = test2 + "</html></body>";
}
public void onCreate(Bundle savedInstanceState) {
main();
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main );
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new NewsClient());
mWebView.setInitialScale(1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadData(test2, "text/html", "utf-8");
final Activity MyActivity = this;
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.loadUrl("http://www.google.com");
//mWebView.loadDataWithBaseURL("", test2,"text/html", "utf-8", "");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return false;
}
return super.onKeyDown(keyCode, event);
}
private class NewsClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (view.canGoBack() == false)
{
String newUrl = text(url);
view.loadData(newUrl, "text/html", "utf-8");
}
else
{
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
view.loadUrl(url);
}
//view.loadDataWithBaseURL("", newUrl,"text/html", "utf-8", "");
return true;
}
}
}
Thanks
Use a Intent to start your activity. Instead of:
News n = new News();
n.onCreate(null);
Use:
Intent i = new Intent(DCU.this, News.class);
DCU.this.startActivity(i);
精彩评论