2011-10-01 33 views
0

我的活動中按下了一個按鈕時,我調用了下面的代碼。在Android中創建AlertDialog時需要提示

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this).create(); 
      alertDialog.setTitle("Alert 1"); 
      alertDialog.setMessage("This is an alert"); 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       return; 
      } }); 

我已經加入進口這兩種:

import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 

但仍得到以下錯誤:

Description Resource Path Location Type 
The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 77 Java Problem 
The method setButton(String, new DialogInterface.OnClickListener(){}) is undefined for the type AlertDialog.Builder MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 80 Java Problem 
Type mismatch: cannot convert from AlertDialog to AlertDialog.Builder MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 77 Java Problem 

任何人能給出任何解決方案,我好嗎?我對Java非常陌生。

回答

3

創建AlertDilog這樣:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); 
    alertDialog.setTitle("Alert 1"); 
    alertDialog.setMessage("This is an alert"); 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     return; 
    } }); 
    AlertDialog alert = alertDialog.create(); 
    alert.show();