在对话框中输入图库视图

发布于 2024-10-24 11:15:15 字数 1109 浏览 5 评论 0原文

我将如何将 GalleryView 放入警报对话框中。我尝试了多种方法,但没有一种有效,并且都导致了错误。非常感谢这

就是我尝试过的

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
case NEWPIC_DIALOG:
            LayoutInflater lo = LayoutInflater.from(this);
            View picDetailView = lo.inflate(R.layout.newpicdialog, null);

            AlertDialog.Builder picDetailBuilder = new AlertDialog.Builder(this);

            Gallery g = (Gallery) findViewById(R.id.NewPicDialogGallery);
            g.setAdapter(new ImageAdapter(DragActivity.this));

            g.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position, long id) {
                    Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });


            picDetailBuilder.setTitle("New Picture");
            picDetailBuilder.setView(picDetailView);
            AlertDialog picDetail = picDetailBuilder.create();

            return picDetail;
        default:
            break;   
    }
    return null;
} 

How would I go about putting a GalleryView into an Alert Dialog. I have tried several ways but none have worked and all have caused errors. Thanks so much for the

This is what I have tried

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
case NEWPIC_DIALOG:
            LayoutInflater lo = LayoutInflater.from(this);
            View picDetailView = lo.inflate(R.layout.newpicdialog, null);

            AlertDialog.Builder picDetailBuilder = new AlertDialog.Builder(this);

            Gallery g = (Gallery) findViewById(R.id.NewPicDialogGallery);
            g.setAdapter(new ImageAdapter(DragActivity.this));

            g.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position, long id) {
                    Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });


            picDetailBuilder.setTitle("New Picture");
            picDetailBuilder.setView(picDetailView);
            AlertDialog picDetail = picDetailBuilder.create();

            return picDetail;
        default:
            break;   
    }
    return null;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2024-10-31 11:15:15

您还必须重写 onPrepareDialog 函数。请尝试以下操作:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case NEWPIC_DIALOG:
            LayoutInflater lo = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View picDetailView = lo.inflate(R.layout.newpicdialog,(Gallery) findViewById(R.id.NewPicDialogGallery);

            AlertDialog.Builder picDetailBuilder = new AlertDialog.Builder(this);

            picDetailBuilder.setTitle("New Picture");
            picDetailBuilder.setView(picDetailView);
            AlertDialog picDetail = picDetailBuilder.create();

            return picDetail;
        default:
            break;   
    }
    return null;
}

    @Override
    protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
        switch (id) {
        case NEWPIC_DIALOG:
            Gallery g = (Gallery) dialog.findViewById(R.id.NewPicDialogGallery);
            g.setAdapter(new ImageAdapter(dialog.getContext()));

            g.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView parent, View v, int position, long  id) {
                  Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();
              }
            });
        }
        super.onPrepareDialog(id, dialog, args);
    }

You have to override the onPrepareDialog function, too. Try the following:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case NEWPIC_DIALOG:
            LayoutInflater lo = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View picDetailView = lo.inflate(R.layout.newpicdialog,(Gallery) findViewById(R.id.NewPicDialogGallery);

            AlertDialog.Builder picDetailBuilder = new AlertDialog.Builder(this);

            picDetailBuilder.setTitle("New Picture");
            picDetailBuilder.setView(picDetailView);
            AlertDialog picDetail = picDetailBuilder.create();

            return picDetail;
        default:
            break;   
    }
    return null;
}

    @Override
    protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
        switch (id) {
        case NEWPIC_DIALOG:
            Gallery g = (Gallery) dialog.findViewById(R.id.NewPicDialogGallery);
            g.setAdapter(new ImageAdapter(dialog.getContext()));

            g.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView parent, View v, int position, long  id) {
                  Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();
              }
            });
        }
        super.onPrepareDialog(id, dialog, args);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文