zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    android基本架构
    c#编辑框只接受数字
    listbox数据源绑定问题
    QQ在线客服代码
    用VB生成DLL封装ASP代码例子
    C#,关于DataGridView的一些方法
    转:ASP.NET中引用dll“找不到指定模块"的完美解决办法
    编译asp.net文件为dll文件
    好看的表格样式
    网站IIS日志解读
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5552986.html
Copyright © 2011-2022 走看看