我在编写的Android应用程序中使用TimeDurationPicker作为对话框。我希望用户输入计时器的持续时间,并将该持续时间传递回调用它的活动。我知道已经有关于此问题的解答,但是我还无法解决任何问题。

这是活动:

public class train extends AppCompatActivity {
    public Integer customTimerlength = null;
    public Integer timerDurationSeconds = 180;  // 3 minutes is a good default value
    public boolean timerIsPaused;
    public long millisLeftOnTimer;
    Button startBreakTimerButton;
    Button stopBreakTimerButton;
    Button pauseBreakTimerButton;
    TextView breakTimerOutput;
    CountDownTimer countdowntimer;
    private CountDownTimer mCountDownTimer;

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

        startBreakTimerButton = (Button) findViewById(R.id.startBreakTimer);
        stopBreakTimerButton = (Button) findViewById(R.id.stopBreakButton);
        pauseBreakTimerButton = (Button) findViewById(R.id.pauseBreakButton);
        breakTimerOutput = (TextView) findViewById(R.id.breakTimerOutput);

        // Break timer long-click set time
        breakTimerOutput.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                //customTimerlength = timerLengthInputAlert();

                new RestDurationPicker().show(getFragmentManager(), "Session break length");


这是片段:

import android.widget.Toast;
import mobi.upod.timedurationpicker.TimeDurationPicker;
import mobi.upod.timedurationpicker.TimeDurationPickerDialogFragment;

public class RestDurationPicker extends TimeDurationPickerDialogFragment {

    @Override
    protected long getInitialDuration() {
        return 0;  // Default to empty
    }

    @Override
    protected int setTimeUnits() {
        return TimeDurationPicker.MM_SS;
    }

    @Override
    public void onDurationSet(TimeDurationPicker view, long duration) {
        Toast.makeText(getContext(), "New break duration set", Toast.LENGTH_LONG).show();
    }
}


我在这里找到了很多关于意图和接口的答案,但是我还没法做任何事情,我很茫然。这是我第一次尝试使用Android应用程序,因此我不确定该怎么做。

非常感谢您的帮助!

最佳答案

弄清楚了!

我将此添加到我的活动中:

// Break timer long-click set time
@Override
public void onDurationSet(long duration) {
    Integer i = (int) (long) duration;  // get integer i from duration (long)
    customTimerlength = i / 1000; // convert millis to seconds

    // Set the timer duration in seconds
    timerDurationSeconds = customTimerlength;

    // Assign the new custom timer duration to the timerduration variable
    breakTimerOutput.setText(Integer.toString(timerDurationSeconds));
    Log.d("NewTimer", "New Timer Duration: " + timerDurationSeconds);
}

public interface DurationListener {
    void onDurationSet(long duration);
}


现在,片段将持续时间传递给活动。

关于java - 从对话框 fragment 中获取变量以返回到调用它的 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41352788/

10-12 01:52