簡體   English   中英

在視圖中創建簡單的是/否對話框(Android)

[英]Creating a simple yes/no dialog box in a view (Android)

我的活動中有一個onCreateDialog方法,該方法具有大小寫切換,可以根據請求顯示要顯示的不同對話框。

我無法在視圖中使用showDialog()方法,因為無法從創建視圖時傳遞的上下文中訪問該方法。 至少,我找不到訪問它的方法。

如何在應用程序視圖中使用showDialog? 我需要創建一個偵聽器嗎? 如果是這樣,怎么辦? 有沒有更好的方法?

這是應用程序活動中存在的onCreateDialog代碼:

protected Dialog onCreateDialog(int id) {
    AlertDialog alert=null;
    switch(id) {
    case DIALOG_GAMEOVER_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("You died. Play again?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //init();
                       //oGameState = eGameState.PLAYING; 
                      dialog.cancel();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       finish();
                   }
               });
            alert = builder.create();
        break;
    default:
        break;
    }

    return alert;
}

我嘗試傳遞對活動的引用,但發生崩潰。 也許我做錯了嗎?

在我的活動中:

    // set our MainView as the View
    oNebulaMainView = new NebulaMainView(this, this);
    setContentView(oNebulaMainView);

在我看來:

public NebulaMainView(Context context, Activity oActivity) {
    super(context);
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);

    // create the game loop thread
    thread = new NebulaMainThread(getHolder(), this);

    setFocusable(true);

    oActivity.showDialog(DIALOG_GAMEOVER_ID);
}

我可能在這里丟失了一些東西,但是是什么原因阻止了您致電:

alert.show()

只需使警報可供視圖訪問,然后在視圖內部調用該表單即可。

將Alert聲明為實例變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM