Android 从url在文本中滚动查看
所以我尝试在我的应用程序中显示这个氏族花名册,并在活动中使用此代码Android 从url在文本中滚动查看,android,url,scrollview,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 {
/* 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?
那么,如何使其工作,以便滚动到列表中更靠下的名称呢?您可以将滚动视图视为
ScrollView sv = new ScrollView(this);
然后将文本视图添加到此滚动视图
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);
}
}
你能帮我改一下密码吗?我真的搞不懂如何使用你发布的代码:有没有办法设置背景色?我在你的代码中添加了滚动视图;然后将textview添加到scrollview并将该scrollview设置为contentview。只要试试这个代码,我想它大部分都能用。请告诉我把代码放在哪里,我不明白。你能设置一个bgcolor吗?告诉我你把你的TextView tv=new TextView这个;我已经编辑了我的旧答案并编写了正确的代码,请将该代码在您的系统中进行检查