`
canofy
  • 浏览: 829771 次
  • 性别: Icon_minigender_1
  • 来自: 北京、四川
社区版块
存档分类
最新评论

android中的弹出框提示

阅读更多
基本步骤如下:
1、继承Activity
2、在需要提示地方showDialog(id);
3、重载Activity的onCreateDialog(id)的方法
如:
@Override
    protected Dialog onCreateDialog(int id) {
          return new AlertDialog.Builder(this)
                        .setMessage("Please update your Settings.")
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                //updatePreferences();
                            }
                        })
                        .setNegativeButton(
                                android.R.string.cancel, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        // Noop.
                                    }
                        })
                        .create();
    }

4、运行android即可

5、有时在一个Activity中有多个不同样式的showDialog(id),则需要对onCreateDialog(id)方法重写成下面对方式:
 @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case CALL_ADDRESS:

                LayoutInflater factory = LayoutInflater.from(this);
                final View textBoxView = factory.inflate(R.layout.call_address_dialog, null);
                return new AlertDialog.Builder(this)
                        .setTitle("title.")
                        .setView(textBoxView)
                        .setPositiveButton(
                                android.R.string.ok, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        EditText textField = (EditText)
                                                (textBoxView.findViewById(R.id.calladdress_edit));
                                        sipAddress = textField.getText().toString();
                                        initiateCall();

                                    }
                        })
                        .setNegativeButton(
                                android.R.string.cancel, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        // Noop.
                                    }
                        })
                        .create();

            case UPDATE_SETTINGS_DIALOG:
                return new AlertDialog.Builder(this)
                        .setMessage("Please update your Settings.")
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                //updatePreferences();
                            }
                        })
                        .setNegativeButton(
                                android.R.string.cancel, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        // Noop.
                                    }
                        })
                        .create();
        }
        return null;
    }


switch (id)是对showDialog(id)中不同的ID选择不同的AlertDialog
View则可以对Dialog进行一些样式的修改


以下是android自带的apidemos下的比较完整弹出框例子
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.database.Cursor;
import android.provider.Contacts;

import com.example.android.apis.R;

/**
 * Example of how to use an {@link android.app.AlertDialog}.
 * <h3>AlertDialogSamples</h3>

<p>This demonstrates the different ways the AlertDialog can be used.</p>

<h4>Demo</h4>
App/Dialog/Alert Dialog
 
<h4>Source files</h4>
 * <table class="LinkTable">
 *         <tr>
 *             <td >src/com.example.android.apis/app/AlertDialogSamples.java</td>
 *             <td >The Alert Dialog Samples implementation</td>
 *         </tr>
 *         <tr>
 *             <td >/res/any/layout/alert_dialog.xml</td>
 *             <td >Defines contents of the screen</td>
 *         </tr>
 * </table> 
 */
public class AlertDialogSamples extends Activity {
    private static final int DIALOG_YES_NO_MESSAGE = 1;
    private static final int DIALOG_YES_NO_LONG_MESSAGE = 2;
    private static final int DIALOG_LIST = 3;
    private static final int DIALOG_PROGRESS = 4;
    private static final int DIALOG_SINGLE_CHOICE = 5;
    private static final int DIALOG_MULTIPLE_CHOICE = 6;
    private static final int DIALOG_TEXT_ENTRY = 7;
    private static final int DIALOG_MULTIPLE_CHOICE_CURSOR = 8;

    private static final int MAX_PROGRESS = 100;
    
    private ProgressDialog mProgressDialog;
    private int mProgress;
    private Handler mProgressHandler;

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_YES_NO_MESSAGE:
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_two_buttons_title)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Cancel so do some stuff */
                    }
                })
                .create();
        case DIALOG_YES_NO_LONG_MESSAGE:
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_two_buttons_msg)
                .setMessage(R.string.alert_dialog_two_buttons2_msg)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
    
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Something so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Cancel so do some stuff */
                    }
                })
                .create();
        case DIALOG_LIST:
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setTitle(R.string.select_dialog)
                .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        /* User clicked so do some stuff */
                        String[] items = getResources().getStringArray(R.array.select_dialog_items);
                        new AlertDialog.Builder(AlertDialogSamples.this)
                                .setMessage("You selected: " + which + " , " + items[which])
                                .show();
                    }
                })
                .create();
        case DIALOG_PROGRESS:
            mProgressDialog = new ProgressDialog(AlertDialogSamples.this);
            mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
            mProgressDialog.setTitle(R.string.select_dialog);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMax(MAX_PROGRESS);
            mProgressDialog.setButton(getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            });
            mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            });
            return mProgressDialog;
        case DIALOG_SINGLE_CHOICE:
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_single_choice)
                .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked on a radio button do some stuff */
                    }
                })
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Yes so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked No so do some stuff */
                    }
                })
               .create();
        case DIALOG_MULTIPLE_CHOICE:
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.ic_popup_reminder)
                .setTitle(R.string.alert_dialog_multi_choice)
                .setMultiChoiceItems(R.array.select_dialog_items3,
                        new boolean[]{false, true, false, true, false, false, false},
                        new DialogInterface.OnMultiChoiceClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton,
                                    boolean isChecked) {

                                /* User clicked on a check box do some stuff */
                            }
                        })
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Yes so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked No so do some stuff */
                    }
                })
               .create();
            case DIALOG_MULTIPLE_CHOICE_CURSOR:
                String[] projection = new String[] {
                        Contacts.People._ID,
                        Contacts.People.NAME,
                        Contacts.People.SEND_TO_VOICEMAIL
                };
                Cursor cursor = managedQuery(Contacts.People.CONTENT_URI, projection, null, null, null);
                return new AlertDialog.Builder(AlertDialogSamples.this)
                    .setIcon(R.drawable.ic_popup_reminder)
                    .setTitle(R.string.alert_dialog_multi_choice_cursor)
                    .setMultiChoiceItems(cursor,
                            Contacts.People.SEND_TO_VOICEMAIL,
                            Contacts.People.NAME,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton,
                                        boolean isChecked) {
                                    Toast.makeText(AlertDialogSamples.this,
                                            "Readonly Demo Only - Data will not be updated",
                                            Toast.LENGTH_SHORT).show();
                                }
                            })
                   .create();
        case DIALOG_TEXT_ENTRY:
            // This example shows how to add a custom layout to an AlertDialog
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_text_entry)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
    
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();
        }
        return null;
    }

    /**
     * Initialization of the Activity after it is first created.  Must at least
     * call {@link android.app.Activity#setContentView(int)} to
     * describe what is to be displayed in the screen.
     */
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.alert_dialog);
                
        /* Display a text message with yes/no buttons and handle each message as well as the cancel action */
        Button twoButtonsTitle = (Button) findViewById(R.id.two_buttons);
        twoButtonsTitle.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_YES_NO_MESSAGE);
            }
        });
        
        /* Display a long text message with yes/no buttons and handle each message as well as the cancel action */
        Button twoButtons2Title = (Button) findViewById(R.id.two_buttons2);
        twoButtons2Title.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_YES_NO_LONG_MESSAGE);
            }
        });
        
        
        /* Display a list of items */
        Button selectButton = (Button) findViewById(R.id.select_button);
        selectButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_LIST);
            }
        });
        
        /* Display a custom progress bar */
        Button progressButton = (Button) findViewById(R.id.progress_button);
        progressButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_PROGRESS);
                mProgress = 0;
                mProgressDialog.setProgress(0);
                mProgressHandler.sendEmptyMessage(0);
            }
        });
        
        /* Display a radio button group */
        Button radioButton = (Button) findViewById(R.id.radio_button);
        radioButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_SINGLE_CHOICE);
            }
        });
        
        /* Display a list of checkboxes */
        Button checkBox = (Button) findViewById(R.id.checkbox_button);
        checkBox.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_MULTIPLE_CHOICE);
            }
        });
        
        /* Display a list of checkboxes, backed by a cursor */
        Button checkBox2 = (Button) findViewById(R.id.checkbox_button2);
        checkBox2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_MULTIPLE_CHOICE_CURSOR);
            }
        });

        /* Display a text entry dialog */
        Button textEntry = (Button) findViewById(R.id.text_entry_button);
        textEntry.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG_TEXT_ENTRY);
            }
        });
        
        mProgressHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (mProgress >= MAX_PROGRESS) {
                    mProgressDialog.dismiss();
                } else {
                    mProgress++;
                    mProgressDialog.incrementProgressBy(1);
                    mProgressHandler.sendEmptyMessageDelayed(0, 100);
                }
            }
        };
    }
}

分享到:
评论

相关推荐

    Android:动画实现精美的弹出框(仿易信)

    在Android开发中,创建引人注目的用户界面是至关重要的,而弹出框作为与用户交互的一种常见方式,其设计和实现方式直接影响到用户体验。本篇文章将深入探讨如何使用动画来实现一个精美的弹出框,类似易信应用中的...

    android自定义弹出框实现

    在Android开发中,自定义弹出框是一种常见的需求,它能提供更加个性化和与应用风格一致的用户体验。本文将深入探讨如何在Android中实现自定义弹出框,并以"CustomDialog"为例,介绍其实现过程。 首先,Android系统...

    android蓝牙调用弹出框

    android蓝牙使用,弹出框选择,获取名字和地址

    android弹出框和加载等待

    在Android开发中,用户体验是至关重要的,而"android弹出框和加载等待"就是提升用户体验的重要手段之一。本文将深入探讨这两个概念,并提供实现自定义弹出框和加载等待效果的方法。 首先,让我们来理解一下Android...

    android弹出框

    在Android开发中,弹出框...总之,Android弹出框是与用户交互的重要工具,根据需求选择合适类型的弹出框并进行定制,可以极大地提升用户体验。理解并熟练运用这些弹出框类型,对于Android开发者来说是至关重要的。

    android 自定义退出提示弹出框

    在Android应用开发中,自定义退出提示弹出框是一个常见的需求,它可以帮助用户在退出应用时得到明确的提示,提高用户体验。本篇文章将详细讲解如何实现这样一个功能。 首先,我们来了解一下`Dialog`和`PopupWindow`...

    Android弹出框

    本篇将深入探讨Android弹出框的使用,包括它的类型、创建方法以及自定义实现。 1. **弹出框类型** - AlertDialog:标准的弹出框,包含标题、内容和按钮区域。 - AlertDialog.Builder:用于构建AlertDialog的对象...

    动态的弹出框,可以自定义布局,使用简单

    一个动态的弹出框,可以做动态提示什么的,自定义布局放入就可以。使用非常简单,并且做了性能上的优化,内存占用较小。但是发现个长时间弹出内存会升高,暂未发现原因

    js模拟android弹出的友好提示框

    本篇文章将深入探讨如何使用JS模拟Android系统的弹出提示框,实现更友好的交互设计。 首先,我们需要了解`alert()`的基本用法。`alert()`是JavaScript中的一个全局函数,用于显示一个带有可选消息和OK按钮的警告...

    Android的弹出框按钮点击接口监听器向C#事件的转换

    实现了Android的弹出框按钮点击接口监听器向C#事件的转换,原生android的listener变为mono android的event.

    android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    在Android应用开发中,标题栏(Toolbar)上弹出提示框是常见的交互设计,用于向用户展示临时信息或提供简短的操作选项。本教程将详细讲解如何使用PopupWindow实现这样的功能,并添加动画效果,以提升用户体验。...

    android常用弹出框

    在Android开发中,弹出框是用户界面设计中不可或缺的一部分,它们用于提供额外的信息或接收用户的输入。在Android系统中,有三种常见的弹出框实现方式:Dialog、PopupWindow和DialogFragment。每种都有其特点和适用...

    模拟弹出框

    在移动应用开发中,Android和iOS平台都有内置的弹出框API。例如,Android的AlertDialog和iOS的UIAlertController可以用来创建系统级别的弹出框。然而,为了追求更一致的视觉风格和增强用户体验,开发者往往会选择...

    Android 点击listview弹出popupwindow弹出框

    本篇将详细介绍如何在用户点击`ListView`时弹出一个包含`PopupWindow`的弹出框,并且这个`PopupWindow`内还具有`ListView`的右滑删除功能。 首先,我们需要创建`PopupWindow`。`PopupWindow`的构造函数通常接收三个...

    带三角的dialog弹出框

    本文将详细探讨如何实现一个带有三角形指示箭头的对话框,即"带三角的dialog弹出框",并分享相关代码实现。 首先,我们需要理解Android中的Dialog。它是一个轻量级窗口,通常用来显示临时信息或获取用户输入。在...

    android 弹出窗口

    在Android开发中,弹出窗口(PopupWindow)是一种常见的用户交互...以上就是关于Android弹出窗口(PopupWindow)的一些核心知识点,它在实际开发中有着广泛的应用,提供了丰富的自定义空间,使得UI设计更加灵活多样。

    java的弹出框

    标题中的"java的弹出框"着重指出了我们将在Java(尤其是Android)环境中讨论弹出框的实现。在这个项目中,开发者已经对`AlertDialog`和`Dialog`进行了封装,创建了多种样式的弹出框,以适应不同的UI需求。这些样式...

    有关于android弹出框

    这篇博客“有关于android弹出框”可能深入探讨了如何在Android应用中创建和使用自定义对话框,以及相关的源码分析。以下是关于Android弹出框的一些关键知识点: 1. **AlertDialog的构建**: Android提供了...

    Android-BubbleLayout弹出提示气泡

    在Android应用开发中,创建吸引用户的交互界面是至关重要的,其中一种常见的设计元素就是提示气泡(BubbleLayout)。气泡提示通常用于引导用户、显示快捷操作或者提供上下文相关的帮助信息。本文将深入探讨Android中...

    AnimDislog(自定义弹出框-监听系统返回键).rar

    在自定义弹出框中,我们可能希望当用户按下返回键时,不是直接退出应用而是显示特定的提示或行为。这可以通过重写`onBackPressed()`方法来实现,通常这个方法位于包含弹出框的Activity中。在`onBackPressed()`里,你...

Global site tag (gtag.js) - Google Analytics