我正在使用TimeDurationPicker在Android应用中进行对话我正在写作。我希望用户输入计时器的持续时间,并将该持续时间传递回调用它的活动。我知道在这个问题上已经回答了关于SO的问题,但我还没有能够得到任何工作。
这是活动:
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应用中的第一次尝试,所以我不知道还能做什么。
我非常感谢您的帮助!
答案 0 :(得分:1)
想出来了!
我将此添加到我的活动中:
// 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);
}
片段现在根据需要将持续时间传递给活动。
答案 1 :(得分:0)
为了让您的Activity从DialogFragment接收持续时间,您需要创建一个界面。这是一个例子
public interface DurationListener {
void onDurationSet(long duration);
}
然后您的活动应该实现此界面。换句话说,活动必须通过实现onDurationSet()方法来遵循接口契约的条款。
public class TrainActivity extends AppCompatActivity implements DurationListener {
//skipping most of your code
@Override
public void onDurationSet(long duration) {
//Do something with the duration
}
}
现在在DialogFragment中,您需要更改构造函数以接受DurationListener,并且当用户更改持续时间时,您需要在侦听器上调用onDurationSet()。
public class RestDurationPicker extends TimeDurationPickerDialogFragment {
private final DurationListener mListener;
public RestDurationPicker() {}
//The modified constructor, which accepts a listener as a parameter
public RestDurationPicker(DurationListener listener) {
mListener = listener;
}
//Most of your code goes here
@Override
public void onDurationSet(TimeDurationPicker view, long duration) {
//When the duration is set by the user, notify the listener
listener.onDurationSet(duration);
}
}
现在,当您创建DialogFragment时,只需将Activity传递给Fragment即可完成!
//This code is in your onCreate method in your activity
breakTimerOutput.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//customTimerlength = timerLengthInputAlert();
new RestDurationPicker(this).show(getFragmentManager(), "Session break length");
}
});
SIDENOTE:您应该确保您的代码遵循Java和Android编码指南,即使用类和方法的正确命名约定,并保持类字段为私有或受保护,除非您有理由将它们公开。此链接可以帮助您:https://source.android.com/source/code-style.html