2013-07-09 24 views
0

我有了這個代碼來創建使用一個TextView可點擊的鏈接一個警告對話框:不能調用類alertdialog

public static class MyOtherAlertDialog { 

     public static AlertDialog create(Context context) { 
      final TextView message = new TextView(context); 
      // i.e.: R.string.dialog_message => 
        // "Test this dialog following the link to dtmilano.blogspot.com" 
      final SpannableString s = 
         new SpannableString(context.getText(R.string.dialog_about)); 
      Linkify.addLinks(s, Linkify.WEB_URLS); 
      message.setText(s); 
      message.setMovementMethod(LinkMovementMethod.getInstance()); 

      return new AlertDialog.Builder(context) 
      .setTitle(R.string.about) 
      .setCancelable(true) 
      .setIcon(android.R.drawable.ic_dialog_info) 
      .setPositiveButton("ok", null) 
      .setView(message) 
      .create(); 
     } 
     } 

但我不知道究竟怎麼稱呼它,

我試過了:

    MyOtherAlertDialog variable = new MyOtherAlertDialog(); 
        variable.create(this); 

但是沒有運氣,該怎麼稱呼這個班?

回答

0

什麼你要找的是

AlertDialog dialog = MyOtherAlertDialog.create(this); 
dialog.show(); 

這是相當基本的Java。如果你需要問這個問題,也許你應該從學習一些基礎知識開始。

+0

謝謝你,工作。我試圖學習java,但我更喜歡通過玩它來學習它。 (我會盡快接受你的回答。) – Mdlc

0

要顯示的對話框中嘗試使用此

MyOtherAlertDialog.create(this).show(); 

其實Dialog從你的代碼中創建,但因爲你是不是要求show()方法時不顯示。

而且由於create()方法是static所以您應該以static的方式訪問它。

+0

謝謝你的解釋 – Mdlc

0

通過回答StinePike和Alejs G是正確的。只是想補充一點,您已經聲明create方法static。因此,您需要使用類名來調用該方法。
這就是爲什麼它應該是

MyOtherAlertDialog.create(this);

相同的概念適用於變量。