AlertDialog - 尝试让警报正常工作
因此,我在这里搜索了一些有关 AlertDialog 的问题,并且我不完全确定我在做什么,所以我很难将这些问题与我自己的示例联系起来。 (我对整个 Android 编程还是个新手,所以请耐心等待。)
我已经在公共类 _ Activity 实现 OnCLickListener 下定义了它...
public AlertDialog myAlertDialog;
然后我在 onClick 下定义了
public void onClick(View src) {
switch(src.getId()){
case R.id.buttonOk:
if (score==0){
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Title");
myAlertDialog.setMessage("Message");
myAlertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
myAlertDialog.show();
}
这一行它下面的行有错误:
myAlertDialog.setButton("OK", new DialogInterface.OnClickListener() {
错误:
第一个:此行有多个标记 - DialogInterface 无法解析为类型 - 方法 setButton(String, new OnClickListener(){}) 未定义类型
2:DialogInterface 无法解析为类型
谁能告诉我我做错了什么?
谢谢!
So I have searched through a few question here regarding the AlertDialog, and I'm not completely sure what I'm doing, so I am having a hard time relating those questions to my own example. (I'm still new to this whole android programming so please bear with me.)
I have defined this under the public class _ Activity implements OnCLickListener ...
public AlertDialog myAlertDialog;
And then I have this under the onClick
public void onClick(View src) {
switch(src.getId()){
case R.id.buttonOk:
if (score==0){
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Title");
myAlertDialog.setMessage("Message");
myAlertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
myAlertDialog.show();
}
This line and the line below it have errors:
myAlertDialog.setButton("OK", new DialogInterface.OnClickListener() {
Errors:
1st: Multiple markers at this line
- DialogInterface cannot be resolved to a type
- The method setButton(String, new OnClickListener(){}) is undefined for the type
2nd: DialogInterface cannot be resolved to a type
Can anyone tell me what I am doing wrong please?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定您只是没有导入 DialogInterface。尝试将此语句添加到代码的开头。
I'm pretty sure you just aren't importing the DialogInterface. Try adding this statement to the beginning of your code.
我认为您需要
setPositiveButton()
或setNegativeButton()
在AlertDialog.Builder
上没有方法setButton()
。并确保导入
DialogInterface
。I think you need
setPositiveButton()
orsetNegativeButton()
there is no methodsetButton()
onAlertDialog.Builder
.And make sure to import
DialogInterface
.