android中alertdialog中的文本编辑数量可变?

发布于 2024-12-08 19:03:10 字数 448 浏览 1 评论 0 原文

是否可以在警报对话框中设置可变数量的文本编辑?我尝试动态填充一些容器视图,例如 StackView 或 LinearLayout,但据说 AdapterView 不支持 addView 方法(例外)。解决办法是什么?
已添加:
我想从动态信息构建alertdialog。

AlertDialog.Builder alert = new AlertDialog.Builder(context);

现在我可以像这样设置它的视图:

alert.setView(v);

但是 v 元素只能是简单的东西,如 TextView 或 EditText。如果我想创建一些可能包含可变数量元素的容器视图,例如 2 个文本视图和 3 个编辑文本,该怎么办?我该怎么做?现在我只是创建单独的布局文件并用它来膨胀视图,这不是一个解决方案。我能做些什么?

Is it possible to set variable number of textedits in alertdialog? I've tried to fill some container views such as StackView or LinearLayout dynamically but method addView is said to be not supported in AdapterView(exception). What's the solution?
Added:
I want to build alertdialog from the dynamic information.

AlertDialog.Builder alert = new AlertDialog.Builder(context);

now I can set its view like this:

alert.setView(v);

but v element can only be something simple as TextView or EditText. What if I want to create some container view which may contain variable number of elements, for example 2 textviews and 3 edittexts? How can I do this? Now I just create separate layout file and inflate view with it bit it's not a solution. What can I do?

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

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

发布评论

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

评论(5

变身佩奇 2024-12-15 19:03:10

为什么需要可变数量的 TextView?您可以使用一个来显示多行。如果您需要更复杂的东西,您可以使用主题 @android:style/Theme.Dialog 创建您自己的类似对话框的活动,限制其尺寸,使其不会覆盖整个显示区域。

更新:

以下是如何执行类似对话框的子活动的示例:::

ComplexDialog.java

public class ComplexDialog extends Activity {
    ...regular Activity stuff...

    protected void onCreate(Bundle savedInstanceState) {
        ...get extras from the intent, set up the layout, handle input, etc...

        LinearLayout dialogLayout = (LinearLayout) findViewById(R.id.dialogLayout);
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth() > 640 ? 640 : (int) (display.getWidth() * 0.80);
        dialogLayout.setMinimumWidth(width);
        dialogLayout.invalidate();

        ...more regular stuff...
};

:: AndroidManifest.xml

<activity android:name=".ComplexDialog" android:theme="@android:style/Theme.Dialog"></activity>

Why would you need a variable number of TextViews? You could use a single one to display multiple lines. If you need something more complicated, you could create your own dialog-like activity with theme @android:style/Theme.Dialog, constraining its dimensions so that it does not cover the entire display area.

Update:

Here is an example of how to do a dialog-like subactivity:

:: ComplexDialog.java

public class ComplexDialog extends Activity {
    ...regular Activity stuff...

    protected void onCreate(Bundle savedInstanceState) {
        ...get extras from the intent, set up the layout, handle input, etc...

        LinearLayout dialogLayout = (LinearLayout) findViewById(R.id.dialogLayout);
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth() > 640 ? 640 : (int) (display.getWidth() * 0.80);
        dialogLayout.setMinimumWidth(width);
        dialogLayout.invalidate();

        ...more regular stuff...
};

:: AndroidManifest.xml

<activity android:name=".ComplexDialog" android:theme="@android:style/Theme.Dialog"></activity>
玩套路吗 2024-12-15 19:03:10

当用户查看对话框时,EditText 的数量是否会发生变化,或者到那时数量已经固定?如果它是固定的并且仅不时变化,您可以为每个布局构建一个自己的布局 xml,然后使用 switch 语句决定要在对话框中显示哪个布局 xml。

可以使用以下代码显示自定义警报对话框:

// This example shows how to add a custom layout to an AlertDialog
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(
                    R.layout.helpdialog_main, null);
            return new AlertDialog.Builder(Main.this)
                    .setView(textEntryView)
                    .setPositiveButton(R.string.stringHelptextButtonOK,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    // Popup is closed automatically, nothing
                                    // needs to be done here
                                }
                            }).create();

Is the number of EditTexts changing while the user is viewing the dialog or is the number fix by then? If it is fixed and only varies from time to time, you could build an own layout xml for each of them, and then decide with a switch statement which layout xml you want to display in the dialog.

A custom alert dialog can be displayed with this code:

// This example shows how to add a custom layout to an AlertDialog
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(
                    R.layout.helpdialog_main, null);
            return new AlertDialog.Builder(Main.this)
                    .setView(textEntryView)
                    .setPositiveButton(R.string.stringHelptextButtonOK,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    // Popup is closed automatically, nothing
                                    // needs to be done here
                                }
                            }).create();
抱猫软卧 2024-12-15 19:03:10

添加 LinearLayout 应该可以正常工作:

#onCreate(Bundle) 中:

