【问题标题】:Variable number of textedits in alertdialog in android?android中alertdialog中可变数量的文本编辑?
【发布时间】:2011-12-03 23:28:04
【问题描述】:

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

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

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

alert.setView(v);

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

【问题讨论】:

  • 嗨..我不太明白你的问题,你能详细说明一下吗?你到底想做什么?不过这个问题看起来很熟悉..stackoverflow.com/questions/4576219/…
  • 嗯,将 LinearLayout 添加到 AlertDialog.Builder#setView(View) 效果很好 - 并且以编程方式从该布局中添加/减去子视图也可以正常工作。

标签: android android-alertdialog textedit


【解决方案1】:

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

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();

【讨论】:

    【解决方案2】:

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

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

    如果你想添加动态布局或编辑现有布局,那么你可以通过方法得到它

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

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

    myMainLayout.addView(yourChildView);
    

    【讨论】:

      【解决方案3】:

      添加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()

      alterDlgLayout();
      

      #onPause()

      mDlg.dismiss();
      

      【讨论】:

        【解决方案4】:

        当用户查看对话框时,EditTexts 的数量是否会发生变化,或者到那时该数量是否已修复?如果它是固定的并且只是不时变化,您可以为它们中的每一个构建自己的布局 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();
        

        【讨论】:

        • edittexts 的数量在用户查看时已修复。您的建议是我已经做到了,但问题是关于其他方法,因为这种方法不是最佳的 - 创建手册布局。
        • 没错,但我认为这比以编程方式将视图添加到警报对话框的基本版本要好
        【解决方案5】:

        为什么需要可变数量的TextViews?您可以使用一个来显示多行。如果您需要更复杂的东西,您可以创建自己的主题为@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>
        

        【讨论】:

        • 不,我不仅需要可变数量的文本视图,我还需要多对——(TextView + TextEdit),其中文本视图中的文本概述了用户应键入的内容。问题是每次我需要构建不同数量的文本编辑。
        • @Sergey:我建议使用EditText 提示属性来概述用户应该键入的内容。让自己成为一个类似于对话框的活动,它接受一个字符串列表以及要显示的编辑文本和提示,并让它们由活动 setResult 返回。
        • 不管怎样,即使我只使用 EditText 元素而没有 textviews,我也面临着它们的变量数的问题
        • @Sergey:确实,这就是我推荐类似对话的子活动的原因。 AlertDialog 仅用于显示警报和简单的东西。如果你想以对话框的形式做一些复杂的事情,只需使用一个活动,让它看起来像一个对话框。
        猜你喜欢
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-20
        • 1970-01-01
        • 1970-01-01
        • 2023-01-05
        相关资源
        最近更新 更多