如何实现自定义AlertDialog查看 [英] How to implement a custom AlertDialog View

查看:169
本文介绍了如何实现自定义AlertDialog查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上AlertDialog 的 Android的文档,它提供了以下指导和示例设置在AlertDialog自定义视图:

<大段引用>如果你想显示一个更复杂的视图,查找名为身体的的FrameLayout,并添加您的看法吧:

 的FrameLayout FL =(的FrameLayout)findViewById(R.id.body);
fl.add(MyView的,新的LayoutParams(FILL_PARENT,WR​​AP_CONTENT));
 

首先,它是pretty的明显,添加()是一个错字,其目的是为 addView()

我很困惑使用R.id.body的第一线。看来,这是AlertDialog的主体元素......但我不能只是进入,在我的codeB / C,它提供了编译错误。哪里R.id.body得到界定或分配或什么?

下面是我的code。我试图用的setView(findViewById(R.layout.whatever)的建设者,但没有奏效。我猜想,因为我没有手动膨胀呢?

  AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setTitle(标题)
    .setCancelable(假)
    .setPositiveButton(去,新DialogInterface.OnClickListener(){

    @覆盖
    公共无效的onClick(DialogInterface对话框,INT ID){
        EditText上的textBox =(EditText上)findViewById(R.id.textbox);
        doStuff();
    }
});

的FrameLayout F1 =(的FrameLayout)findViewById(R.id.body / *目前是错误* /);
f1.addView(findViewById(R.layout.dialog_view));

AlertDialog警报= builder.create();
alert.show();
 

解决方案

您是正确的,那是因为你没有手动充气的。看来,你想提取从活动的布局身体的ID,并且将无法正常工作。

您可能想是这样的:

  LayoutInflater吹气= getLayoutInflater();
的FrameLayout F1 =(的FrameLayout)alert.findViewById(android.R.id.body);
f1.addView(inflater.inflate(R.layout.dialog_view,F1,FALSE));
 

In the Android docs on AlertDialog, it gives the following instruction and example for setting a custom view in an AlertDialog:

If you want to display a more complex view, look up the FrameLayout called "body" and add your view to it:

FrameLayout fl = (FrameLayout) findViewById(R.id.body);
fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));

First off, it's pretty obvious that add() is a typo and is meant to be addView().

I'm confused by the first line using R.id.body. It seems that it's the body element of the AlertDialog ... but I can't just enter that in my code b/c it gives a compile error. Where does R.id.body get defined or assigned or whatever?

Here's my code. I tried to use setView(findViewById(R.layout.whatever) on the builder but it didn't work. I'm assuming because I didn't manually inflate it?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title")
    .setCancelable(false)
    .setPositiveButton("Go", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int id) {
        EditText textBox = (EditText) findViewById(R.id.textbox);
        doStuff();
    }
});

FrameLayout f1 = (FrameLayout)findViewById(R.id.body /*CURRENTLY an ERROR*/);
f1.addView(findViewById(R.layout.dialog_view));

AlertDialog alert = builder.create();
alert.show();

解决方案

You are correct, it's because you didn't manually inflate it. It appears that you're trying to "extract" the "body" id from your Activity's layout, and that won't work.

You probably want something like this:

LayoutInflater inflater = getLayoutInflater();
FrameLayout f1 = (FrameLayout)alert.findViewById(android.R.id.body);
f1.addView(inflater.inflate(R.layout.dialog_view, f1, false));

这篇关于如何实现自定义AlertDialog查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