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 mBottomButtons = new ArrayList();
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();
}

你可能感兴趣的:(Android - SDK开发必备Dialog封装 (支持HMI操作))