如何在CountdownTimer上关闭onFinish后关闭活动

时间:2015-03-09 18:57:49

标签: android android-activity countdowntimer

我无法在内部类CountDownTimer中引用我的activity类。每当我使用静态引用来调用finish()来关闭Activity。它会提示错误,因为onFinish()方法不是静态方法。请帮助:)初学者。

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(start,interval);
    }

}

private static class CooldownTimer extends CountDownTimer{

    private CooldownTimer(long startTime, long interval){
        super(startTime,interval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        long currentTimeInSeconds = millisUntilFinished/1000;
        String current = "" + currentTimeInSeconds;
        strTime.setText(current);
    }

    @Override
    public void onFinish() {
        TimerActivity.this.finish();
    }



}

}

1 个答案:

答案 0 :(得分:0)

让你的编码像这样

public class TimerActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);
}

public static class PlaceholderFragment extends Fragment {
    private static TextView strTime;
    private final static long start = 60000;
    private final static long interval = 1000;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.activity_timer,
               container, false);
        return null;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);
        strTime = (TextView) view.findViewById(R.id.txtTime);


        new CountDownTimer(start, interval) {
            @Override
            public void onTick(long millisUntilFinished) {
                long currentTimeInSeconds = millisUntilFinished/1000;
                String current = "" + currentTimeInSeconds;
                strTime.setText(current);
            }

            @Override
            public void onFinish() {
                getActivity().finish();

            }
        }.onTick(1000);
    }

    }

}