【问题标题】:Android: Start Dialog Box that return back results from activityAndroid:从活动返回结果的启动对话框
【发布时间】:2015-08-07 00:57:57
【问题描述】:

我想从我的活动中启动一个对话框。
对话框返回一些对我有用的结果。
我怎样才能做到这一点?
我不知道要在“?”(下面提到)中输入什么,因为我的 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();
        }
    }

【问题讨论】:

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


【解决方案1】:

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

// 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多