如何使用ImageView和EditText显示AlertDialog而不使用layout.xml?

时间:2017-01-18 23:15:57

标签: java android

我已经创建了一个带图像的警告对话框。但是,我需要以编程方式与EditText一起显示图像。

这是代码..

    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

2 个答案:

答案 0 :(得分:1)

请尝试以下代码:

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

答案 1 :(得分:-1)

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