Example analysis of AlertDialog usage in Android

  • 2021-01-25 07:54:32
  • OfStack

This paper analyzes the usage of AlertDialog in Android and shares it with you for your reference. The details are as follows:

Android provides a dialog box for some programs, some features can be further to meet the needs of the program. Here's an example.

The procedure is as follows:


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class A01Activity extends Activity {
 Button b;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b=(Button)findViewById(R.id.button);
    b.setOnClickListener(new OnClickListener(){
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  new AlertDialog.Builder(A01Activity.this)
  .setTitle(R.string.str_title)
  .setIcon(R.drawable.fei)
  .setMessage(R.string.str_message)
  .setPositiveButton("OK",new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   finish();  // The end of the program Activity
   }
  })
  // Returns the program's Activity
  .setNegativeButton("NO", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   }
  }).show();
  }
    });
  }
}

More about Android related content interested readers can see the site features: Android control usage summary and Android development introduction and advanced tutorial

I hope this article is helpful to Android program design.


Related articles: