自定义对话框未显示

时间:2019-12-19 06:59:18

标签: android android-layout android-fragments dialog

我正在尝试创建一个自定义对话框,但未显示。enter image description here

这是我单击按钮以显示对话框时的样子

这是我的对话框类和mainActivity


这是我的主要活动

public class MainActivity extends AppCompatActivity {
    Button click;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click = findViewById(R.id.click);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AddUserDialog dialog = new AddUserDialog();
                dialog.show(getSupportFragmentManager(), "MyCustomDialog");

            }
        });
    }



}

这是我的AddUserDialog


public class AddUserDialog extends DialogFragment {

    public Button add;
    public EditText userName, UserEmail, userPhone;
   /* public ImageView addPhoto;*/
    public TextView cancel;


    @NonNull
    protected View onCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  

        View v = inflater.inflate(R.layout.add_user_dialog, container, false);

        add = v.findViewById(R.id.button);
        cancel = v.findViewById(R.id.cancel_dialog);
        userName = v.findViewById(R.id.name);
        UserEmail = v.findViewById(R.id.email);
        userPhone = v.findViewById(R.id.phone);
       /* addPhoto = v.findViewById(R.id.addPhoto);
*/
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().dismiss();
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "User added", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                getDialog().dismiss();
            }
        });

        return v;

    }


}

在我遇到的情况下我是否想念任何东西,请告诉我,谢谢。

3 个答案:

答案 0 :(得分:0)

它对我有用,请使用onCreateDialog

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
   View dialog = inflater.inflate(R.layout.date_picker_dialog, null);

    ////your code here 
    pickerDay = dialog.findViewById(R.id.picker_day);
    pickerMonth = dialog.findViewById(R.id.picker_month);
    pickerYear = dialog.findViewById(R.id.picker_year);
    //////////

    builder.setView(dialog);
    return builder.create();
}

答案 1 :(得分:0)

AddUserDialog更改为

public class AddUserDialog extends DialogFragment {

    public Button add;
    public EditText userName, UserEmail, userPhone;
    /* public ImageView addPhoto;*/
    public TextView cancel;

    static AddUserDialog newInstance() {
        AddUserDialog f = new AddUserDialog();
        return f;
    }

    @NonNull
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.add_user_dialog, container, false);

        add = v.findViewById(R.id.button);
        cancel = v.findViewById(R.id.cancel_dialog);
        userName = v.findViewById(R.id.name);
        UserEmail = v.findViewById(R.id.email);
        userPhone = v.findViewById(R.id.phone);
        /* addPhoto = v.findViewById(R.id.addPhoto);
         */
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().dismiss();
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "User added", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                getDialog().dismiss();
            }
        });

        return v;
    }
}

并调用对话框

AddUserDialog dialog = AddUserDialog.newInstance();
dialog.show(getSupportFragmentManager(), "MyCustomDialog");

答案 2 :(得分:-1)

在您的OnClick()中。使用此

AddUserDialo userDialog = new AddUserDialo ( MainActivity.this );
                userdDialog.getWindow ().setBackgroundDrawable ( new ColorDrawable ( Color.TRANSPARENT ) );
                userdDialog.show ();