2011-01-25 7 views
0

これは私の問題です。私は、タイトルとポジティブなボタンを持つAlertDialogを持っていたいと思います。 AlertDialogの内容をXMLファイルに記述したい(タイトル/ボタンを除く)。 レイアウトリソースにdlg_addpwd.xmlというファイルを作成しました。ここで は、私が使用しているコードです:AlertDialogのビューとしてXMLファイルに記述されたビューを定義する方法

AlertDialog alert = new AlertDialog.Builder(this); 
    alert.setTitle("Password access"); 
    alert.setView(findViewById(R.layout.dlg_addpwd)); 
    alert.setPositiveButton("Add", listenAddPwdDlg); 
    alert.show(); 

私は

alert.setView(findViewById(R.layout.dlg_addpwd)); 

ラインが間違っていると思い、そうではありませんか? 私の質問の主な考え方は、AlertDialogのビューとしてXMLファイルで記述されたビューを定義する方法ですか?

ヴィンセント

答えて

7
LayoutInflater inflater = (LayoutInflater) 
     getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.paypaldialog, 
     (ViewGroup) findViewById(R.id.yourDialog)); 
    AlertDialog.Builder builder = new AlertDialog.Builder(YourClass.this) 
     .setView(layout); 
    alertDialog = builder.create(); 
    alertDialog.show(); 

これは私のやり方です。

1

おかげでカスタムアラートダイアログを作成するためにCustomizing the Alert dialog in Androidに私の答えを参照してください。

コードの主な問題は、AlertDialog.Builderを使用しないことです。代わりに、新しいDialogを作成し、setContentView()を使用してXMLをレンダリングします。

関連する問題