你的位置:首页 > 操作系统

[操作系统]对话框AlertDialog的基本创建


 

测试代码:

 

<RelativeLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context="com.zzw.testalerdialog.MainActivity" >  <Button    android:id="@+id/but"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:text="点击" /></RelativeLayout>

 

package com.zzw.testalerdialog;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.Toast;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);        final  Activity context=this;     findViewById(R.id.but).setOnClickListener(new OnClickListener() {            //DialogInterface.BUTTON_POSITIVE只要作用是显示顺序      @Override      public void onClick(View v) {        AlertDialog dialog = new AlertDialog.Builder(context).create();        dialog.setIcon(R.drawable.ic_launcher);        dialog.setTitle("标题");        dialog.setMessage("提示信息");        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            Toast.makeText(context, "确定", 0).show();          }        });                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            Toast.makeText(context, "取消", 0).show();          }        });                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "考虑", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            Toast.makeText(context, "考虑", 0).show();          }        });                dialog.show();      }    });  }}