首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从对话框片段中获取变量以返回到调用它的活动

从对话框片段中获取变量以返回到调用它的活动
EN

Stack Overflow用户
提问于 2016-12-28 06:08:47
回答 2查看 205关注 0票数 1

在我正在编写的一个安卓应用程序中,我正在使用TimeDurationPicker进行对话。我希望用户输入计时器的持续时间,并将该持续时间传递回调用它的活动。我知道已经有关于这个问题的答案了,但我还没能让任何东西正常工作。

下面是活动:

代码语言:javascript
复制
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");

下面是这段代码:

代码语言:javascript
复制
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应用程序,所以我不知道还能做什么。

我真的很感谢你的提前帮助!

EN

回答 2

Stack Overflow用户

发布于 2016-12-29 00:37:24

想明白了!

我将这个添加到我的活动中:

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2016-12-28 06:59:52

为了让您的活动从DialogFragment接收持续时间,您需要创建一个接口。下面是一个例子

代码语言:javascript
复制
public interface DurationListener {
    void onDurationSet(long duration);
}

然后,您的活动应该实现此接口。换句话说,该活动必须通过实现onDurationSet()方法来遵循接口约定的条款。

代码语言:javascript
复制
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()。

代码语言:javascript
复制
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时,只需将活动传递给片段,就完成了!

代码语言:javascript
复制
//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");
    }
});

附注:您应该确保您的代码遵循Java和Android编码指南,即对类和方法使用正确的命名约定,并保持类字段为私有或受保护,除非您有理由将其设置为公共字段。此链接可帮助您完成此操作:https://source.android.com/source/code-style.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41352788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档