警报对话框修改

时间:2017-11-03 16:43:31

标签: android alert alertdialog

我制作了一个带有两个Edittext视图的警报对话框。 现在我想把imageView放在上面,imageview应该显示4个Drawables,当你从中选择任何一个时,它会在ImageView中加载选中的Drawable

现在我不知道我是怎么做到的。

  AlertDialog.Builder alert = new AlertDialog.Builder(this);
    Context context = this;
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);

    alert.setTitle("Shot On Logo Design");
    alert.setMessage("Shot On Logo");
    final EditText titleBox = new EditText(context);
    titleBox.setHint("Which Phone you are using");
    layout.addView(titleBox);

    final EditText descriptionBox = new EditText(context);
    descriptionBox.setHint("Who took the picture");
    layout.addView(descriptionBox);
    alert.setView(layout);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //What ever you want to do with the value
            //OR
            String YouEditTextValue2 = titleBox.getText().toString();

            shotText.setText("Shot on "+ YouEditTextValue2);

            String YouEditTextValue3 = descriptionBox.getText().toString();
            byText.setText("By: "+ YouEditTextValue3);


            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);


        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // what ever you want to do with No option.
        }
    });

    alert.show();

1 个答案:

答案 0 :(得分:0)

这是DialogFragment的一般实现。您可以根据需要更改以下代码 - 甚至可以根据需要自定义布局。这只是您制作和实施自定义DialogFragment的一个示例。

以下是我用于简单' OK' /'取消'对话。我发现它非常灵活,因为您可以为略有不同的任务创建几个类似的对话框。您需要创建一个布局文件 - 这为您提供了很大的功能和风格灵活性。

我的布局文件是fragment_ok_cancel_dialog

为了满足您的要求,只需创建您自己的布局文件,其中包含您需要的所有元素(如图像)。

在调用对话框的Activity中,您需要实现Listener。

implements OkCancelDialogFragment.OkCancelDialogListener

另一个优点是我的代码可以更改标题和消息以满足任何活动的需要。

private void callMyDialog(){ 
    //Customize the title and message as needed
    String title = "This is my dialog title";
    String mess = "This is my dialog message";
    OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess);
    dialog.show(getFragmentManager(), "OkCancelDialogFragment2");
}

现在,您需要在调用DialogFragment

的Activity中实现对话框回调
@Override
public void onFinishOkCancelDialog(boolean submit) {
    if(submit){
        // Do something positive
    }
    else{
        // Do something negative
    }
}

现在是DialogFragment的代码:

public class OkCancelDialogFragment extends DialogFragment {

    private static final String ARG_TITLE = "title";
    private static final String ARG_MESSAGE = "message";

    Context context = null;

    private String title;
    private String message;
    private boolean submitData = false;

    private OkCancelDialogListener mListener;

    public OkCancelDialogFragment() {
    }

    public static OkCancelDialogFragment newInstance(String title, String message) {
        OkCancelDialogFragment fragment = new OkCancelDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_MESSAGE, message);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE);
            message = getArguments().getString(ARG_MESSAGE);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle saveIntsanceState){

        context = getActivity();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View rootView = inflater.inflate(R.layout.fragment_ok_cancel_dialog, null, false);
        final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle);
        final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage);

        titleView.setText(title);
        messView.setText(message);

        builder.setView(rootView)
//                .setTitle(title)
                .setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        submitData = true;
                        if(mListener == null) mListener = (OkCancelDialogListener) context;
                        mListener.onFinishOkCancelDialog(submitData);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        submitData = false;
                        if(mListener == null) mListener = (OkCancelDialogListener) context;
                        mListener.onFinishOkCancelDialog(submitData);
                    }
                });
        return builder.create();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if(mListener == null) mListener = (OkCancelDialogListener) context;
        }
        catch (Exception ex){
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OkCancelDialogListener {
        void onFinishOkCancelDialog(boolean submit);
    }

}

请注意.setTitle(标题)对API 23或更高版本(或者可能是API 21或更高版本?)有效。