菜鸟Android学习之路30——是否退出对话框

来源:互联网 发布:网络stp 编辑:程序博客网 时间:2024/06/06 03:14

xml文件代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageButton
        android:contentDescription="@string/background"
        android:id="@+id/imageButton1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/picw01"
        />
   
</LinearLayout>

maia_activity代码:

ImageButton button1 = (ImageButton)findViewById(R.id.imageButton1);
  button1.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
//   设置图标
   alert.setIcon(R.drawable.img06);
//   设置标题
   alert.setTitle("是否退出?");
//   设置内容
   alert.setMessage("真的不看这美女的身体了?");
//   添加取消按钮
   alert.setButton(DialogInterface.BUTTON_NEGATIVE, "no", new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
     
    }
   });
//   添加确定按钮
   alert.setButton(DialogInterface.BUTTON_POSITIVE, "YES",new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
     finish();
    }
   });
   alert.show();
   }
  });

效果如下:


0 0
原创粉丝点击