【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);
    }
}

相关文章
|
6月前
|
Java 关系型数据库 数据库
Android App连接真机步骤与APP的开发语言和工程结构讲解以及运行实例(超详细必看)
Android App连接真机步骤与APP的开发语言和工程结构讲解以及运行实例(超详细必看)
96 0
|
6月前
|
Android开发
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
95 0
|
6月前
|
Android开发
Android应用实例(一)之---有道辞典VZ.0
Android应用实例(一)之---有道辞典VZ.0
43 2
|
3月前
|
API Android开发
Android使用AlertDialog实现弹出菜单
本文分享了在Android开发中使用AlertDialog实现弹出菜单的方法,并通过代码示例和错误处理,展示了如何避免因资源ID找不到导致的crash问题。
61 1
|
4月前
|
API Android开发
Android 监听Notification 被清除实例代码
Android 监听Notification 被清除实例代码
|
5月前
|
安全 Java Android开发
使用Unidbg进行安卓逆向实例讲解
使用Unidbg进行安卓逆向实例讲解
132 2
|
5月前
|
Android开发 UED
|
5月前
|
API Android开发
32. 【Android教程】对话框:AlertDialog
32. 【Android教程】对话框:AlertDialog
86 2
|
4月前
|
API Android开发 UED
Android中使用AlertDialog创建对话框
Android中使用AlertDialog创建对话框
|
6月前
|
Android开发
Android之日期及时间选择对话框
Android之日期及时间选择对话框
29 1