Android - SDK开发必备Dialog封装 (支持HMI操作)

主要步骤
1.BaseKeyDialog类 主要是监听按键移动操作,抽象出对应的方法
2.BaseSmallDialog类 对Dialog方法进行覆写封装
3.集合存储对话框按键位置 以及HMI位置
4.案例操作SDK封装的Dialog
主要思路
  • 1.HMI操作:按键向下向上移动,以及按键确定和长按操作.。
  • 2.在项目开发中,创建Dialog是必备。如果在每个对应类中去创建对应的Dialog,这样就会显得很累赘。这是我们需要在自己SDK里面创建自己需要的统一风格Dialog.
    以便客户端直接覆写里面对应的方法和实现对应的接口。
  • 3.HMI选择操作:用户可以通过方控旋转按钮来操作对应的页面(主要在车机和智能家居行业)。
1.BaseKeyDialog类 监听按键移动操作,抽象出对应的方法

dispatchkeyEvent方法:监听HMI按键向下向上移动,以及按键确定和长按操作。

public class BaseKeyDialog extends Dialog{

/**HMI长按*/
private static final int LONG_PRESS_KEY = 0x142;

public BaseKeyDialog(Context context) {
    super(context);
}

public BaseKeyDialog(Context context, int style) {
    super(context, style);
}

public boolean dispatchkeyEvent(android.view.KeyEvent event) {
    int keyCode = event.getKeyCode();
    if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT
            || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
            || keyCode == KeyEvent.KEYCODE_DPAD_CENTER
            || keyCode == KeyEvent.KEYCODE_ENTER
            || keyCode == LONG_PRESS_KEY) {
        if(event.getAction() == KeyEvent.ACTION_UP) { //按键放开时
            if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                moveToUp(); //HMI向上操作
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                moveToDown(); //HMI向下操作
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
                    || keyCode == KeyEvent.KEYCODE_ENTER) {
                centerSure();
            } else if (keyCode == LONG_PRESS_KEY) {
                longCenterSure();
            }
            
        } 
        return true;
    }
    return super.dispatchKeyEvent(event);
    
}

/**
 * HMI向上操作
 */
protected void moveToUp() {
    
}

/**
 * HMI向下操作
 */
protected void moveToDown() {
    
}

/**
 * HMI确定操作
 */
protected void centerSure() {
    
}

/**
 * 长按操作
 */
protected void longCenterSure() {
    
}
2.BaseSmallDialog类 对Dialog方法进行覆写封装

该类主要是覆写对话框里面的标题、布局、按钮、显示和隐藏等方法,以及使用ArrayList集合存储对应的对话框按键位置 以及HMI位置。

public class BaseSmallDialog extends BaseKeyDialog{

private View rootView;
protected static final String TAG = BaseSmallDialog.class.getSimpleName();

public BaseSmallDialog(Context context) {
    super(context);
    init();
}

private void init() {
    View view = View.inflate(getContext(), R.layout.dialog_small, null);
    initView(view);
    setContentView(view);
    LayoutParams ls = getWindow().getAttributes();
    ls.width = (int) getContext().getResources().getDimension(R.dimen.small_dialog_width);
    ls.height = (int) getContext().getResources().getDimension(R.dimen.small_dialog_height);
}

private void initView(View view) {
    rootView = view;
}

public void setMessage(String message) {
    ((TextView)rootView.findViewById(R.id.message)).setText(message);
    rootView.findViewById(R.id.message).setVisibility(View.VISIBLE);
}

public void setMessage(int message) {
    ((TextView)rootView.findViewById(R.id.message)).setText(message);
    rootView.findViewById(R.id.message).setVisibility(View.VISIBLE);
}

public void setTitle(String message) {
    ((TextView)rootView.findViewById(R.id.title)).setText(message);
    rootView.findViewById(R.id.title).setVisibility(View.VISIBLE);
}

public void setTitle(int message) {
    ((TextView)rootView.findViewById(R.id.title)).setText(message);
    rootView.findViewById(R.id.title).setVisibility(View.VISIBLE);
}

public void setLeftButton(int text, View.OnClickListener listener) {
    rootView.findViewById(R.id.left_button_layout).setVisibility(View.VISIBLE);
    setButton(R.id.left_button, text, listener);
}

public void setLeftButton(String text, View.OnClickListener listener) {
    rootView.findViewById(R.id.left_button_layout).setVisibility(View.VISIBLE);
    setButton(R.id.left_button, text, listener);
}

public void setMiddleButton(int text, View.OnClickListener listener) {
    rootView.findViewById(R.id.middle_button_layout).setVisibility(View.VISIBLE);
    setButton(R.id.middle_button, text, listener);
}

public void setMiddleButton(String text, View.OnClickListener listener) {
    rootView.findViewById(R.id.middle_button_layout).setVisibility(View.VISIBLE);
    setButton(R.id.middle_button, text, listener);
}

public void setRightButton(int text, View.OnClickListener listener) {
    rootView.findViewById(R.id.right_button_layout).setVisibility(View.VISIBLE);
    setButton(R.id.right_button, text, listener);
}

public void setRightButton(String text, View.OnClickListener listener) {
    rootView.findViewById(R.id.right_button_layout).setVisibility(View.VISIBLE);
    setButton(R.id.right_button, text, listener);
}

private void setButton(int buttonId, int text, View.OnClickListener listener) {
    TextView button = (TextView) rootView.findViewById(buttonId);
    if(button != null) {
        rootView.findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
        button.setText(text);
        if(listener == null) {
            button.setOnClickListener(closeListener);
        } else {
            button.setOnClickListener(listener);
        }
    }
}

private void setButton(int buttonId, String text, View.OnClickListener listener) {
    TextView button = (TextView) rootView.findViewById(buttonId);
    if(button != null) {
        rootView.findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
        button.setText(text);
        if(listener == null) {
            button.setOnClickListener(closeListener);
        } else {
            button.setOnClickListener(listener);
        }
    }
}

public void setContent(View view) {
    if(view != null) {
        ViewGroup content = (ViewGroup) rootView.findViewById(R.id.content);
        content.setVisibility(View.VISIBLE);
        content.addView(view);
    }
}

public void setContext(int viewId) {
    View view = View.inflate(getContext(), viewId, null);
    setContent(view);
}

private View.OnClickListener closeListener = new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
        dismiss();
    }
};

