1.创建一个包含ScrollView的布局文件:
2.实现ScrollView的刷新
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener{
private Button btnExit;
private TextView text;
private ScrollView scroll;
private boolean bool = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnExit = (Button) findViewById(R.id.exit);
text = (TextView) findViewById(R.id.text);
scroll = (ScrollView) findViewById(R.id.scroll);
btnExit.setOnClickListener(this);
//在主线程main中开一子线程来刷新局部的ScrollView
new Thread(new RefreshThread()).start();
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.exit :
finish();
break;
}
}
public class RefreshThread implements Runnable{
@Override
public void run() {
while(bool) {
try {
Thread.sleep(5000);//每五秒刷新一次
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
//用post方法刷新
text.post(new Runnable() {
@Override
public void run() {
text.append("Hello World...n");
}
});
}
}
}
}
@Override
protected void onDestroy(){
super.onDestroy();
bool = false;
}
}