从DialogFragment获取价值

我想DialogFragment返回一个值,当我被editQuantity时input在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(); } } 

Solutions Collecting From Web of "从DialogFragment获取价值"

假设你想要结果给调用活动:)试试这个代码片段:

 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. } } 

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

 public interface OnDialogResultListener { public abstract void onPositiveResult(String value); public abstract void onNegativeResult(); } public void setOnDialogResultListener(OnDialogResultListener listener) { this.onDialogResultListener = listener; } 

在要覆盖的onPositiveResult(String) onCancel(DialogInterface)onPositiveResult(String)onPositiveResult(String) ,您希望对话框返回值。

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

然后在主要活动中,您可以创build对话框的侦听器,如下所示:

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

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