怎么在fragment的里面的textview实现一秒一更新 就像时钟样一秒变一下数字 50
1个回答
展开全部
public class MainActivity extends Activity {
private int index;
private TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
setContentView(mTextView);
new DataThread().start();
}
private String getWebData() {
return String.valueOf(++index);
}
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
mTextView.setText((String)msg.obj);
};
};
private class DataThread extends Thread {
@Override
public void run() {
for(int i = 0; i < 4; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
final String data = getWebData();
// 只能在主线程中修改ui控件
mHandler.sendMessage(mHandler.obtainMessage(0, data));
}
}
}
}
private int index;
private TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
setContentView(mTextView);
new DataThread().start();
}
private String getWebData() {
return String.valueOf(++index);
}
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
mTextView.setText((String)msg.obj);
};
};
private class DataThread extends Thread {
@Override
public void run() {
for(int i = 0; i < 4; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
final String data = getWebData();
// 只能在主线程中修改ui控件
mHandler.sendMessage(mHandler.obtainMessage(0, data));
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询