Android 从 DialogFragment 获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12622742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value from DialogFragment
提问by Homam
I want the DialogFragment
to return a value to me that was entered in editQuantity
when dismissed.
我希望DialogFragment
返回一个在editQuantity
解雇时输入的值给我。
But i am not getting any way to make it work. I can do this by passing the value through the intent
but that destroys the progress of the current activity.
但我没有办法让它发挥作用。我可以通过传递值来做到这一点,intent
但这会破坏当前活动的进度。
Is there any way other than passing through intent
that will return me value?
除了通过之外还有什么方法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();
}
}
回答by Anis BEN NSIR
Assuming that you want to foward result to the calling Activity:) try this code snippet:
假设您想将结果转发给调用 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();
}
}
and on Your activity add :
并在您的活动中添加:
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.
}
}
回答by mrolcsi
Taking this idea a little further, I created a listener interface inside the dialog and implemented it in the main activity.
进一步考虑这个想法,我在对话框中创建了一个侦听器接口,并在主活动中实现了它。
public interface OnDialogResultListener {
public abstract void onPositiveResult(String value);
public abstract void onNegativeResult();
}
public void setOnDialogResultListener(OnDialogResultListener listener) {
this.onDialogResultListener = listener;
}
Call onNegativeResult() inside an overriden onCancel(DialogInterface)
and onPositiveResult(String)
where you want your dialog to return the value.
在覆盖中调用 onNegativeResult()onCancel(DialogInterface)
以及onPositiveResult(String)
您希望对话框返回值的位置。
Note: don't forget to dismiss()
your dialog after calling onPositiveResult()
or the dialog window will stay opened.
注意:dismiss()
调用后不要忘记您的对话框,onPositiveResult()
否则对话框窗口将保持打开状态。
Then inside your main activity you can create a listener for the dialog, like so:
然后在您的主要活动中,您可以为对话框创建一个侦听器,如下所示:
QuantityDialogFragment dialog = new QuantityDialogFragment();
dialog.setOnDialogResultListener(new QuantityDialogFragment.OnDialogResultListener() {
@Override
public void onPositiveResult(String value) {
//Do something...
}
@Override
public void onNegativeResult() {
//Do something...
}
});
This will make your dialog easier to reuse later.
这将使您的对话框以后更容易重用。