自定义对话框

一,继承自Dialog
在构造方法中调用initView方法,InitView是初始化布局,setContentView,各种空间的初始化,然后是setCanceledOnTouchOutside,setCancelable等
``
public class DoubleButtonDialog extends Dialog implements OnClickListener {
private TextView mPromptTitleTV;//对话框标题
private TextView mPromptContentTV;
private TextView mLeftButton , mRightButton;
private TextView mInputDesTV;//输入框描述组件
private EditText mInputET;//输入框
private ViewGroup mInputLayout;//输入框布局
protected OnButtonClickListener mClickListener;
private CharSequence mPromptContent = "提示内容";
private CharSequence mPromptTitle = "提示";
//输入框内容为空时的提示语,如果未设置该变量,则不进行非空判断
private CharSequence mInputEmptyHint;
public DoubleButtonDialog(Context context , CharSequence title , CharSequence PromptContent) {
this(context);
mPromptTitle = title;
mPromptContent = PromptContent;
mPromptContentTV.setText(mPromptContent);
mPromptTitleTV.setText(mPromptTitle);
}
public DoubleButtonDialog(Context context , CharSequence PromptContent) {
this(context);
mPromptContentTV.setText(PromptContent);
}

public DoubleButtonDialog(Context context) {
    super(context, R.style.dialogUpload);
    initView(context);
}
private void initView(Context context) {
    View mLayoutView = LayoutInflater.from(context).inflate(R.layout.dialog_double_button_layout, null);
    setContentView(mLayoutView);
    mPromptContentTV = (TextView) mLayoutView.findViewById(R.id.dialog_double_button_message);
    mPromptTitleTV = (TextView) mLayoutView.findViewById(R.id.dialog_double_button_title);
    mLeftButton = (TextView) mLayoutView.findViewById(R.id.dialog_double_button_cancel);
    mRightButton = (TextView) mLayoutView.findViewById(R.id.dialog_double_button_determine);
    mInputDesTV = (TextView) mLayoutView.findViewById(R.id.dialog_double_button_inputDescTV);
    mInputET = (EditText) mLayoutView.findViewById(R.id.dialog_double_button_inputET);
    mInputLayout = (ViewGroup) mLayoutView.findViewById(R.id.dialog_double_button_inputLayout);
    mPromptTitle = context.getResources().getString(R.string.prompt);

    mPromptTitleTV.setText(mPromptTitle);
    mPromptContentTV.setText(mPromptContent);
    setCanceledOnTouchOutside(false);
    setCancelable(false);
    mLeftButton.setOnClickListener(this);
    mRightButton.setOnClickListener(this);
}
public void showInputLayout(){
    mInputLayout.setVisibility(View.VISIBLE);
    mPromptContentTV.setVisibility(View.GONE);
}

public CharSequence getInputMsg(){
    return mInputET.getText();
}

public void setLeftButton(CharSequence text) {
    mLeftButton.setText(text);
}

public void setRightButton(CharSequence text) {
    mRightButton.setText(text);
}
/**
 * 获取提示框标题
 * @return
 */
public CharSequence getPromptTitle() {
    return mPromptTitle;
}

/**
 * 隐藏标题栏名称布局
 */
public void hidePromptTitle(){
    mPromptTitleTV.setVisibility(View.GONE);
}

/**
 * 隐藏对话框左侧按钮
 */
public void hideLeftBtn(){
    mLeftButton.setVisibility(View.GONE);
}

/**
 * 设置提示框标题
 * @return
 */
public void setPromptTitle(CharSequence title) {
    this.mPromptTitle = title;
}
public CharSequence getPromptContent() {
    return mPromptContent;
}

public void setPromptContent(CharSequence mPromptContent) {
    this.mPromptContent = mPromptContent;
    mPromptContentTV.setText(mPromptContent);
}
public void setInputEmptyHint(CharSequence charSequence){
    mInputEmptyHint = charSequence;
}
public void setMaxEms(int maxems){
    mInputET.setMaxEms(maxems);
}

/**
 * 设置输入框背景提示
 * @param hint
 */
public void setEditHint(String hint){
    mInputET.setHint(hint);
}
public void setLengthFilter(int max){
    mInputET.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
}
public void setOnButtonClickListener(OnButtonClickListener listener) {
    mClickListener = listener;
}
public interface OnButtonClickListener{
    void onLeftButtonClick(View view);
    void onRightButtonClick(View view);
}
@Override
public void onClick(View v) {
    if (mClickListener == null) {
        dismiss();
        return;
    }
    switch (v.getId()) {
        case R.id.dialog_double_button_cancel://左侧按钮
            mClickListener.onLeftButtonClick(v);
            break;
        case R.id.dialog_double_button_determine://右侧按钮
            if (!TextUtils.isEmpty(mInputEmptyHint) && TextUtils.isEmpty(mInputET.getText())){
                ToastManager.showShort(getContext(), mInputEmptyHint);
                return;
            }
            mClickListener.onRightButtonClick(v);
            break;
        default:
            break;
    }
    dismiss();
}

}
``

你可能感兴趣的:(自定义对话框)