zoukankan      html  css  js  c++  java
  • 【Android】Android中AlertDialog对话框的使用实例

    package com.ceac.deng;
    
    import android.R.string;
    import android.support.v7.app.ActionBarActivity;
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.os.Looper;
    import android.os.SystemClock;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Chronometer;
    import android.widget.Toast;
    import android.widget.Chronometer.OnChronometerTickListener;
    
    
    public class MainActivity extends ActionBarActivity {
    
    	private Chronometer chronometer;
    	private Button stopButton;
    	private Button startButton;
    	
    	private Button button;
    	private Button button2;
    	private Button button3;
    	private Button button4;
    	
    	Thread myThread;
    	
        boolean[] checkedItems = new boolean[] {false, true, false, true, false, true};
        String[] items = new String[] {"植物大战僵尸", "愤慨的 小鸟", "泡泡龙", "开心农场", "QQ飞车", "超级玛丽"};
    	
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            findView();
            
            chronometer.setBase(SystemClock.elapsedRealtime());
    //        chronometer.setFormat("已经用了多少时间:  %s");
            chronometer.start();
            
            chronometer.setOnChronometerTickListener(new OnChronometerTickListener() {
    			
    			@Override
    			public void onChronometerTick(Chronometer chronometer) {
    				// TODO Auto-generated method stub
    				if (SystemClock.elapsedRealtime() - chronometer.getBase() >= 100000)
    				{
    					chronometer.stop();
    				}
    			}
    		});
            
            setListenner();
        }
    
        /**
         * findView
         */
        private void findView()
        {
        	chronometer = (Chronometer) findViewById(R.id.chronometer1);
        	stopButton = (Button) findViewById(R.id.stop);
        	startButton = (Button) findViewById(R.id.start);
        	
        	button = (Button) findViewById(R.id.button1);
        	button2 = (Button) findViewById(R.id.button2);
        	button3 = (Button) findViewById(R.id.button3);
        	button4 = (Button) findViewById(R.id.button4);
        	
        	
        }
        
        private void setListenner()
        {
        	stopButton.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				chronometer.stop();
    				System.out.println("stop chronometer....");
    			}
    		});
        	
        	startButton.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				chronometer.setBase(SystemClock.elapsedRealtime());
    				chronometer.start();
    				System.out.println("start Chronometer....");
    			}
    		});
        	
        	button.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    
    //				showAlertDialog();
    				myThread = new Thread(new Runnable() {
    					
    					@Override
    					public void run() {
    						Looper.prepare();
    						showAlertDialog();
    						System.out.println("hello===============>");
    						try {
    							Thread.sleep(1000);
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						}
    						
    						Looper.loop();
    					}
    				});
    				
    				myThread.start();
    				
    			}
    		});
        	
        	button2.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				showListDialog();
    			}
    		});
        	
        	button3.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				showSingleChoiceDialog();
    			}
    		});
        	
        	button4.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				showMultiChoiceDialog();
    			}
    		});
        	
        	
        }
        
        /**
         * showDialog
         */
        private void showAlertDialog()
        {
    //		AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
    //		alertDialog.setIcon(R.drawable.ic_launcher);
    //		alertDialog.setTitle("系统提示");
    //		alertDialog.setMessage("带取消 、中立、确定按钮的对话框");
    ////		alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", listener);
        	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    		builder.setIcon(R.drawable.ic_launcher);
    		builder.setTitle("系统提示");
    		builder.setMessage("带取消 、中立、确定按钮的对话框");
    		
    		//确定
    		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub
    				showToast("点击了确定的按钮");
    			}
    		});
    		
    		//中立
    		builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub
    				showToast("点击了中立按钮");
    			}
    		});
    		
    		//取消
    		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub
    				showToast("点击了取消按钮");
    			}
    		});
    		
    		builder.show();	
        }
        
        /**
         * 带列表的对话框
         */
        private void showListDialog()
        {
        	final String[] items = new String[] {"跑步", "羽毛球", "足球"};
        	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        	builder.setIcon(R.drawable.ic_launcher);
        	builder.setTitle("请选择相应的运动项目");
        	builder.setItems(items, new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub
    				showToast(items[which]);
    			}
    		}); 	
        	builder.show();	
        }
        
        /**
         * 带有单选列表的和一个按钮的对话框
         */
        private void showSingleChoiceDialog()
        {
        	final String[] items = new String[] {"标准模式", "静音模式", "会议模式", "飞行模式", "户外模式"};
        	Builder builder = new AlertDialog.Builder(MainActivity.this);
        	builder.setIcon(R.drawable.ic_launcher);
        	builder.setTitle("请你选择须要的情景模式");
        	builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub
    				showToast("你选择了" + items[which]);
    			}
    		});
        	builder.setPositiveButton("确定", null);
        	builder.show();
        }
        
        /**
         * 带有多选列表项和一个确定按钮的列表对话框
         */
        private void showMultiChoiceDialog()
        {
    //    	final boolean[] checkedItems = new boolean[] {false, true, false, true, false};
    //    	String[] items = new String[] {"植物大战僵尸", "愤慨的 小鸟", "泡泡龙", "开心农场", "QQ飞车", "超级玛丽"};
        	
        	Builder builder = new AlertDialog.Builder(MainActivity.this);
        	builder.setIcon(R.drawable.ic_launcher);
        	builder.setTitle("请选择你喜欢的游戏");
        	builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    				// TODO Auto-generated method stub
    				checkedItems[which] = isChecked;
    			}
    		});
        	
        	builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub
    				String result = "";
    				for (int i= 0; i < checkedItems.length; i++)
    				{
    					if (checkedItems[i])
    					{
    						result += items[i];
    						result += "  ";
    					}
    				}
    				
    				if (!"".equals(result))
    				{
    					showToast(result);
    				}
    			}
    		});
        	
        	builder.show();
        }
        
        /**
         * 
         * @param str
         */
        private void showToast(String str)
        {
        	Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
        }
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    

  • 相关阅读:
    web页面前图标
    leetcode收获
    Shell统计函数耗时(实现数字运算)
    Shell判断数值是否存在于列表
    设置Ubuntu虚拟机硬件时间与系统同步
    Python捕获键盘中断^C方法(Ctrl-C)
    Shell创建zip文件不包含完整路径方法
    jquery判断复选框checkbox是否被选中
    php Base64编码/解码
    php二维数组排序
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6780390.html
Copyright © 2011-2022 走看看