2012-04-03 27 views
0

,所以我想顯示在我的應用程序這支戰隊陣容與此代碼在活動滾動型文本從URL

 /* 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); 
    } 
} 

但是如何啓用,因爲它不是用我所做的佈局滾動:名冊.XML? 那麼,我如何得到它的工作,以便我可以滾動到名單上更進一步的名單?

回答

0

你可以採取滾動視圖作爲

ScrollView sv = new ScrollView(this); 

那麼你的TextView添加到此滾動型

sv.addView(tv); 

然後設置這個滾動視圖爲內容查看像

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); 
    } 
} 
+0

請你糾正我的代碼?我真的不知道如何使用你發佈的代碼:)有沒有辦法設置背景顏色? – SnoX 2012-04-03 17:46:10

+0

我已經將scrollview添加到您的代碼中;然後將textview添加到滾動視圖並將該滾動視圖設置爲contentview。 Jus試試這個代碼,我認爲它將主要工作。 – 2012-04-03 17:53:06

+0

請告訴我在哪裏放置代碼,我不明白。你能設置一個bgcolor嗎? – SnoX 2012-04-03 17:56:52