2

alertdialogで可変数のテキスト編集を設定することは可能ですか?StackViewやLinearLayoutなどのいくつかのコンテナビューを動的に埋めようとしましたが、メソッドaddViewはAdapterView(例外)ではサポートされていないと言われています。解決策は何ですか?
追加:
動的情報からalertdialogを作成したい。

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

これで、ビューを次のように設定できます。

alert.setView(v);

ただし、v要素はTextViewまたはEditTextのような単純なものにすることしかできません。2つのテキストビューと3つのエディットテキストなど、可変数の要素を含む可能性のあるコンテナビューを作成したい場合はどうなりますか?これどうやってするの?今、私は別のレイアウトファイルを作成し、それでビューを膨らませるだけですが、それは解決策ではありません。私に何ができる?

4

5 に答える 5

0

以下のリンクは、ダイアログボックスの静的レイアウト用です。

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);
于 2011-10-19T05:45:18.683 に答える
0

TextView可変数のsが必要なのはなぜですか? 1 つの行を使用して複数の行を表示できます。もっと複雑なものが必要な場合は、テーマを使用して独自のダイアログのようなアクティビティを作成し@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>
于 2011-10-12T06:47:48.360 に答える
0

ユーザーがダイアログを表示している間に EditText の数が変化していますか、それとも数はそれまでに修正されていますか? それが固定されていて時々しか変わらない場合は、それぞれに独自のレイアウト xml を作成し、ダイアログに表示するレイアウト xml を switch ステートメントで決定できます。

カスタム アラート ダイアログは、次のコードで表示できます。

// 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();
于 2011-10-12T07:44:12.627 に答える
0

a を追加すると、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();
于 2011-10-12T08:45:48.957 に答える
0

最も簡単な方法は、ビューを動的に膨張させることです。ダイアログ作成メソッドで、次のコードを配置してダイアログを構築します。

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();
于 2011-10-19T06:21:00.177 に答える