如何在单击按钮时获得简单的警报?
我正在玩一个简单的应用程序,我想在单击按钮时显示警报。
到目前为止我的代码是:
package max.helloworld.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
private Button closeButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setContentView(R.layout.main);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setKeyListener(new onClickListener() {
@Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Transformers");
alertDialog.setMessage("Optimus Prime");
alertDialog.setButton("OK", new OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
});
//alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
finish();
}
});
}
}
问题在于它说 AlertDialogalertDialog = new AlertDialog.Builder(this).create();
它说构造函数未定义。请问我该如何解决这个问题?
I was playing around with a simple app and I want to show an alert when a button is clicked.
The code I have so far is:
package max.helloworld.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
private Button closeButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setContentView(R.layout.main);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setKeyListener(new onClickListener() {
@Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Transformers");
alertDialog.setMessage("Optimus Prime");
alertDialog.setButton("OK", new OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
});
//alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
finish();
}
});
}
}
The issue is where it says AlertDialog alertDialog = new AlertDialog.Builder(this).create();
It says the constructor is undefined. How do I fix that please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此行更改
为:
Change this line:
To:
关键字
this
解析为该范围内的OnClickListener
实例。要引用回您的活动实例,请将this
替换为HelloWorldActivity.this
The keyword
this
resolves to the instance ofOnClickListener
at that scope. To refer back to your activity instance replacethis
withHelloWorldActivity.this