2011-01-21 13 views
1

AlertDialogにいくつかのパラメータを渡そうとしていましたが、このダイアログはこれらの2つのパラメータ(「foo」と「bar」パラメータを想定)を表示しています。このダイアログはshowDialog(int id)を呼び出しています。アクティビティクラスにはオブジェクトにパラメータを渡す別のメソッドがあります:showDialog(int id, Bundle args)ですが、このメソッドはAPI 8以上でのみ使用でき、API 7で作業する必要があります。AndroidのAlertDialogにパラメータを渡す

ここで私はいくつかのチャンクを私がやっていることをもっと楽にする。私の活動で

私はこのようなAlertDialogを作成します。

protected Dialog onCreateDialog(int id) { 
     final LayoutInflater factory = LayoutInflater.from(this); 

     switch(id) { 
     case DIALOG_ID: 
      final View view = factory.inflate(R.layout.dialog_layout, null); 
      final TextView fooValue = (TextView)view.findViewById(R.id.foo_label); 
      final TextView barValue = (TextView)view.findViewById(R.id.foo_label); 
      //fooLabel.setText("HERE MUST BE FOO PARAMETER VALUE"); 
      //barLabel.setText("HERE MUST BE BAR PARAMETER VALUE"); 

      return new AlertDialog.Builder(this). 
       setIcon(R.drawable.icon). 
       setTitle(R.string.app_name). 
       setView(view). 
       setPositiveButton(R.string.close, null). 
       create(); 
... 

、他の部分で、私は、このダイアログを呼び出す:

// THESE PARAMETERS MUST BE PASSED TO DIALOG 
    int foo = result.getInt("foo"); 
    String bar = result.getString("bar"); 

    showDialog(DIALOG_ID); 
... 

はどうもありがとうございました!

答えて

2

上記のonCreateDialog関数を実装しているクラスにメソッドsetFooBar(int foo, String bar)を追加して、showDialogが呼び出される前にfooとbarの値を受け取ることをお勧めします。

アクティビティのインスタンスがない場合は、メソッドと変数を静的にすることを検討してください。

-1

LayoutInflater factory = LayoutInflater.from(this); 
      final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);// this layout can created by yourself whatever you want. 
      return new AlertDialog.Builder(AlertDialogSamples.this) 
       .setIcon(R.drawable.alert_dialog_icon) 
       .setTitle(R.string.alert_dialog_text_entry) 
       .setView(textEntryView) 
       .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 

         /* User clicked OK so do some stuff */ 
        } 
       }) 
       .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 

         /* User clicked cancel so do some stuff */ 
        } 
       }) 
       .create(); 

layoutnameこの例を試してみてください。main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" android:textColor="#663355" 
     android:layout_height="wrap_content" android:hint="@string/hello" /> 
</LinearLayout> 
関連する問題