博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
阅读量:4136 次
发布时间:2019-05-25

本文共 2999 字,大约阅读时间需要 9 分钟。

1:androidmanifest.xml的内容

[html] 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.itcast.htmlview"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16.     <uses-sdk android:minSdkVersion="8" />  
  17.     <uses-permission android:name="android.permission.INTERNET" />  
  18.       
  19. </manifest>   

2:布局文件main.xml的内容

[html] 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ScrollView  
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="fill_parent"  
  10. >  
  11. <TextView    
  12.     android:layout_width="fill_parent"   
  13.     android:layout_height="fill_parent"   
  14.     android:id="@+id/html"  
  15.     />  
  16. </ScrollView>  
  17.   
  18. </LinearLayout>  

3:MainActivity的内容

[java] 
  1. package com.itcast.htmlview;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import com.itcast.service.HtmlService;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private static final String TAG = "MainActivity";  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         TextView tv = (TextView)findViewById(R.id.html);  
  21.         String path = "http://www.163.com/";  
  22.         try {  
  23.             String htmlContent = HtmlService.getHtmlContent(path);  
  24.             tv.setText(htmlContent);  
  25.               
  26.         } catch (IOException e) {  
  27.             Log.e(TAG, e.toString());  
  28.             Toast.makeText(MainActivity.this"连接超时", Toast.LENGTH_LONG);  
  29.         }  
  30.           
  31.           
  32.     }  
  33. }  

4:HtmlService的内容

[java] 
  1. package com.itcast.service;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7.   
  8. import com.itcast.utils.StreamTool;  
  9.   
  10. public class HtmlService {  
  11.       
  12.     public static String getHtmlContent(String path) throws IOException {  
  13.         URL url = new URL(path);  
  14.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  15.         InputStream inputStream = conn.getInputStream();      //通过输入流获得网站数据  
  16.         byte[] getData = StreamTool.readInputStream(inputStream);    //获得网站的二进制数据  
  17.         String data = new String(getData, "gb2312");  
  18.         return data;  
  19.       
  20.     }  
  21. }  

5:StreamTool类

[java] 
  1. package com.itcast.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. public class StreamTool {  
  8.   
  9.     /* 
  10.      * 从数据流中获得数据 
  11.      */  
  12.     public static  byte[] readInputStream(InputStream inputStream) throws IOException {  
  13.         byte[] buffer = new byte[1024];  
  14.         int len = 0;  
  15.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  16.         while((len = inputStream.read(buffer)) != -1) {  
  17.             bos.write(buffer, 0, len);  
  18.         }  
  19.         bos.close();  
  20.         return bos.toByteArray();  
  21.           
  22.     }  
  23. }  

 

6:程序界面与效果图

当下拉时右边会显示一个下拉的列表

转载地址:http://cclvi.baihongyu.com/

你可能感兴趣的文章
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>
HTTP和HttpServletRequest 要点
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>
laravel 修改api返回默认的异常处理
查看>>
laravel事务
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
这才是学习Vite2的正确姿势!
查看>>