效果:
main.xml
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><Buttonandroid:id="@+id/alert"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="弹出对话框"android:layout_x="122px"android:layout_y="141px"/></AbsoluteLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, AlertTest!</string> <string name="title">消息提示</string> <string name="text">我很好!</string> <string name="buttonValue">确定</string> <string name="app_name">AlertTest</string></resources>
package alert.test;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Button;public class AlertTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** 载入main.xml */ setContentView(R.layout.main); /** 找到button组件 */ Button bt=(Button)findViewById(R.id.alert); /** 设置Button按钮的监听事件 */ bt.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ /** 当前Activity对象传进 AlertDialog,指当前窗口弹出对话框*/ Builder builder=new AlertDialog.Builder(AlertTest.this); /** 设置对话框的标题*/ builder.setTitle(R.string.title); /** 设置对话框的内容*/ builder.setMessage(R.string.text); /** 设置对话框的里按钮的Value值和设置按钮的点击事件并且弹出对话框*/ builder.setPositiveButton(R.string.buttonValue, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { /** 离开程序*/ finish(); } }).show(); } }); }}