2013-04-21 44 views
0

我正在做警報對話框演示。它工作正常,但我發現在MainActivity.java文件中的「setButton」的一行。聽到的是我的代碼:一條線淹沒在MainActivity.java中的alertDialog.setButton

@SuppressWarnings("deprecation") 
public void onClick(View arg0) { 
      // Creating alert Dialog with one Button 

      AlertDialog alertDialog = new AlertDialog.Builder(
        MainActivity.this).create(); 

      // Setting Dialog Title 
      alertDialog.setTitle("Alert Dialog"); 

      // Setting Dialog Message 
      alertDialog.setMessage("Welcome to AndroidHive.info"); 

      // Setting Icon to Dialog 

      // I got line hear........ over setButtob ......... 
      alertDialog.setButton("OK", 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, 
           int which) { 
          // Write your code here to execute after dialog 
          // closed 
          Toast.makeText(getApplicationContext(), 
            "You clicked on OK", Toast.LENGTH_SHORT) 
            .show(); 
         } 
        }); 

      // Showing Alert Message 
      alertDialog.show();![enter image description here][1] 

     } 
    });` 

您駕駛室看到圖像here和得到什麼,我的意思是說更多的想法。

回答

1

我經歷了url圖片,這讓您的問題變得清晰。這是因爲該方法(setButton)已折舊。您正在使用

@SuppressWarnings("deprecation") 

頂部。這可以防止發出警告消息,並且無法讀取警告消息(可能是這種情況:此方法已折舊)。如果你刪除了第一行,那麼它會告訴你爲什麼它顯示了setButton的交叉線。見here。它說這種方法在API級別3中折舊。

要了解如何正確使用它,可以通過使用Alert Dialog的官方文檔頁面。你也可以看到教程here

2

你的意思是你有一個警告說該方法已被棄用?

這是因爲SET按鈕不再使用,它​​的棄用,而不是你應該做這樣的事情:

alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog,int id) { 
    // blablabla 
} 
}); 

還要注意,你可以得到的字符串「OK」之類的東西,以及:android.R。 string.ok,這是推薦的方式;)