android中AlertDialog按钮的问题
我有一个警报对话框,应该将布尔值设置为 true。对于 setPositiveButton
我将 Dialog onclick
界面设置为 null。当我将 onClickListener
添加到 setNegativeButtons
onclick
界面时,它给我一个编译错误:方法 setNegativeButton(int, DialogInterface.
不适用于参数 AlertDialog.Builder
类型中的 OnClickListener)(String, new View.OnClickListener(){})
这是我的代码,为什么我会收到编译错误以及如何修复此错误?谢谢
new AlertDialog.Builder(ImTracking.this)
.setMessage(
Html.fromHtml(getText("http://www.cellphonesolutions.net/im-following-"
+ getResources().getString(
R.string.Country))))
.setPositiveButton("OK", null)
// The red squigly is under the .setNegativeButton
.setNegativeButton("Don't Remind", new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SharedPreferences prefs= getSharedPreferences("Settings",0);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("ImTrackingDontRemind",true);
editor.commit();
}
}).show();
I have an Alert Dialog that is supposed to set a boolean to true. For setPositiveButton
I have the Dialog onclick
interface as null. When I add an onClickListener
to the setNegativeButtons
onclick
interface it gives me a compile error saying: The method setNegativeButton(int, DialogInterface.OnClickListener)
in the type AlertDialog.Builder
is not applicable for the arguments (String, new View.OnClickListener(){})
Here is my code, why am I getting a compile error and how do I fix this? Thanks
new AlertDialog.Builder(ImTracking.this)
.setMessage(
Html.fromHtml(getText("http://www.cellphonesolutions.net/im-following-"
+ getResources().getString(
R.string.Country))))
.setPositiveButton("OK", null)
// The red squigly is under the .setNegativeButton
.setNegativeButton("Don't Remind", new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SharedPreferences prefs= getSharedPreferences("Settings",0);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("ImTrackingDontRemind",true);
editor.commit();
}
}).show();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以这是应该的
So this is was it should be
这是你的解决方案,你犯了一个愚蠢的错误,伙计。
不应该是
应该是
Here is your solution, you did a silly mistake there buddy.
It should not be
It should be
除了提供答案之外,也许谨慎的做法是解释为什么会出现这个答案。另外,肖恩的问题是,
强调我的。虽然接受的答案回答了后一个问题,但它并不试图回答前一个问题。
Sean,您正在创建的
onClickListner
匿名内部类实际上是View
的成员函数,因为您没有提供类名。您的编译错误源于以下事实:AlertDialog
扩展了Dialog
类,而不是View
类,因此具有onClickListner
> 具有不同函数签名的成员函数:public abstract void onClick (DialogInterfacedialog, int which)
与
View
的onClickListner
成员函数相比, :public Abstract void onClick (查看v)
。In addition to providing the answer, perhaps it would also be prudent to provide an explanation of why that's the answer. Plus, Sean's question was,
emphasis mine. While the accepted answer answers the latter question, it does not attempt to answer the former question.
Sean, the
onClickListner
anonymous inner class you're creating is actually a member function ofView
since you provided no class name. Your compile error stems from the fact thatAlertDialog
extends theDialog
class, not theView
class and thus has anonClickListner
member function with a different function signature:public abstract void onClick (DialogInterface dialog, int which)
than that of
View
'sonClickListner
member function:public abstract void onClick (View v)
.