从DialogFragment获取价值

时间:2012-09-27 13:38:37

标签: android alertdialog

我希望DialogFragment在我被解雇时输入editQuantity中输入的值。

但我没有办法让它发挥作用。我可以通过将值传递到intent来执行此操作,但这会破坏当前活动的进度。

除了通过intent之外还有什么方法可以让我回报价值吗?

package com.example.myprojectname;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.InputType;
import android.util.Log;
import android.widget.EditText;

public class QuantityDialogFragment extends DialogFragment implements OnClickListener { 


    private EditText editQuantity;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        editQuantity = new EditText(getActivity());
        editQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);

        return new AlertDialog.Builder(getActivity())
        .setTitle(R.string.app_name)
        .setMessage("Please Enter Quantity")
        .setPositiveButton("OK", this)
        .setNegativeButton("CANCEL", null)
        .setView(editQuantity)
        .create();

    }

    @Override
    public void onClick(DialogInterface dialog, int position) {
        String value = editQuantity.getText().toString();
        Log.d("Quantity: ", value);
        dialog.dismiss();       
    }
}

2 个答案:

答案 0 :(得分:49)

假设您要将结果转发给调用Activity :)请尝试以下代码片段:

public class QuantityDialogFragment extends DialogFragment implements OnClickListener {

    private EditText editQuantity;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        editQuantity = new EditText(getActivity());
        editQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);

        return new AlertDialog.Builder(getActivity()).setTitle(R.string.app_name).setMessage("Please Enter Quantity")
                .setPositiveButton("OK", this).setNegativeButton("CANCEL", null).setView(editQuantity).create();

    }

    @Override
    public void onClick(DialogInterface dialog, int position) {
        String value = editQuantity.getText().toString();
        Log.d("Quantity: ", value);
        MainActivity callingActivity = (MainActivity) getActivity();
        callingActivity.onUserSelectValue(value);
        dialog.dismiss();
    }
}

并在您的活动中添加:

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QuantityDialogFragment dialog = new QuantityDialogFragment();
        dialog.show(getSupportFragmentManager(), "Dialog");
    }

    /**
     * callback method from QuantityDialogFragment, returning the value of user
     * input.
     * 
     * @param selectedValue
     */
    public void onUserSelectValue(String selectedValue) {
        // TODO add your implementation.
    }
}

答案 1 :(得分:4)

进一步考虑这个想法,我在对话框中创建了一个监听器接口,并在主要活动中实现了它。

public interface OnDialogResultListener {
    public abstract void onPositiveResult(String value);
    public abstract void onNegativeResult();
}

public void setOnDialogResultListener(OnDialogResultListener listener) {
    this.onDialogResultListener = listener;
}

在覆盖onCancel(DialogInterface)onPositiveResult(String)内调用onNegativeResult(),您希望对话框返回值。

注意:在调用dismiss()后不要忘记onPositiveResult()对话框,否则对话框窗口将保持打开状态。

然后在主活动中,您可以为对话框创建一个监听器,如下所示:

QuantityDialogFragment dialog = new QuantityDialogFragment();
dialog.setOnDialogResultListener(new QuantityDialogFragment.OnDialogResultListener() {
        @Override
        public void onPositiveResult(String value) {
            //Do something...
        }

        @Override
        public void onNegativeResult() {
            //Do something...
        }
    });

这将使您的对话更容易在以后重复使用。