0

如何以编程方式使我的 AlertDialog 可滚动?

我的 AlertDialog 是这样定义的:

private void DisplayAlertChoice(String title, String msg, final int pos)
{       
    alert = new AlertDialog.Builder(this);
    alert.setTitle(title);
    final LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
     CheckBox[] t=new CheckBox[15];
     for (i=0;i<currentBattery.temperatureNumber;i++)
     {               
         t[i]=new CheckBox(this);
         t[i].setText("title");
         t[i].setChecked(false);
         t[i].setOnCheckedChangeListener(this);

         layout.addView(t[i]);      
     }
    alert.setView(layout);      
    alert.show();   

}

我尝试了一些在网上找到的解决方案,但对我不起作用。

有人可以给我一个相关的解决方案,让它以编程方式滚动吗?

4

1 回答 1

2

我试过了,但它迫使我的应用程序停止。

ScrollView 可以只有一个孩子。这是一个Android约束。所以,做

    final LinearLayout layout = new LinearLayout(this);
    ScrollView scrollView = new ScrollView(this);
    layout.setOrientation(LinearLayout.VERTICAL);
     CheckBox[] t=new CheckBox[15];
     for (i=0;i<currentBattery.temperatureNumber;i++)
     {               
         t[i]=new CheckBox(this);
         t[i].setText("title");
         t[i].setChecked(false);
         t[i].setOnCheckedChangeListener(this);

         scrollView.addView(t[i]);      
     }

将使您的应用程序崩溃,因为您向 ScrollView 添加了多个子项。但如果你这样做

final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
 CheckBox[] t=new CheckBox[15];
 for (i=0;i<currentBattery.temperatureNumber;i++)
 {               
     t[i]=new CheckBox(this);
     t[i].setText("title");
     t[i].setChecked(false);
     t[i].setOnCheckedChangeListener(this);

     layout.addView(t[i]);      
 }
 final ScrollView scrollView = new ScrollView(this); 
 scrollView.addView(layout);

它应该工作顺利。

于 2014-05-28T14:31:45.867 回答