我在这里做错了什么或者我需要添加什么?
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:
}}}
答案 0 :(得分:1)
将您的班级重命名为Dialog
以外的其他名称。称之为CustomDialog
或其他。
答案 1 :(得分:1)
这是“开始Android应用程序开发”一书中的示例。
它没有用,所以我补充说:
import your.package.name.R;
@override
,即onClick()
方法的.setIcon(R.drawable.icon)
。 (只需删除注释,而不是方法。)此代码有效。