2011-06-26 122 views
0

我在這裏做錯了什麼,或者我需要添加什麼?使用活動顯示對話框?

package dialog.com; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 


public class Dialog extends Activity { 
    CharSequence [] items = { "google", "apple", "microsoft" }; 
    boolean [] itemschecked = new boolean [items.length]; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button btn = (Button) findViewById(R.id.btn_dialog); 
     btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       showDialog(0); 
      } 
     }); 
    } 
    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch(id) { 
     case 0: 
      return new AlertDialog.Builder(this) 
      .setIcon(R.drawable.icon) 
      .setTitle("This is a Dialog with some simple text...") 
      .setPositiveButton("ok", new 
        DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, 
         int whichbutton) 
       { 
        Toast.makeText(getBaseContext(), 
          "OK Clicked!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
      .setNegativeButton("cancel",new 
        DialogInterface.OnclickListener() { 
       public void onClick(DialogInterface dialog, 
         int whichButton) 
       {Toast.makeText(getBaseContext(), 
         "cancel clicked!", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      .setMultiChoiceItems(itemschecked, new 
        DialogInterface.OnMultiChoiceClickListener() { 
       @Override 
       public void onClick(dialoginterface dialog, int which, boolean isChecked) { 
        Toast.makeText(getBaseContext(), 
          items[which] + (isChecked ? " checked!": 
           "unchecked!"), 
           Toast.LENGTH_SHORT).show(); 
       } 
      } 
      ) 
      .create(); 
     } 
     return null: 

    }}} 
+1

問題是什麼? – Haphazard

+0

導入android.app.Dialog與在同一個文件中定義的類型衝突//返回類型與Activity.onCreateDialog(int)不兼容//類型不匹配:無法從AlertDialog.Builder轉換爲對話框//令牌上的語法錯誤「。」,刪除此令牌// DialogInterface.OnclickListener無法解析爲類型// – ricardo123

回答

1

將您的班級重命名爲Dialog以外的班級。叫它CustomDialog什麼的。

+0

謝謝,我會試一試 – ricardo123

1

這是「開始Android應用程序開發」一書中的例子。

它沒有工作,所以我加入這個:

  1. import your.package.name.R;
  2. 並刪除最後@override,一個爲onClick()方法。 (只是刪除註釋,不是方法。)
  3. 我刪除了該行:.setIcon(R.drawable.icon),無法解決該問題。

此代碼工作後。