自定义的弹窗,带文本编辑,按需更改 下面是封装好的抽象类 package b2a.main.utils; import android.content.Context; import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.Selection; import android.text.TextWatcher; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import b2a.main.R; /** * Created by pc-004 on 2015/10/19. */ public abstract class DialogMessage { public void dialogShowDiy(Context context, LinearLayout viewGroup, String leftTitle, String rightTitle, String leftBtn, String rightBtn, String eText) { final AlertDialog dialog = new AlertDialog.Builder(context).create(); dialog.setView(viewGroup); TextView txtvTitleLeft = (TextView) viewGroup.findViewById(R.id.txtv_tile_left); final TextView txtvTitleRight = (TextView) viewGroup.findViewById(R.id.txtv_title_right); TextView txtvBtnLeft = (TextView) viewGroup.findViewById(R.id.txtv_left_btn); TextView txtvBtnRight = (TextView) viewGroup.findViewById(R.id.txtv_right_btn); final EditText editText = (EditText) viewGroup.findViewById(R.id.etxt_area); editText.setText(eText); txtvTitleLeft.setText(leftTitle); txtvTitleRight.setText(rightTitle); txtvBtnLeft.setText(leftBtn); txtvBtnRight.setText(rightBtn); txtvBtnLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { leftBtnClick(editText); dialog.cancel(); } }); txtvBtnRight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rightBtnClick(editText); dialog.cancel(); } }); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { txtvTitleRight.setText(editText.length() + "/" + "300字"); Editable editable = editText.getText(); int len = editable.length(); if (len > 300) { int selEndIndex = Selection.getSelectionEnd(editable); String str = editable.toString(); //截取新字符串 String newStr = str.substring(0, 300); editText.setText(newStr); editable = editText.getText(); //新字符串的长度 int newLen = editable.length(); //旧光标位置超过字符串长度 if (selEndIndex > newLen) { selEndIndex = editable.length(); } //设置新光标所在的位置 Selection.setSelection(editable, selEndIndex); } } @Override public void afterTextChanged(Editable s) { } }); dialog.show(); } /** * 自定义dialog */ public abstract void leftBtnClick(EditText editText); public abstract void rightBtnClick(EditText editText); } 在activity或者是fragment中使用时,要实现这个抽象类DialogMessage dialogMessage = new DialogMessage() { @Override public void leftBtnClick(EditText editText) { } @Override public void rightBtnClick(EditText editText) { } }; dialogMessage.dialogShowDiy(context, (LinearLayout) View.inflate(context, R.layout.dialog_input_area, null), input, "", context.getResources().getString(R.string.cancle), context.getResources().getString(R.string.save), etxt);
封装的自定义dialog,文本编辑,EditText,按需更改
最新推荐文章于 2021-05-26 13:31:05 发布