如何改变Fragment的TextView内容
1个回答
2016-07-18 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
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));
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询