查看: 1393|回复: 0

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

[复制链接]
  • TA的每日心情

    2018-11-20 13:41
  • 签到天数: 3 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2019-2-27 16:11:35 | 显示全部楼层 |阅读模式
    分享到:
    [td]
    主要步骤
    1.BaseKeyDialog类 主要是监听按键移动操作,抽象出对应的方法
    2.BaseSmallDialog类 对Dialog方法进行覆写封装
    3.集合存储对话框按键位置 以及HMI位置
    4.案例操作SDK封装的Dialog

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

    1. public class BaseKeyDialog extends Dialog{

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

    4. public BaseKeyDialog(Context context) {
    5.     super(context);
    6. }

    7. public BaseKeyDialog(Context context, int style) {
    8.     super(context, style);
    9. }

    10. public boolean dispatchkeyEvent(android.view.KeyEvent event) {
    11.     int keyCode = event.getKeyCode();
    12.     if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT
    13.             || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
    14.             || keyCode == KeyEvent.KEYCODE_DPAD_CENTER
    15.             || keyCode == KeyEvent.KEYCODE_ENTER
    16.             || keyCode == LONG_PRESS_KEY) {
    17.         if(event.getAction() == KeyEvent.ACTION_UP) { //按键放开时
    18.             if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
    19.                 moveToUp(); //HMI向上操作
    20.             } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
    21.                 moveToDown(); //HMI向下操作
    22.             } else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
    23.                     || keyCode == KeyEvent.KEYCODE_ENTER) {
    24.                 centerSure();
    25.             } else if (keyCode == LONG_PRESS_KEY) {
    26.                 longCenterSure();
    27.             }
    28.             
    29.         }
    30.         return true;
    31.     }
    32.     return super.dispatchKeyEvent(event);
    33.    
    34. }

    35. /**
    36. * HMI向上操作
    37. */
    38. protected void moveToUp() {
    39.    
    40. }

    41. /**
    42. * HMI向下操作
    43. */
    44. protected void moveToDown() {
    45.    
    46. }

    47. /**
    48. * HMI确定操作
    49. */
    50. protected void centerSure() {
    51.    
    52. }

    53. /**
    54. * 长按操作
    55. */
    56. protected void longCenterSure() {
    57.    
    58. }
    复制代码

    2.BaseSmallDialog类 对Dialog方法进行覆写封装
    该类主要是覆写对话框里面的标题、布局、按钮、显示和隐藏等方法,以及使用ArrayList集合存储对应的对话框按键位置 以及HMI位置。
    1. public class BaseSmallDialog extends BaseKeyDialog{

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

    4. public BaseSmallDialog(Context context) {
    5.     super(context);
    6.     init();
    7. }

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

    16. private void initView(View view) {
    17.     rootView = view;
    18. }

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

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

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

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

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

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

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

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

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

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

    59. private void setButton(int buttonId, int text, View.OnClickListener listener) {
    60.     TextView button = (TextView) rootView.findViewById(buttonId);
    61.     if(button != null) {
    62.         rootView.findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
    63.         button.setText(text);
    64.         if(listener == null) {
    65.             button.setOnClickListener(closeListener);
    66.         } else {
    67.             button.setOnClickListener(listener);
    68.         }
    69.     }
    70. }

    71. private void setButton(int buttonId, String text, View.OnClickListener listener) {
    72.     TextView button = (TextView) rootView.findViewById(buttonId);
    73.     if(button != null) {
    74.         rootView.findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
    75.         button.setText(text);
    76.         if(listener == null) {
    77.             button.setOnClickListener(closeListener);
    78.         } else {
    79.             button.setOnClickListener(listener);
    80.         }
    81.     }
    82. }

    83. public void setContent(View view) {
    84.     if(view != null) {
    85.         ViewGroup content = (ViewGroup) rootView.findViewById(R.id.content);
    86.         content.setVisibility(View.VISIBLE);
    87.         content.addView(view);
    88.     }
    89. }

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

    94. private View.OnClickListener closeListener = new View.OnClickListener() {
    95.    
    96.     @Override
    97.     public void onClick(View v) {
    98.         dismiss();
    99.     }
    100. };

    101. /**
    102. * HMI操作的小标
    103. */
    104. private int mHMIIndex = 0;

    105. /**
    106. * 底部的按钮
    107. */
    108. private List<View> mBottomButtons = new ArrayList<View>();
    109. private int mBootomButtonSize;

    110. public void show() {
    111.     super.show();
    112.     prepareShowForHmi();
    113. }

    114. /**
    115. * 为了HMI的底部按钮响应做一下准工作
    116. */
    117. private void prepareShowForHmi() {
    118.     mBottomButtons.clear();
    119.     mHMIIndex = 0;
    120.    
    121.     if(findViewById(R.id.left_button_layout).getVisibility() == View.VISIBLE) {
    122.         mBottomButtons.add(findViewById(R.id.left_button));
    123.     }
    124.    
    125.     if(findViewById(R.id.middle_button_layout).getVisibility() == View.VISIBLE) {
    126.         mBottomButtons.add(findViewById(R.id.middle_button));
    127.     }
    128.    
    129.     if(findViewById(R.id.right_button_layout).getVisibility() == View.VISIBLE) {
    130.         mBottomButtons.add(findViewById(R.id.right_button));
    131.     }
    132.     mBootomButtonSize = mBottomButtons.size();
    133.     selectBootomButton();
    134. }

    135. /**
    136. * 选择底部按钮
    137. */
    138. private void selectBootomButton() {
    139.     if(canSelect()) {
    140.         if(mBottomButtons.get(mHMIIndex).isEnabled()) {
    141.             updateSelect();
    142.         } else {
    143.             nextSelect();
    144.         }
    145.     }
    146. }

    147. /**
    148. * HMI向下操作
    149. */
    150. private void nextSelect() {
    151.     if(canSelect()) {
    152.         mHMIIndex = (mHMIIndex + 1) % mBottomButtons.size();
    153.         selectBootomButton();
    154.     }
    155. }

    156. /**
    157. * HMI向上操作
    158. */
    159. private void prevSelect() {
    160.     if(canSelect()) {
    161.         mHMIIndex = (mHMIIndex - 1 + mBottomButtons.size()) % mBottomButtons.size();
    162.         selectBootomButton();
    163.     }
    164. }

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

    170. private boolean canSelect() {
    171.     for(View view: mBottomButtons) {
    172.         if(view.isEnabled()) {
    173.             return true;
    174.         }
    175.     }
    176.     return false;
    177. }

    178. @Override
    179. protected void moveToUp() {
    180.      prevSelect();
    181. }

    182. @Override
    183. protected void moveToDown() {
    184.     nextSelect();
    185. }

    186. @Override
    187. protected void centerSure() {
    188.     if(mHMIIndex >=0 && mHMIIndex < mBottomButtons.size() && mBottomButtons.get(mHMIIndex).isEnabled()) {
    189.         mBottomButtons.get(mHMIIndex).performClick();
    190.     }
    191. }

    192. @Override
    193. protected void longCenterSure() {
    194.     super.longCenterSure();
    195. }
    复制代码
    3.案例操作SDK封装的Dialog
    1. private void showSysnContactsDialog(boolean isShow) {
    2.     if(!isShow) {
    3.         if(syncDialog != null && syncDialog.isShow()) {
    4.             syncDialog.dismiss();
    5.         }
    6.         return;
    7.     }
    8.    
    9.     if(this == null) {
    10.         return;
    11.     }
    12.    
    13.     if(syncDialog == null) {
    14.         syncDialog = new BtTipsDialog(this);
    15.         syncDialog.setTitle(R.string.action_settings);
    16.         syncDialog.setCanceledOnTouchOutside(false);
    17.         View v = View.inflate(this, R.layout.dialog_bt_searching, null);
    18.         TextView text = (TextView) v.findViewById(R.id.tvTextView);
    19.         text.setText(R.string.bt_contacts_update);
    20.         syncDialog.setContent(v);
    21.     }
    22.    
    23.     syncDialog.show();
    24. }
    复制代码



    作者:ZebraWei
    来源:简书
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 02:29 , Processed in 0.111481 second(s), 14 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.