Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > android局部ui刷新

刷新Activity中的scrollview示例(局部ui刷新)

作者:

代码很简单,但是很实用,适合在一个Activity中要刷新局部的UI,比如在扫描一维码的时候,要把每次扫描的结果都显示在界面上

1.创建一个包含ScrollView的布局文件:

2.实现ScrollView的刷新

复制代码 代码如下:

package com.example.uirefresh;

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;
}
}

您可能感兴趣的文章:
阅读全文