/**
 * HMI操作的小标
 */
private int mHMIIndex = 0;

/**
 * 底部的按钮
 */
private List<View> mBottomButtons = new ArrayList<View>();
private int mBootomButtonSize;

public void show() {
    super.show();
    prepareShowForHmi();
}

/**
 * 为了HMI的底部按钮响应做一下准工作
 */
private void prepareShowForHmi() {
    mBottomButtons.clear();
    mHMIIndex = 0;
    
    if(findViewById(R.id.left_button_layout).getVisibility() == View.VISIBLE) {
        mBottomButtons.add(findViewById(R.id.left_button));
    }
    
    if(findViewById(R.id.middle_button_layout).getVisibility() == View.VISIBLE) {
        mBottomButtons.add(findViewById(R.id.middle_button));
    }
    
    if(findViewById(R.id.right_button_layout).getVisibility() == View.VISIBLE) {
        mBottomButtons.add(findViewById(R.id.right_button));
    }
    mBootomButtonSize = mBottomButtons.size();
    selectBootomButton();
}

/**
 * 选择底部按钮
 */
private void selectBootomButton() {
    if(canSelect()) {
        if(mBottomButtons.get(mHMIIndex).isEnabled()) {
            updateSelect();
        } else {
            nextSelect();
        }
    }
}

/**
 * HMI向下操作
 */
private void nextSelect() {
    if(canSelect()) {
        mHMIIndex = (mHMIIndex + 1) % mBottomButtons.size();
        selectBootomButton();
    }
}

/**
 * HMI向上操作
 */
private void prevSelect() {
    if(canSelect()) {
        mHMIIndex = (mHMIIndex - 1 + mBottomButtons.size()) % mBottomButtons.size();
        selectBootomButton();
    }
}

private void updateSelect() {
    for(int i = 0; i < mBootomButtonSize; i++) {
        mBottomButtons.get(i).setSelected(mHMIIndex == i);
    }
}

private boolean canSelect() {
    for(View view: mBottomButtons) {
        if(view.isEnabled()) {
            return true;
        }
    }
    return false;
}

@Override
protected void moveToUp() {
     prevSelect();
}

@Override
protected void moveToDown() {
    nextSelect();
}

@Override
protected void centerSure() {
    if(mHMIIndex >=0 && mHMIIndex < mBottomButtons.size() && mBottomButtons.get(mHMIIndex).isEnabled()) {
        mBottomButtons.get(mHMIIndex).performClick();
    }
}

@Override
protected void longCenterSure() {
    super.longCenterSure();
}
3.案例操作SDK封装的Dialog
private void showSysnContactsDialog(boolean isShow) {
    if(!isShow) {
        if(syncDialog != null && syncDialog.isShow()) {
            syncDialog.dismiss();
        }
        return;
    }
    
    if(this == null) {
        return;
    }
    
    if(syncDialog == null) {
        syncDialog = new BtTipsDialog(this);
        syncDialog.setTitle(R.string.action_settings);
        syncDialog.setCanceledOnTouchOutside(false);
        View v = View.inflate(this, R.layout.dialog_bt_searching, null);
        TextView text = (TextView) v.findViewById(R.id.tvTextView);
        text.setText(R.string.bt_contacts_update);
        syncDialog.setContent(v);
    }
    
    syncDialog.show();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,639评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,093评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,079评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,329评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,343评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,047评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,645评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,565评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,095评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,201评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,338评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,014评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,701评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,194评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,320评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,685评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,345评论 2 358

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,245评论 25 707
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 13,780评论 0 15
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,440评论 0 17
  • 亲爱的朋友们,大家好!我是以琳,新学期开始一周多了,你和孩子相处得怎么样?有没有碰到一些挑战让你很抓狂,心中的小火...
    王以琳阅读 711评论 0 50
  • 小舅舅已经做了三十年的机械维修,国企改制后,就在镇上开了一家纺织机械维修部。随着周边纺织厂生意越来越差,舅舅的维修...
    倪茗希阅读 281评论 0 0