如何创建一个自定义对话框,并在android系统收到成效? [英] how to create a custom dialog and receive results in android?

查看:177
本文介绍了如何创建一个自定义对话框,并在android系统收到成效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,当用户点击按钮,在打开的对话​​框。在此对话框中存在有3种选择一个微调:蓝色,红色,绿色。并有一个提交按钮。我想,当用户选择一种颜色并点击提交,在调用者的活动,其串色设置为在对话框中选择颜色。我试试这个:但没有奏效。请帮我....

i have an activity that when user click on button , a dialog open. in this dialog there is a spinner that have 3 choices: Blue,Red,Green. and there is a submit button. i want that when user select a color and click on submit, in caller activity, its String color set to selected color in dialog. i try this: but not worked. please help me....

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


        }
    });

我使用的直接和意图的方式来分配我的颜色字符串到选定值两者。但没有奏效。我哪里有错?

i use both of direct and with intent ways to assign my color String to selected value. but not worked. where i have mistake?

推荐答案

我觉得创建自定义对话框的最佳方式是现在的对话片段,因为简单的对话很有限。例如它的创建与材料设计一个对话的方式。和你有型动物的方式采取从对话的片段信息,第一并的例如第二

I think the best way to create custom dialogs now is the Dialog Fragment, because the simple dialog it's limited. For example it's the way to create a dialogs with material design. And you have a differents ways to take info from dialog fragment, the first and the second for example.

这是基本的code创建一个对话框片段:

This is basic code to create a dialog fragment:

//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");
}

这是基本的对话片段类:

And this is the basic dialog fragment class:

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;
    }
}
}


告诉我,如果我帮助你,良好的编程!


Tell me if I helped you, good programming!

这篇关于如何创建一个自定义对话框,并在android系统收到成效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