2010-02-01 294 views
5

我想調試一些東西,想彈出消息對話框。 Eclipse告訴我它想讓我「創建方法showAlert(string,string,string,boolean)」如何在Android中使用showAlert方法?

我已經導入了這個 import android.content.DialogInterface;

我錯過了哪一步?

回答

0

看起來你可能有一個參數類型不匹配。檢查你的參數實際上是字符串還是布爾值。也許你需要對你的對象打電話toString()

+0

那麼,我已經走了另一條路......這些是從其他博客幫助頁給出的參數。所以現在我使用的是來自developer.android.com的AlertDialog builder ...已經過去了這個錯誤: [2010-02-01 13:41:12 - MobileServiceCallContacts] ActivityManager:java.lang.SecurityException:Permission Denial :從空(pid = -1,uid = -1)啓動Intent {flg = 0x10000000 cmp = com.msi.ibm.tutorial/.MobileServiceCallContacts}需要android.permission.READ_CONTACTS 並且是的,我將該權限設置爲我的清單...其他地方是否願意去? – jkmcgee 2010-02-01 19:48:18

+0

您應該爲此打開另一個問題......但據我所知,只有在AndroidManifest.xml中需要放置權限 – Justin 2010-02-01 20:47:54

6

如果您嘗試創建並顯示AlertDialog,則應該使用AlertDialog.Builder作爲例子。 DialogInterface,顧名思義,一個接口,只有2種方法:cancel()和dismiss()。

創建AlertDialog相當容易:

new AlertDialog.Builder(this) 
.setTitle("Some Title") 
.setMessage("some message") 
.setPositiveButton("OK", new OnClickListener() { 
    public void onClick(DialogInterface arg0, int arg1) { 
     // Some stuff to do when ok got clicked 
    } 
}) 
.setNegativeButton("cancel", new OnClickListener() { 
    public void onClick(DialogInterface arg0, int arg1) { 
     // Some stuff to do when cancel got clicked 
    } 
}) 
.show(); 

這顯示了一個簡單AlertDialog。

提示:檢查Activity.showDialog(int)和Activity.onCreateDialog()它們使您在使用對話框時更加輕鬆。

5

如果只顯示一個調試消息,你可以嘗試Toast.makeText()

Toast.makeText(context, "Hi there!", Toast.LENGTH_SHORT).show(); 

不要忘記調用show()

+0

有沒有方法可以將確認按鈕添加到Toast方法中?這就是爲什麼我想使用警報對話方法。 – jkmcgee 2010-04-12 18:47:25

+0

原則上,您可以將Toast佈局設置爲任何您想要的內容,但Toast的使用僅用於顯示自己消失的小消息(例如,如果您更改了手機上的音量 - 這是一個Toast,它會向您顯示目前的音量)。 – Ridcully 2011-02-15 14:44:48

+0

Toast.LENGHT_SHORT應該是Toast.LENGTH_SHORT – AaA 2012-08-29 02:44:36

相關問題