2012-03-18 6 views
3

私は非常に簡単なダイアログのように定義されている:ダイアログを表示するには?

import android.app.AlertDialog; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 

public class MyDialog{ 

    private String promptReply = null; // local variable to return the prompt reply value 

    public String showAlert(String ignored, Context ctx) 
    { 
    LayoutInflater li = LayoutInflater.from(ctx); 
    View view = li.inflate(R.layout.promptdialog, null); 

    AlertDialog.Builder builder = new AlertDialog.Builder(ctx); 
    builder.setTitle("Dialog Title"); 
    builder.setView(view); 

    builder.setPositiveButton("OK", (myActivity)ctx); 
    builder.setNegativeButton("Cancel", (myActivity)ctx); 

    AlertDialog ad = builder.create(); 

    ad.show(); 
    return "dummystring";  
    } 
} 

そして、私は活動の主なレイアウトのsetContentView()を呼び出し後onCreate()でそれを表示しようとすると、ダイアログは単に表示されません。

MyDialog dialog = new MyDialog(); 
dialog.showAlert("Why isn't this shown???", this); 

一方、と同じ正確なコールをの前に配置すると、アクティビティのメインレイアウトにはsetContentView()が呼び出されます。ダイアログには正常に表示されます。

私の質問はなぜですか?

この場合、注文が重要なのはなぜですか?

私には何が欠けていますか?ビューを膨らませるためにあなたのコードで

答えて

2

は、このようなものを使用します。layout_rootカスタムダイアログのトップレベルのレイアウトのIDです

View layout = inflater.inflate(R.layout.promptdialog, 
          (ViewGroup) findViewById(R.id.layout_root)); 

関連する問題