简体   繁体   中英

How to display AlertDialog with ImageView and EditText without layout.xml?

I have created an alert dialog with an image. However, I need to display the image together with the EditText programatically.

Here's the code..

    final EditText input = new EditText(getActivity());

    ImageView imageView = new ImageView(((EwiseDemoApplication) getActivity().getApplication()).getApplicationContext());
    Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapDecodedString, 0, bitmapDecodedString.length);
    imageView.setImageBitmap(bitmap);

    input.setTransformationMethod(PasswordTransformationMethod.getInstance());

    AAlertDialog alertDialog =  new AlertDialog.Builder(getActivity())
            .setView(imageView)
            .setPositiveButton("Submit",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            //some method here
                        }
                    }
            )
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    }
            )
            .create(); 
    //other codes here

Please try this code:

private void displayDialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
    alert.setTitle("Your Title");

    EditText editText = new EditText(this);
    alert.setView(editText);

    ImageView imageView = new ImageView(this);
    alert.setView(imageView);

    alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
           //Do stuff
        }
    });

    alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //Do stuff
        }
    });
    alert.show();
}

您需要创建一个布局并将其设置为您的 alertDialog 的视图。或者您可以使用 DialogFragment。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM