• Back弹出AlertDialog


    package com.pingyijinren.helloworld.activity;
    
    import android.content.DialogInterface;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.widget.Toast;
    
    import com.pingyijinren.helloworld.R;
    public class MainActivity extends AppCompatActivity {
        private AlertDialog alertDialog;
        private CharSequence[] data=new CharSequence[]{"香蕉","苹果","桃子"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode==KeyEvent.KEYCODE_BACK){
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setTitle("程序退出?");
    //            builder.setMessage("确定退出吗?");
                builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                });
                builder.setSingleChoiceItems(data, 0, new DialogInterface.OnClickListener() {  //其实就是setMessage
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,data[which],Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog=builder.create();
                alertDialog.show();
            }
            return super.onKeyDown(keyCode, event);
        }
    }
  • 相关阅读:
    正则表达说明—Pattern API
    C#调用dll(Java方法)
    获取页面URL两种方式
    可拖动布局之jQueryUI
    可拖动布局之Gridster
    Thymeleaf入门基础
    Eclipse安装反编译插件
    Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案
    Spring Boot : Whitelabel Error Page解决方案
    MYSQL卸载
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5552986.html
Copyright © 2020-2023  润新知