基本上,我创建了一个DialogFragment,其中包含3个按钮和一条消息。消息是,并且整数值从0开始,并且正负按钮允许用户每次单击按钮时将消息中的值递增和递减1。一旦用户单击中性按钮“确定”,我想知道是否可以将消息作为字符串获取并将其返回到主要活动中?如果没有,怎么可能呢?
我知道如何在片段和活动之间传递数据,我只是想知道是否有可能在片段中获取消息并将其存储到字符串变量中
非常感谢
对不起,如果我第一次在这里使用此错误,
public class AddEmployeesDialogFragment extends DialogFragment {
//variables
int value = 0;
AlertDialog.Builder builder;
AlertDialog alertDialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
builder = new AlertDialog.Builder(getActivity());
builder.setTitle("How many Employees do you want to add to");
builder.setMessage((new Integer(value)).toString());
builder.setPositiveButton("+", null);
builder.setNegativeButton("---", null);
builder.setNeutralButton("Ok", null);
// Create the AlertDialog object and return it
return builder.create();
}
@Override
public void onStart(){
super.onStart();
alertDialog = (AlertDialog)getDialog();
if(alertDialog != null)
{
Button positiveButton = (Button)alertDialog.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
value++;
alertDialog.setMessage((new Integer(value)).toString());
}
});
Button negativeButton = (Button)alertDialog.getButton(Dialog.BUTTON_NEGATIVE);
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(value > 0) {
value--;
}
alertDialog.setMessage((new Integer(value)).toString());
}
});
Button neutralButton = (Button)alertDialog.getButton(Dialog.BUTTON_NEUTRAL);
neutralButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
}