How do I display an alert dialog on Android?

后端 未结 30 3270
清歌不尽
清歌不尽 2020-11-22 03:02

I want to display a dialog/popup window with a message to the user that shows \"Are you sure you want to delete this entry?\" with one button that says \'Delete\'. When

30条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 03:23

    I have created a dialog for asking a Person whether he wants to call a Person or not.

    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("Avbryt", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    dialog.dismiss();
                }
            });
    
            builder.show();
        }
    }
    

提交回复
热议问题