...
...
/*LinearLayout*/ mDlgLayout = new LinearLayout(this);
mDlgLayout.setOrientation(LinearLayout.VERTICAL);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Some title");
alert.setView(mDlgLayout);
alert.setNeutralButton("Regenerate", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dlg, int which) {
            // onClick will dismiss the dialog, just posting delayed
            // to pop up the dialog again & change the layout.
            mDlgLayout.postDelayed(new Runnable() {
                    public void run() {
                        alterDlgLayout();
                    }
                }, 200L);
        }
    });
/*AlertDialog*/ mDlg = alert.create();
...
...

#alterDlgLayout()

void alterDlgLayout() {
    mDlgLayout.removeAllViews();
    Random rnd = new Random(System.currentTimeMillis());
    int n = rnd.nextInt(3) + 1;
    for (int i = 0; i < n; i++) {
        EditText txt = new EditText(this);
        txt.setHint("Some hint" + rnd.nextInt(100));
        mDlgLayout.addView(txt);
    }
    mDlgLayout.invalidate();
    mDlg.show();
}

中 在 #onResume()< /code>

alterDlgLayout();

#onPause()

mDlg.dismiss();

Adding a LinearLayout should work just fine:

In #onCreate(Bundle):

...
...
/*LinearLayout*/ mDlgLayout = new LinearLayout(this);
mDlgLayout.setOrientation(LinearLayout.VERTICAL);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Some title");
alert.setView(mDlgLayout);
alert.setNeutralButton("Regenerate", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dlg, int which) {
            // onClick will dismiss the dialog, just posting delayed
            // to pop up the dialog again & change the layout.
            mDlgLayout.postDelayed(new Runnable() {
                    public void run() {
                        alterDlgLayout();
                    }
                }, 200L);
        }
    });
/*AlertDialog*/ mDlg = alert.create();
...
...

In #alterDlgLayout()

void alterDlgLayout() {
    mDlgLayout.removeAllViews();
    Random rnd = new Random(System.currentTimeMillis());
    int n = rnd.nextInt(3) + 1;
    for (int i = 0; i < n; i++) {
        EditText txt = new EditText(this);
        txt.setHint("Some hint" + rnd.nextInt(100));
        mDlgLayout.addView(txt);
    }
    mDlgLayout.invalidate();
    mDlg.show();
}

In #onResume()

alterDlgLayout();

In #onPause()

mDlg.dismiss();
臻嫒无言 2024-12-15 19:03:10

下面的链接是对话框的静态布局

http://knol.google.com/k/thiyagaraaj-mp/custom-dialog-box-popup-using-layout-in/1lfp8o9xxpx13/171#

如果您想添加动态布局或编辑现有布局,然后您可以通过方法获取它

RelativeLayout myMainLayout= (RelativeLayout)myDialog.findViewById(R.id.myMainLayout);

,并通过在 java 中创建视图并使用 addView() 方法将您选择的视图添加到主布局中。

myMainLayout.addView(yourChildView);

here is the link below is for static layout of dialogbox

http://knol.google.com/k/thiyagaraaj-m-p/custom-dialog-box-popup-using-layout-in/1lfp8o9xxpx13/171#

if you want to add dynamic layout or edit the existing layout then you can get that by method

RelativeLayout myMainLayout= (RelativeLayout)myDialog.findViewById(R.id.myMainLayout);

and add view of your choice to your main layout by creating them in java and using addView() method..

myMainLayout.addView(yourChildView);
江挽川 2024-12-15 19:03:10

最简单的方法是动态地膨胀视图,在对话框创建方法中放入以下代码来构建对话框:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Some title");
ViewGroup final mainLayout = getLayoutInflater().inflate(R.layout.the_custom_holder);
final EditText[] editors = new EditText[requiredItemCount];
for (int i = 0; i < requiredItemCount; i++) {
   View inputter = getLayoutInflater().inflate(R.layout.the_custom_line);
   editors[i] = inputter.findViewById(R.id.editorId);
}

alert.setPositiveButton(R.string.stringHelptextButtonOK,
   new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
      // Accessing one of the edittexts
      String requiredText = editors[3].getText().toString();
      // TODO Do stuff with result
   }
alert.setView(mDlgLayout);
alert.create().show();

The easiest way is to inflate views dinamically, In your dialog creation method put this code to build Dialog:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Some title");
ViewGroup final mainLayout = getLayoutInflater().inflate(R.layout.the_custom_holder);
final EditText[] editors = new EditText[requiredItemCount];
for (int i = 0; i < requiredItemCount; i++) {
   View inputter = getLayoutInflater().inflate(R.layout.the_custom_line);
   editors[i] = inputter.findViewById(R.id.editorId);
}

alert.setPositiveButton(R.string.stringHelptextButtonOK,
   new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
      // Accessing one of the edittexts
      String requiredText = editors[3].getText().toString();
      // TODO Do stuff with result
   }
alert.setView(mDlgLayout);
alert.create().show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文