实现自定义对话框

时间:2014-01-21 14:13:09

标签: android dialog

我正在实施自定义对话框,但在活动中,我遇到了麻烦。这是代码:

public class MainActivity extends Activity {

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.buttonDialog);

    /*Add button listener*/
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            /*Custom dialog*/
            final Dialog dialog = new Dialog(this);
            dialog.setTitle("Cerrar App");
            dialog.setContentView(R.layout.custom_dialog);

        //...

final Dialog dialog = new Dialog(this);行中,它给我一个错误,上面写着:构造函数Dialog(new View.OnClickListener(){})未定义。

我做错了什么?

3 个答案:

答案 0 :(得分:6)

更改

final Dialog dialog = new Dialog(this);

final Dialog dialog = new Dialog(MainActivity.this);

在您的情况下,这指的是内部onClickListener

答案 1 :(得分:4)

变化:

final Dialog dialog = new Dialog(this);

final Dialog dialog = new Dialog(MainActivity.this);

答案 2 :(得分:-2)

Dialog videoDialog = new Dialog(getApplicationContext());
videoDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
videoDialog.setContentView(R.layout.video_dialoge);

videoDialog.getWindow().setGravity(
        Gravity.BOTTOM | Gravity.CENTER_VERTICAL);

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = videoDialog.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
LinearLayout frmalbumb = (LinearLayout) videoDialog
        .findViewById(R.id.layout_album);
LinearLayout frmcamera = (LinearLayout) videoDialog
        .findViewById(R.id.layout_camera);
frmcamera.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v)
    {
        Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(cameraIntent, capturevideo);
        videoDialog.dismiss();
    }
});
frmalbumb.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
        mediaChooser.setType("video/*");
        startActivityForResult(mediaChooser, galleryVideo);
        videoDialog.dismiss();
    }
});

WindowManager.LayoutParams wmlp = videoDialog.getWindow()
        .getAttributes();
wmlp.gravity = Gravity.BOTTOM;
wmlp.x = (img_btn_Video.getLeft() + (img_btn_Video.getLeft() / 2));
if (CommonUtilities.istablet == true) {
    wmlp.y = 69;
} else {
    if (CommonUtilities.screen_height >= 1920) {
        wmlp.y = 130;
    }else if(CommonUtilities.screen_height<=854)
    {
        wmlp.y = 75;
    }
    else {
        wmlp.y = 90;
    }
}
videoDialog.getWindow().setBackgroundDrawable(
        new ColorDrawable(android.graphics.Color.TRANSPARENT));
    videoDialog.show();