每当调用onTick方法时,我都无法动态更改textview。但我不认为这个方法被调用,因为它不会改变我的UI。
public class TimerActivity extends ActionBarActivity {
private static TextView strTime;
private static CooldownTimer timer;
private final static long start = 60000;
private final static long interval = 1000;
//private static Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public void onStart() {
super.onStart();
timer.start();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_timer,
container, false);
//handler = new Handler();
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
strTime = (TextView)view.findViewById(R.id.txtTime);
timer = new CooldownTimer(getActivity(),start,interval);
//timer.setMyActivity(getActivity());
}
}
private static class CooldownTimer extends CountDownTimer{
private Activity _context;
private String current;
private CooldownTimer(Activity c,long startTime, long interval){
super(startTime, interval);
this._context = c;
}
@Override
public void onTick(long millisUntilFinished) {
long currentTimeInSeconds = millisUntilFinished/1000;
current = "" + currentTimeInSeconds;
strTime.setText(current);
}
@Override
public void onFinish() {
_context.finish();
}
}
}
需要帮助。谢谢。这是Android初学者。
编辑:
我在这里使用处理程序类从UI线程发布,但仍然没有响应。
public class TimerActivity extends ActionBarActivity {
private static TextView strTime;
private static CooldownTimer timer;
private final static long start = 60000;
private final static long interval = 1000;
private static Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_timer,
container, false);
handler = new Handler();
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
strTime = (TextView)view.findViewById(R.id.txtTime);
timer = new CooldownTimer(getActivity(),start,interval);
timer.start();
//timer.setMyActivity(getActivity());
}
}
public static class CooldownTimer extends CountDownTimer{
private Activity _context;
private String current;
private CooldownTimer(Activity c,long startTime, long interval){
super(startTime, interval);
this._context = c;
}
@Override
public void onTick(long millisUntilFinished) {
long currentTimeInSeconds = millisUntilFinished/1000;
current = "" + currentTimeInSeconds;
handler.post(new Runnable(){
@Override
public void run() {
strTime.setText(current);
}
});
}
@Override
public void onFinish() {
_context.finish();
}
}
}
答案 0 :(得分:0)
我现在得到它,我将activity_timer.xml布局引用到Activity和Fragment,这就是为什么我的片段永远不会像@ user370305那样被实例化。
现在是工作代码:
public class TimerActivity extends ActionBarActivity {
private static TextView strTime;
private static CooldownTimer timer;
private final static long start = 60000;
private final static long interval = 1000;
private static Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
handler = new Handler();
strTime = (TextView)findViewById(R.id.txtTime);
timer = new CooldownTimer(this,start,interval);
timer.start();
}
public static class CooldownTimer extends CountDownTimer{
private Activity _context;
private String current;
private CooldownTimer(Activity c,long startTime, long interval){
super(startTime, interval);
this._context = c;
}
@Override
public void onTick(long millisUntilFinished) {
long currentTimeInSeconds = millisUntilFinished/1000;
current = "" + currentTimeInSeconds;
handler.post(new Runnable(){
@Override
public void run() {
strTime.setText(current);
}
});
}
@Override
public void onFinish() {
_context.finish();
}
}
}
感谢您的帮助!