昨天,我通过助手编程,用我的安卓手机编写了一个小AlertDialog。在互联网上我找到了源代码
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();我已经在AIDE中测试过它,它正在工作,然后我在AndroidStudio中测试了它,但是它没有工作。为什么它在AIDE而不是Android演播室工作呢?
发布于 2015-08-02 09:54:36
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this);按照上面的方式编辑第一行,因为您需要在创建alertDialog之前设置生成器
完整代码如下:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialogMain = alertDialog.create();
// Showing Alert Message
alertDialogMain.show();https://stackoverflow.com/questions/31770560
复制相似问题