是的,我知道有AlertDialog。但我很惊讶地知道在Android中显示对话框有多么困难(好吧,至少不是程序员友好的)。
我曾经是一名。net开发人员,我想知道在android上是否有类似的功能?
if (MessageBox.Show("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){
// Do something...
}
是的,我知道有AlertDialog。但我很惊讶地知道在Android中显示对话框有多么困难(好吧,至少不是程序员友好的)。
我曾经是一名。net开发人员,我想知道在android上是否有类似的功能?
if (MessageBox.Show("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){
// Do something...
}
当前回答
这对我来说很管用:
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing, but close the dialog
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
其他回答
问一个人他是否想打电话对话。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class Firstclass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig);
imageViewCall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
try{
showDialog("0728570527");
} catch (Exception e){
e.printStackTrace();
}
}
});
}
public void showDialog(final String phone) throws Exception {
AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this);
builder.setMessage("Ring: " + phone);
builder.setPositiveButton("Ring", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phone));
startActivity(callIntent);
dialog.dismiss();
}
});
builder.setNegativeButton("Abort", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
});
builder.show();
}
}
谢谢。我使用API级别2 (Android 1.1),而不是BUTTON_POSITIVE和BUTTON_NEGATIVE,我必须使用BUTTON1和BUTTON2。
你可以在Kotlin中轻松完成:
alert("Testing alerts") {
title = "Alert"
yesButton { toast("Yess!!!") }
noButton { }
}.show()
显示对话框匿名作为命令链&没有定义另一个对象:
new AlertDialog.Builder(this).setTitle("Confirm Delete?")
.setMessage("Are you sure?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Perform Action & Dismiss dialog
dialog.dismiss();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
})
.create()
.show();
史蒂夫的回答是正确的,尽管有些片段过时了。下面是一个FragmentDialog的例子。
类:
public class SomeDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Sure you wanna do this!")
.setNegativeButton(android.R.string.no, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing (will close dialog)
}
})
.setPositiveButton(android.R.string.yes, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
})
.create();
}
}
开始对话:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Create and show the dialog.
SomeDialog newFragment = new SomeDialog ();
newFragment.show(ft, "dialog");
您还可以让类实现onClickListener,并使用它来代替嵌入式侦听器。