Android Dialog

Android Dialog
package com.he.h0317_dialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.TableLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Button commonDialog;
    private Button radioButtonDialog;
    private Button checkboxDialog;
    private Button listDialog;
    private Button customDialog;
    
    private static final int NOTIFICATION_ID = 0x1101;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        findViewById();
    }
    
    public void findViewById() {
        commonDialog = (Button) findViewById(R.id.commonDialog);
        radioButtonDialog = (Button) findViewById(R.id.radioButtonDialog);
        checkboxDialog = (Button) findViewById(R.id.checkboxDialog);
        listDialog = (Button) findViewById(R.id.listDialog);
        customDialog = (Button) findViewById(R.id.customDialog);
        
        commonDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openCommonDialog();
            }
        });
        radioButtonDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openRadioButtonDialog();
            }
        });
        checkboxDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openCheckboxDialog();
            }
        });
        listDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openListDialog();
            }
        });
        customDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openCustomDialog();
            }
        });
    }

    @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;
    }
    
    /**
     * 普通的对话框
     */
    public void openCommonDialog() {
        //builder 设计模式
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框的标题
        builder.setTitle("请确认您的电子邮箱地址");
        builder.setMessage(Html.fromHtml("<font color=‘red‘>消息内容了</font>"));
        
        builder.setIcon(getResources().getDrawable(R.drawable.mail));
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消按钮, which: " + which, Toast.LENGTH_SHORT).show();
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Notification notify = new Notification(R.drawable.games, "确认按钮, which:" + which, System.currentTimeMillis());
                //为notify设置声音
                notify.defaults = Notification.DEFAULT_SOUND;
                //为notify设置默认声音、默认震动、默认闪光灯
                notify.defaults = Notification.DEFAULT_ALL;
                
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                //Context context, int requestCode, Intent intent, int flags
                PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                
                //让声音、振动无限循环,直到用户响应
                notify.flags |= Notification.FLAG_INSISTENT;
                //通知被点击后,自动消失
                notify.flags |= Notification.FLAG_AUTO_CANCEL;
                //点击‘Clear‘时,不清楚该通知(QQ的通知无法清除,就是用的这个)
                notify.flags |= Notification.FLAG_NO_CLEAR;
                
                //设置事件信息
                notify.setLatestEventInfo(MainActivity.this, "通知 contentTitle", "通知 contentText", pi);
                //获取系统的NotificationManager 服务
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(NOTIFICATION_ID, notify);
            }
        });
        
        builder.setNeutralButton("中立的按钮", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "中立按钮, which: " + which, Toast.LENGTH_SHORT).show();
            }
        });
        
        Dialog dialog = builder.create();
        dialog.show();
    }
    
    /**
     * 单选按钮对话框
     */
    public void openRadioButtonDialog() {
        //builder 设计模式
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框的标题
        builder.setTitle("请选择您喜欢的项目");
        
        builder.setIcon(getResources().getDrawable(R.drawable.audio_player));
        String[] items = new String[]{"游泳", "足球", "电玩"};
        
        builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你选择了, which: " + which, Toast.LENGTH_SHORT).show();
            }
            
        });
        
        builder.setPositiveButton("OK", null);
        
        Dialog dialog = builder.create();
        dialog.show();
    }
    
    /**
     * 多选按钮对话框
     */
    public void openCheckboxDialog() {
        //builder 设计模式
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框的标题
        builder.setTitle("请选择您喜欢的项目");
        
        builder.setIcon(getResources().getDrawable(R.drawable.audio_player));
        final String[] items = new String[]{"游泳", "足球", "电玩"};
        boolean[] select = new boolean[]{true, false, true};
        
        builder.setMultiChoiceItems(items, select, new DialogInterface.OnMultiChoiceClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(MainActivity.this, "你选择了, which: " + items[which] + "  " + (isChecked ? "ON" : "OFF") , Toast.LENGTH_SHORT).show();
            }
        });
        
        builder.setPositiveButton("OK", null);
        
        Dialog dialog = builder.create();
        dialog.show();
    }
    
    /**
     * 列表对话框
     */
    public void openListDialog() {
        //builder 设计模式
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框的标题
        builder.setTitle("请选择您喜欢的项目");
        
        builder.setIcon(getResources().getDrawable(R.drawable.radio));
        final String[] items = new String[]{"游泳", "足球", "电玩", "进修", "美人"};
        final int[] icons = new int[]{R.drawable.audio_player, R.drawable.camera, R.drawable.games, R.drawable.mail, R.drawable.radio};

        final List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(5);
        for(int i = 0; i < items.length; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("dialog_name", items[i]);
            item.put("dialog_icon", icons[i]);
            listItems.add(item);
        }
        
        SimpleAdapter adapter = new SimpleAdapter(this
                , listItems
                , R.layout.list_item
                , new String[]{"dialog_name", "dialog_icon"}
                , new int[]{R.id.dialog_name, R.id.dialog_icon});

        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Map<String, Object> item = listItems.get(which);
                Toast.makeText(MainActivity.this, "你选择了, which: " + item.get("dialog_name"), Toast.LENGTH_SHORT).show();
            }
        });
        
        Dialog dialog = builder.create();
        dialog.show();
    }
    
    /**
     * 自定义对话框
     */
    public void openCustomDialog() {
        //builder 设计模式
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框的标题
        builder.setTitle("请选择您的信息");
        
        builder.setIcon(getResources().getDrawable(R.drawable.radio));
        
        //装载layout
        final TableLayout login = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
        builder.setView(login);
        
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText usernameEdit = (EditText) login.findViewById(R.id.usernameEdit);
                
                if(usernameEdit != null) {
                    Toast.makeText(MainActivity.this, "用户名:" + usernameEdit.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        
        Dialog dialog = builder.create();
        dialog.show();
    }

}
Android Dialog

Android Dialog,布布扣,bubuko.com

Android Dialog

上一篇:Android TextView 嵌套图片及其点击,TextView 部分文字点击


下一篇:Android中集成第三方软件包