<pre name="code" class="javascript">package com.example.loadhtml;
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView showHtml;
private Button loadHtml;
private WebView loadShowHtml;
String HtmlCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showHtml = (TextView) findViewById(R.id.showHtml);
loadHtml = (Button) findViewById(R.id.loadHtml);
loadShowHtml = (WebView) findViewById(R.id.loadShowHtml);
// showHtml.setText(HtmlCode);
loadHtml.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HtmlCode = getProjectIdFromAssert(MainActivity.this);
loadShowHtml.loadData(HtmlCode, "text/html", "utf-8");
}
});
}
private static String getProjectIdFromAssert(Context context) {
InputStream is = null;
try {
is = context.getResources().getAssets().open("aaa.txt");
if (is == null) {
return null;
}
int i = 0;
byte[] buf = new byte[100];
String info = "";
while ((i = is.read(buf)) != -1) {
info += new String(buf, 0, i);
}
is.close();
return info.trim(); // 去掉2边的空格
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}