是否可以在警报对话框中设置可变数量的文本编辑?我尝试动态填充一些容器视图,例如 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?
发布评论
评论(5)
为什么需要可变数量的
TextView
?您可以使用一个来显示多行。如果您需要更复杂的东西,您可以使用主题@android:style/Theme.Dialog
创建您自己的类似对话框的活动,限制其尺寸,使其不会覆盖整个显示区域。更新:
以下是如何执行类似对话框的子活动的示例:::
ComplexDialog.java
:: AndroidManifest.xml
Why would you need a variable number of
TextView
s? 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
:: AndroidManifest.xml
当用户查看对话框时,EditText 的数量是否会发生变化,或者到那时数量已经固定?如果它是固定的并且仅不时变化,您可以为每个布局构建一个自己的布局 xml,然后使用 switch 语句决定要在对话框中显示哪个布局 xml。
可以使用以下代码显示自定义警报对话框:
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:
添加
LinearLayout
应该可以正常工作:在
#onCreate(Bundle)
中:在
#alterDlgLayout()
中 在
#onResume()< /code>
在
#onPause()
中Adding a
LinearLayout
should work just fine:In
#onCreate(Bundle)
:In
#alterDlgLayout()
In
#onResume()
In
#onPause()
下面的链接是对话框的静态布局
http://knol.google.com/k/thiyagaraaj-mp/custom-dialog-box-popup-using-layout-in/1lfp8o9xxpx13/171#
如果您想添加动态布局或编辑现有布局,然后您可以通过方法获取它
,并通过在 java 中创建视图并使用 addView() 方法将您选择的视图添加到主布局中。
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
and add view of your choice to your main layout by creating them in java and using addView() method..
最简单的方法是动态地膨胀视图,在对话框创建方法中放入以下代码来构建对话框:
The easiest way is to inflate views dinamically, In your dialog creation method put this code to build Dialog: