【问题标题】:how to create a custom dialog and receive results in android?如何在android中创建自定义对话框并接收结果?
【发布时间】:2015-06-12 08:46:46
【问题描述】:

我有一个活动,当用户单击按钮时,会打开一个对话框。在此对话框中有一个微调器,有 3 个选项:蓝色、红色、绿色。并且有一个提交按钮。我希望当用户选择一种颜色并单击提交时,在调用者活动中,其String color 设置为对话框中的选定颜色。我试试这个:但没有奏效。请帮帮我....

String color;
String dialogColor;

showDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.setTitle("my dialog");

            Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner);

            final TextView status = (TextView) dialog.findViewById(R.id.status);
            Button submit = (Button) dialog.findViewById(R.id.submit);


            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    dialogColor = parent.getItemAtPosition(position).toString();
                    status.setText("Color is: "+dialogColor);
                    color = dialogColor;
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("Color",dialogColor);

                dialog.dismiss();
            }
        });

            dialog.show();


        }
    });

我使用直接和有意的方式将我的颜色字符串分配给选定的值。但没有奏效。我哪里错了?

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    我认为现在创建自定义对话框的最佳方式是Dialog Fragment,因为它的简单对话框是有限的。例如,这是创建dialogs with material design 的方法。你有不同的方式从对话片段中获取信息,例如the firstthe second


    这是创建对话框片段的基本代码:

    //Method to call and start dialog fragment class
    public void ShowPhotoFilesDialog(Activity context,File photo){
        //Declaration of classes
        Custom_DialogFragment custom_dialogFragment = new Custom_DialogFragment ();
    
        FragmentManager fragmentManager = context.getFragmentManager();
    
        // The device is using a large layout, so show the fragment as a dialog
        custom_dialogFragment.show(fragmentManager, "dialog");
    }
    

    这是基本的对话框片段类:

    public class Custom_DialogFragment extends DialogFragment {
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        try {
            // The only reason you might override this method when using onCreateView() is
            // to modify any dialog characteristics. For example, the dialog includes a
            // title by default, but your custom layout might not need it. So here you can
            // remove the dialog title, but you must call the superclass to get the Dialog.
            Dialog dialog = super.onCreateDialog(savedInstanceState);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            //To hide action bar from layout
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            //Declaration of controls
            View v = getActivity().getLayoutInflater().inflate(R.layout.my_custom_layout);
            builder.setView(v);
    
            //My code
    
            return builder.create();
        }
        catch (Exception ex){
            Log.e("-- Custom_DialogFragment.onCreateDialog --","",ex);
            return null;
        }
    }
    }
    

    如果我帮助了你,请告诉我,编程很好!

    【讨论】:

    • 我认为这是最简单最有效的方式,很好的编程! :)
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多