I have a button and I would like to open a dialog when pressed. This is my code:
Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Intent myIntent = new Intent(view.getContext(), agones.class);
//startActivityForResult(myIntent, 0);
Aler开发者_JAVA技巧tDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("hi");
alertDialog.setMessage("this is my app");
alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
}
});
As @Roflcoptr has said, you haven't called alertDialog.show()
method. thus your dialog doesn't appear.
Here's your edited code:
Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Intent myIntent = new Intent(view.getContext(), agones.class);
//startActivityForResult(myIntent, 0);
AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update
alertDialog.setTitle("hi");
alertDialog.setMessage("this is my app");
alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show(); //<-- See This!
}
});
if you write this
instead of <ActivityName>.this
, then it is going to take the reference of View.OnClickListener
since this
is currently being accessed inside it. You need to give your Activity's name there.
Your dialog isn't displayed, because you don't call AlertDialog#show.
Aman Alam's souliton is good, but the .setButton()
part gave me an error. So I implemented it in kotlin and fixed the error.
Kotlin
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Title")
.setMessage("Write your message here.")
.setPositiveButton("YES") { dialog, whichButton ->
// DO YOUR STAFF
}
.setNegativeButton("NO") { dialog, whichButton ->
// DO YOUR STAFF
// dialog.close()
}
dialog.show()
More about dialogs: https://developer.android.com/guide/topics/ui/dialogs
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("this is message");
builder.setTitle("this is title");
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application ?");T
//This will not allow to close dialogbox until user selects an option
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(this, "positive button", Toast.LENGTH_SHORT).show();
//builder.finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
Toast.makeText(this, "negative button", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
//alert.setTitle("AlertDialogExample");
alert.show();
精彩评论