来自url的文本中的ScrollView

时间:2012-04-03 17:22:10

标签: android url scrollview

所以我试图在我的应用程序中使用此代码在活动

中显示这个战队名单
        /* We will show the data we read in a TextView. */ 
        TextView tv = new TextView(this); 

        /* Will be filled and displayed later. */ 
        String myString = null; 
        try { 
             /* Define the URL we want to load data from. */ 
            //http://androidtest.host.org/roster.txt
             URL myURL = new URL( 
                       "http://androidtest.host.org/roster.txt"); 
             /* Open a connection to that URL. */ 
             URLConnection ucon = myURL.openConnection(); 

             /* Define InputStreams to read 
              * from the URLConnection. */ 
             InputStream is = ucon.getInputStream(); 
             BufferedInputStream bis = new BufferedInputStream(is); 

             /* Read bytes to the Buffer until 
              * there is nothing more to read(-1). */ 
             ByteArrayBuffer baf = new ByteArrayBuffer(50); 
             int current = 0; 
             while((current = bis.read()) != -1){ 
                  baf.append((byte)current); 
             } 

             /* Convert the Bytes read to a String. */ 
             myString = new String(baf.toByteArray()); 
        } catch (Exception e) { 
             /* On any Error we want to display it. */ 
             myString = e.getMessage(); 
        } 
        /* Show the String on the GUI. */ 
        tv.setText(myString); 
        this.setContentView(tv); 
    }
}

但是我如何启用滚动,因为它没有使用我所做的布局:Roster.xml? 那么我如何让它工作,以便我可以滚动到列表中更进一步的名称?

1 个答案:

答案 0 :(得分:0)

您可以将滚动视图作为

    ScrollView sv = new ScrollView(this);

然后将TextView添加到此ScrollView

    sv.addView(tv);

然后将此scrollview设置为ContentView,如

    this.setContentView(sv);

将您的代码设置如下:

//     take here a scrollview    ///////////////////////////////////////
    ScrollView sv = new ScrollView(this);

    /* We will show the data we read in a TextView. */ 
    TextView tv = new TextView(this); 

    /* Will be filled and displayed later. */ 
    String myString = null; 
    try { 
         /* Define the URL we want to load data from. */ 
        //http://androidtest.host.org/roster.txt
         URL myURL = new URL( 
                   "http://androidtest.host.org/roster.txt"); 
         /* Open a connection to that URL. */ 
         URLConnection ucon = myURL.openConnection(); 

         /* Define InputStreams to read 
          * from the URLConnection. */ 
         InputStream is = ucon.getInputStream(); 
         BufferedInputStream bis = new BufferedInputStream(is); 

         /* Read bytes to the Buffer until 
          * there is nothing more to read(-1). */ 
         ByteArrayBuffer baf = new ByteArrayBuffer(50); 
         int current = 0; 
         while((current = bis.read()) != -1){ 
              baf.append((byte)current); 
         } 

         /* Convert the Bytes read to a String. */ 
         myString = new String(baf.toByteArray()); 
    } catch (Exception e) { 
         /* On any Error we want to display it. */ 
         myString = e.getMessage(); 
    } 
    /* Show the String on the GUI. */ 
    tv.setText(myString); 
//    add your textview to scrollview    /////////////////////////////////////
        sv.addView(tv);

//    NOW set scrollview as your contentview    /////////////////////////////
        this.setContentView(sv); 
    }
}