SettingBarView.java 4.6 KB
Newer Older
门心叼龙's avatar
门心叼龙 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package com.fly.tour.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fly.tour.common.R;

/**
 * Description: <SettingBarView><br>
M
mxdl 已提交
18
 * Author:      mxdl<br>
门心叼龙's avatar
门心叼龙 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 * Date:        2019/3/4<br>
 * Version:     V1.0.0<br>
 * Update:     <br>
 */
public class SettingBarView extends RelativeLayout {
    private ImageView imgLeftIcon;
    private TextView txtSetTitle;
    private EditText txtSetContent;
    private ImageView imgRightIcon;
    private RelativeLayout layoutSettingBar;
    private OnClickSettingBarViewListener mOnClickSettingBarViewListener;
    private OnClickRightImgListener mOnClickRightImgListener;
    private boolean isEdit = false;//是否需要编辑

    public interface OnClickSettingBarViewListener {
        void onClick();
    }

    public interface OnClickRightImgListener {
        void onClick();
    }

    public void setOnClickRightImgListener(OnClickRightImgListener onClickRightImgListener) {
        mOnClickRightImgListener = onClickRightImgListener;
    }

    public void setOnClickSettingBarViewListener(OnClickSettingBarViewListener onClickSettingBarViewListener) {
        mOnClickSettingBarViewListener = onClickSettingBarViewListener;
    }

    public SettingBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.view_setting_bar, this);
        layoutSettingBar = findViewById(R.id.layout_setting_bar);
        imgLeftIcon = findViewById(R.id.img_left_icon);
        txtSetTitle = findViewById(R.id.txt_set_title);
        txtSetContent = findViewById(R.id.txt_set_content);
        imgRightIcon = findViewById(R.id.img_right_icon);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingBarView);
        boolean isVisableLeftIcon = typedArray.getBoolean(R.styleable.SettingBarView_set_left_icon_visable, false);
        boolean isVisableRightIcon = typedArray.getBoolean(R.styleable.SettingBarView_set_right_icon_visable, false);
        String title = typedArray.getString(R.styleable.SettingBarView_set_title);
        String hint = typedArray.getString(R.styleable.SettingBarView_set_content_hint);
        String content = typedArray.getString(R.styleable.SettingBarView_set_content);
        int rightIcon = typedArray.getResourceId(R.styleable.SettingBarView_set_right_icon, 0);
        int leftIcon = typedArray.getResourceId(R.styleable.SettingBarView_set_left_icon, 0);

        imgLeftIcon.setVisibility(isVisableLeftIcon ? View.VISIBLE : View.GONE);
        txtSetTitle.setText(title);
        txtSetContent.setHint(hint);
        txtSetContent.setText(content);
        imgRightIcon.setVisibility(isVisableRightIcon ? View.VISIBLE : View.GONE);
        if (leftIcon > 0) {
            imgLeftIcon.setImageResource(leftIcon);
        }
        if (rightIcon > 0) {
            imgRightIcon.setImageResource(rightIcon);
        }
        imgRightIcon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOnClickRightImgListener != null) {
                    mOnClickRightImgListener.onClick();
                }
            }
        });
        layoutSettingBar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOnClickSettingBarViewListener != null) {
                    mOnClickSettingBarViewListener.onClick();
                }
            }
        });
    }

    public void setContent(String value) {
        if (txtSetContent != null && !TextUtils.isEmpty(value)) {
            txtSetContent.setText(value);
        }
    }

    public String getContent() {
        if (txtSetContent != null) {
            return txtSetContent.getText().toString();
        }
        return null;
    }

    public void enableEditContext(boolean b) {
        isEdit = b;
        if (txtSetContent != null) {
            txtSetContent.setEnabled(b);
        }
    }

    public void showImgLeftIcon(boolean show) {
        if (imgLeftIcon != null) {
            imgLeftIcon.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return !isEdit;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return layoutSettingBar.onTouchEvent(event);
    }

}