本文实例分析了Android中AlertDialog用法,分享给大家供大家参考,具体如下:
Android中AlertDialog为一些程序提供了对话框,有些功能能够进一步满足程序的需要。下面举例介绍。
程序如下:
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(); //结束该程序的Activity } }) //返回该程序的Activity .setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).show(); } }); } }
希望本文所述对大家Android程序设计有所帮助。