android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
2011-07-20 14:03
846 查看
1:androidmanifest.xml的内容
2:布局文件main.xml的内容
4:HtmlService的内容
5:StreamTool类
6:程序界面与效果图
当下拉时右边会显示一个下拉的列表
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itcast.htmlview" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
2:布局文件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" > <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/html" /> </ScrollView> </LinearLayout>3:MainActivity的内容
package com.itcast.htmlview; import java.io.IOException; import com.itcast.service.HtmlService; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView)findViewById(R.id.html); String path = "http://www.163.com/"; try { String htmlContent = HtmlService.getHtmlContent(path); tv.setText(htmlContent); } catch (IOException e) { Log.e(TAG, e.toString()); Toast.makeText(MainActivity.this, "连接超时", Toast.LENGTH_LONG); } } }
4:HtmlService的内容
package com.itcast.service; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import com.itcast.utils.StreamTool; public class HtmlService { public static String getHtmlContent(String path) throws IOException { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStream inputStream = conn.getInputStream(); //通过输入流获得网站数据 byte[] getData = StreamTool.readInputStream(inputStream); //获得网站的二进制数据 String data = new String(getData, "gb2312"); return data; } }
5:StreamTool类
package com.itcast.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamTool { /* * 从数据流中获得数据 */ public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); } }
6:程序界面与效果图
当下拉时右边会显示一个下拉的列表
相关文章推荐
- android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
- Android UI设计之<六>使用HTML标签,实现在TextView中对部分文字进行加粗显示
- Android入门:向TextView添加滚动条(添加<ScrollView>标签)
- sp使用<c:choose>标签来控制内容显示
- jsp使用<c:choose>标签来控制内容显示
- Android中View绘制优化二一---- 使用<include />标签复用布局文件
- android内嵌页中使用<img>后面的图片是一大串数据流形式显示
- android定位已获取经纬度,上传google解析后textview不显示,基础问题,求大神解答~~>_<`
- Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结: 先看下面的代码: <shape>
- 在xml文件里使用<fragment>报android.view.InflateException: Error inflating class fragment
- android 中 <AutoCompleteTextView>标签的使用
- 从零开始学android<TextView的使用.四.>
- android自定义Spinner下拉菜单和AutoCompleteTextView自动显示的(下拉列表框)样式
- <Android>使用缓冲区实时写入文件一
- 我的Android进阶之旅------> Android在TextView中显示图片方法
- 从零开始学android<EditText组件的使用.六.>
- 以Android Library的方式使用<工程> for <Vitamio >
- SQL中NTEXT字段内容显示<long text>的原因
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- <div style="display:none">中更改的内容使用js无法获取