Android 封装实现各种样式对话框

先上图

实现代码

package com.jock.alertdialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 精仿iOSAlertViewController控件Demo
 */
public class MainActivity extends Activity implements OnItemClickListener, OnDismissListener {

    private AlertView mAlertView;//避免创建反复View。先创建View,然后须要的时候show出来,推荐这个做法
    private AlertView mAlertViewExt;//窗体拓展样例
    private EditText etName;//拓展View内容
    private InputMethodManager imm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        mAlertView = new AlertView("标题", "内容", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, this).setCancelable(true).setOnDismissListener(this);
        //拓展窗体
        mAlertViewExt = new AlertView("提示", "请完好你的个人资料。", "取消", null, new String[]{"完毕"}, this, AlertView.Style.Alert, this);
        ViewGroup extView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.alertext_form,null);
        etName = (EditText) extView.findViewById(R.id.etName);
        etName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean focus) {
                //输入框出来则往上移动
                boolean isOpen=imm.isActive();
                mAlertViewExt.setMarginBottom(isOpen&&focus ?

120 :0);
                System.out.println(isOpen);
            }
        });
        mAlertViewExt.addExtView(extView);
    }

    public void alertShow1(View view) {
        mAlertView.show();
    }

    public void alertShow2(View view) {
        new AlertView("标题", "内容", null, new String[]{"确定"}, null, this, AlertView.Style.Alert, this).show();
    }

    public void alertShow3(View view) {
        new AlertView(null, null, null, new String[]{"高亮button1", "高亮button2", "高亮button3"},
                new String[]{"其它button1", "其它button2", "其它button3", "其它button4", "其它button5", "其它button6",
                        "其它button7", "其它button8", "其它button9", "其它button10", "其它button11", "其它button12"},
                this, AlertView.Style.Alert, this).show();
    }

    public void alertShow4(View view) {
        new AlertView("标题", null, "取消", new String[]{"高亮button1"}, new String[]{"其它button1", "其它button2", "其它button3"}, this, AlertView.Style.ActionSheet, this).show();
    }

    public void alertShow5(View view) {
        new AlertView("标题", "内容", "取消", null, null, this, AlertView.Style.ActionSheet, this).setCancelable(true).show();
    }

    public void alertShow6(View view) {
        new AlertView("上传头像", null, "取消", null,
                new String[]{"拍照", "从相冊中选择"},
                this, AlertView.Style.ActionSheet, this).show();
    }

    public void alertShowExt(View view) {
        mAlertViewExt.show();
    }
    private void closeKeyboard() {
        //关闭软键盘
        imm.hideSoftInputFromWindow(etName.getWindowToken(),0);
        //恢复位置
        mAlertViewExt.setMarginBottom(0);
    }
    @Override
    public void onItemClick(Object o,int position) {
        closeKeyboard();
        //推断是否是拓展窗体View。并且点击的是非取消button
        if(o == mAlertViewExt && position != AlertView.CANCELPOSITION){
            String name = etName.getText().toString();
            if(name.isEmpty()){
                Toast.makeText(this, "啥都没填呢", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(this, "hello,"+name, Toast.LENGTH_SHORT).show();
            }

            return;
        }
        Toast.makeText(this, "点击了第" + position + "个", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDismiss(Object o) {
        closeKeyboard();
        Toast.makeText(this, "消失了", Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
        {
            if(mAlertView!=null && mAlertView.isShowing()){
                mAlertView.dismiss();
                return false;
            }
        }

        return super.onKeyDown(keyCode, event);

    }
}

点击下载源代码

时间: 2024-08-04 01:09:23

Android 封装实现各种样式对话框的相关文章

Android 标签的主题样式

Android平台定义的主题样式: android:theme="@android:style/Theme.Dialog"   将一个Activity显示为对话框模式 •android:theme="@android:style/Theme.NoTitleBar"  不显示应用程序标题栏•android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  不显示应用程序标题栏,并全屏 •andr

【Android UI设计】Dialog对话框详解(二)

上一篇我们介绍了Dialog的基本使用方法,[Android UI设计]Dialog对话框详解(一)今天继续介绍,废话不多说,今天主要实现ProgressDialog和透明Dialog两种效果,最后介绍一下github上的一个Dialog动画开源库,里面包含多种动画特效,效果图如下: 一.ProgressDialog基本使用 1.ProgressDialog关键代码 mProgressDialog = new ProgressDialog(MainActivity.this); // 圆形pro

(转)Android系统自带样式(@android:style/)

在AndroidManifest.xml文件的activity中配置 1.android:theme="@android:style/Theme" 默认状态,即如果theme这里不填任何属性的时候,默认为Theme 2.android:theme="@android:style/Theme.NoDisplay" 任何都不显示.比较适用于只是运行了activity,但未显示任何东西 3.android:theme="@android:style/Theme.

Android中制作自定义dialog对话框的实例

http://www.jb51.net/article/83319.htm 这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继承Dialog类来制作自己的对话框,需要的朋友可以参考下 自定义dialog基础版很多时候,我们在使用android sdk提供的alerdialog的时候,会因为你的系统的不同而产生不同的效果,就好比如你刷的是MIUI的系统,弹出框都会在顶部显示!这里简单的介绍自定义弹出框的应用. 首先创建布局文件d

Android封装TitleBar基本适用所有常规开发

Android封装TitleBar基本适用所有常规开发 github地址:https://github.com/SiberiaDante/SiberiaDanteLib/blob/master/sample/src/main/notes/TitleBar.md 主要实现:(后续会继续完善,喜欢的给个star,感谢支持) 使用方法 allprojects { repositories { ... aven { url 'https://jitpack.io' } } } dependencies

Android 修改Activity标题样式 actionBar

修改Activity的标题样式及ActionBar ,代码如下 <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <!-- <item name=

【转】24. android dialog ——ProgressDialog 进度条对话框详解

原文网址:http://blog.csdn.net/jamesliulyc/article/details/6375598 首先在onCreateDialog方法里创建一个ProgressDialog,如下: [java] view plaincopy //this表示该对话框是针对当前Activity的 progressDialog = new ProgressDialog(this); //设置最大值为100 progressDialog.setMax(100); //设置进度条风格STYL

Android自定义进度条样式

最近在做一个widget,上面需要一个progressbar,产品经理和设计师给出来的东西是要实现一个圆角的progress和自定义的颜色,研究一小下,分享出来给大家哦. 测试于:Android4.0+ 操作步骤: 1.创建你的layout文件引用progressbar如下,标红处引用你自定的样式: <ProgressBar android:id="@+id/progressDownload" style="?android:attr/progressBarStyleH

Android Studio显示主题/样式设置

估计很多刚开始用Android Studio的DEV,都有经常看到网上关于Android Studio的贴图是灰色样式的,但是为啥自己刚安装的就是白色样式的呢. 这个其实只要改下显示主题就可以了. 如下图,选择Darcula就可以了,IntelliJ是默认风格,Windows这个风格其实颜色和IntelliJ是差不多的: Android Studio显示主题/样式设置,布布扣,bubuko.com