是的,我知道有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...
}
当前回答
Kotlin在Android::
override fun onBackPressed() {
confirmToCancel()
}
private fun confirmToCancel() {
AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you want to cancel?")
.setCancelable(false)
.setPositiveButton("Yes") {
dialog: DialogInterface, _: Int ->
dialog.dismiss()
// for sending data to previous activity use
// setResult(response code, data)
finish()
}
.setNegativeButton("No") {
dialog: DialogInterface, _: Int ->
dialog.dismiss()
}
.show()
}
其他回答
AlertDialog。Builder真的没有那么难用。一开始肯定有点吓人,但一旦你使用了一点,它就变得简单而强大了。我知道你说过你知道如何使用它,但这里只是一个简单的例子:
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
你也可以重用那个DialogInterface。OnClickListener如果你有其他是/否框,应该做同样的事情。
如果你是从视图中创建对话框。OnClickListener,你可以使用view.getContext()来获取Context。或者你也可以使用yourFragmentName.getActivity()。
Kotlin在Android::
override fun onBackPressed() {
confirmToCancel()
}
private fun confirmToCancel() {
AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you want to cancel?")
.setCancelable(false)
.setPositiveButton("Yes") {
dialog: DialogInterface, _: Int ->
dialog.dismiss()
// for sending data to previous activity use
// setResult(response code, data)
finish()
}
.setNegativeButton("No") {
dialog: DialogInterface, _: Int ->
dialog.dismiss()
}
.show()
}
谢谢尼基,你的回答帮助我改善了现有的简单添加我想要的行动如下
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Do this action");
builder.setMessage("do you want confirm this action?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do do my action here
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// I do not need any action here you might
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();
}
}
史蒂夫的回答是正确的,尽管有些片段过时了。下面是一个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,并使用它来代替嵌入式侦听器。