3

TextView、SeekBar、およびその他のウィジェットで参照を取得する際に奇妙な問題があります。私の AlertDialog は次のようになります。

public class LineDialog extends AlertDialog {
private static SeekBar seekBar1, seekBar2, seekBar3;
private static TextView textView1, textview2, textView3;

protected LineDialog(final Context context, final DrawView drawView) {
    super(context);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dialogLayout = inflater.inflate(R.layout.line_dialog, null);

    setView(dialogLayout);
    setTitle("Line properties");
    textView1 = (TextView) findViewById(R.id.textView1); // TODO Code crash here :(
    setButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            seekBar1 = (SeekBar) findViewById(R.id.seek1);
            // some other code...
        }
    });
}

Lineで参照を取得したいときは

textView1 = (TextView) findViewById(R.id.textView1);

Logcat からエラーが送信されます

requestFeature() must be called before adding content

しかし、LineDiealog(AlertDialog) の onClick() メソッドで参照を取得すると、すべて正常に動作します。LineDialog.show() が呼び出される前にこの参照が必要なため、残念ながらこれは遅すぎます...

4

3 に答える 3

3

元のコードにはほとんどありました。によって返されたビューを保存したLayoutInflaterので、これを呼び出すときに使用するfindViewById()必要があるため、 である必要がありますdialogLayout.findViewById(...)。私自身も同じ問題を抱えていましたが、今では魅力的に機能しています。

于 2014-07-01T00:49:55.233 に答える
1

LinearLayout または xml ファイルの android:id で textView を参照する必要があります。

LinearLayout の例:

LinearLayout mainLinear = new LinearLayout(this);
mainLinear.addView(textBox);

ボックスにテキストを追加します。

textBox.addText("This is the text to add to the text box");

xml 参照 (最初に xml でテキスト ボックスを作成する必要があります!!!!):

TextView text1=(TextView) findViewById(R.id.NAME);
text1.setText("Hello please pick an option below");

NAME 部分は、.xml ファイルの android:id 名に置き換える必要があります。

于 2012-09-26T16:27:36.980 に答える
1

カスタムAlertDialogboxの例とカスタム AlertDialog ビューの実装方法を 次に示します。ビューを alertdialogbox に追加する方法を確認できます。

于 2012-05-08T09:17:21.040 に答える