Android-AlertDialog - 不起作用
Android-AlertDialog - doesn't work
昨天我通过编程的 AIDE 用我的 Android-Phone 编写了一个小的 AlertDialog。在 Internet 上我找到了源代码
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 studio 中无效?
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();
昨天我通过编程的 AIDE 用我的 Android-Phone 编写了一个小的 AlertDialog。在 Internet 上我找到了源代码
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 studio 中无效?
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();