Android:启动对话框,返回活动结果

时间:2015-08-07 00:57:58

标签: java android android-intent android-alertdialog start-activity

我想从我的活动开始一个对话框 对话框返回一些对我有用的结果 我怎样才能做到这一点?
我不知道在“?”(下面提到)中放什么,因为我的DialogClass扩展了Fragment而不是活动 请更正以下代码:

我的活动中的代码段:

    buttonDone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     Intent intent = new Intent(this,?);
                     startActivityForResult(intent,1);
                }
            });

//On Activity result method
 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) return;
        if (requestCode == GlobalVariables.REQUEST_CODE_LIST_NAME) {
                 Log.d("Result received","");
}
}

我的DialogFragment:

public static class MyDialogClass extends DialogFragment
    {
        private String textListName;
        @Override
        public Dialog onCreateDialog(Bundle bundle)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(TagClass.ENTER_NAME);
            final EditText input = new EditText(getActivity());
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            builder.setView(input);
            builder.setPositiveButton(TagClass.OK, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    textListName = input.getText().toString();

                    if (!textListName.equals("")) {
                        Intent i = new Intent();
                        i.putExtra("ListName", textListName);

                        startActivityForResult(i,1);

                    } else
                        input.setError(TagClass.ERROR_BLANK_FIELD);
                }
            });
            builder.setNegativeButton(TagClass.CANCEL, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            return builder.create();
        }
    }

1 个答案:

答案 0 :(得分:1)

检查此代码段,它描述了如何在活动中显示片段。

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this     fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();