import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.InputFilter; import android.text.InputType; import android.text.TextUtils; import android.text.TextWatcher; import android.text.method.DigitsKeyListener; import android.util.Log; import android.view.Display; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import com.hq.fxh.CustomerView.TestEdit.Mydialgo; import com.hq.fxh.R; import com.hq.fxh.Utils.AppConstant; import com.hq.fxh.Utils.LogUtils; import com.hq.fxh.Utils.Tool; import com.hq.fxh.imageLoader.FolderPictureActivity; import java.util.Timer; import java.util.TimerTask; import static com.hq.fxh.R.id.timer; /** * Created by Administrator on 2017/2/16. */ public class PopupForChangeInfo extends AlertDialog implements View.OnClickListener { protected View rootView; protected TextView popupchangeTitle; protected EditText popupcommentEditContent; protected Button popupchangeQuXiao; protected Button popupcomentSent; private Activity context; protected ChangeCallBack commentCallBack; public PopupForChangeInfo(Activity context, ChangeCallBack commentCallBack) { super(context); this.context = context; this.commentCallBack = commentCallBack; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.popup_write_changeinfo); popupchangeTitle = (TextView) findViewById(R.id.popupchange_title); popupcommentEditContent = (EditText) findViewById(R.id.popupchange_edit_content); popupchangeQuXiao = (Button) findViewById(R.id.popupchange_QuXiao); popupchangeQuXiao.setOnClickListener(PopupForChangeInfo.this); popupcomentSent = (Button) findViewById(R.id.popupchange_sent); popupcomentSent.setOnClickListener(PopupForChangeInfo.this); initData(); } public void setMaxNum(int maxnum) { //设置输入框的最大字数 popupcommentEditContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxnum)}); //最大输入长度 } public void setContent(String content) { // 设置输入框已经拥有的内容 if (content != null && !content.equals("")) { popupcommentEditContent.setText(content); popupcommentEditContent.setSelection(popupcommentEditContent.getText().length()); } else { popupcommentEditContent.setText(""); popupcommentEditContent.setSelection(0); } } public void openKeyboard() { //打开软键盘 Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } }, 300); } int isNum = 0; public void setPhoneNum(int isPhone) { //设置输入框输入的类型 isNum = isPhone; if (isPhone == AppConstant.isFloat) { popupcommentEditContent.setInputType(InputType.TYPE_CLASS_PHONE); popupcommentEditContent.setKeyListener(DigitsKeyListener.getInstance("1234567890.")); } else if (isPhone == AppConstant.isString) { popupcommentEditContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); popupcommentEditContent.setHorizontallyScrolling(false); //改变默认的单行模式 popupcommentEditContent.setSingleLine(false); } else if (isPhone == AppConstant.isInt) { popupcommentEditContent.setInputType(InputType.TYPE_CLASS_PHONE); popupcommentEditContent.setKeyListener(DigitsKeyListener.getInstance("1234567890")); } } public void showEdit() { //显示弹窗 show(); //设置弹窗在底部 Window window = getWindow(); window.setGravity(Gravity.BOTTOM); window.setBackgroundDrawableResource(android.R.color.transparent); WindowManager m = context.getWindowManager(); Display d = m.getDefaultDisplay(); //为获取屏幕宽、高 WindowManager.LayoutParams p = getWindow().getAttributes(); //获取对话框当前的参数值 p.width = d.getWidth(); //宽度设置为屏幕 getWindow().setAttributes(p); //设置生效 } public void initData() { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); popupcommentEditContent.setFocusable(true); popupcommentEditContent.setFocusableInTouchMode(true); popupcommentEditContent.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) { } @Override public void afterTextChanged(Editable s) { if (!Tool.editIsEmpty(popupcommentEditContent)) { popupcomentSent.setBackgroundResource(R.drawable.style_redbtn_r4); popupcomentSent.setClickable(true); } else { popupcomentSent.setBackgroundResource(R.drawable.style_greybtn_r4); popupcomentSent.setClickable(false); } } }); } @Override public void onClick(View view) { if (view.getId() == R.id.popupcoment_sent) { } else if (view.getId() == R.id.popupchange_QuXiao) { dismiss(); setTitle(""); } else if (view.getId() == R.id.popupchange_sent) { String text = popupcommentEditContent.getText().toString(); Log.e("text", text); boolean empty = TextUtils.isEmpty(popupcommentEditContent.getText()); if (!empty) { if (isNum == AppConstant.isFloat) { float floatFromString = Tool.getPopFloatFromString(text); if (floatFromString == -1) { LogUtils.startToash(context, "请输入合法的数字"); return; } else { commentCallBack.call(text, floatFromString, (int) floatFromString); } } else if (isNum == AppConstant.isString) { commentCallBack.call(text, 0, 0); } else if (isNum == AppConstant.isInt) { commentCallBack.call(text, 0, 0); } } dismiss(); setTitle(""); } } public void setTitle(String title) { //设置输入框标题 popupchangeTitle.setText(title); } public interface ChangeCallBack { void call(String comment, float num, int intNum); } } xml文件:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/greybackground" android:fitsSystemWindows="true" android:orientation="vertical"> <TextView android:id="@+id/popupchange_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="15dp" android:text="请输入" android:textColor="@color/textblack" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="15dp" android:paddingLeft="10dp" android:paddingRight="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/style_edit"> <EditText android:id="@+id/popupchange_edit_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:gravity="top" android:hint="请输入内容" android:minHeight="60dp" android:padding="3dp" android:textSize="13sp" /> </LinearLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:gravity="right"> <Button android:id="@+id/popupchange_QuXiao" android:layout_width="100dp" android:layout_height="30dp" android:layout_marginRight="10dp" android:background="@drawable/style_greybtn_r4" android:gravity="center" android:text="取消" android:textColor="@color/white" android:textSize="14sp" /> <Button android:id="@+id/popupchange_sent" android:layout_width="100dp" android:layout_height="30dp" android:layout_marginRight="10dp" android:background="@drawable/style_greybtn_r4" android:clickable="false" android:gravity="center" android:text="确认" android:textColor="@color/white" android:textSize="14sp" /> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout>
资源文件style_greybtn_r4
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 圆角 --> <corners android:radius="5dp" /><!-- 设置圆角半径 --> <!-- 间隔 --> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /><!-- 各方向的间隔 --> <!-- 填充 --> <solid android:color="@color/grey" /><!-- 填充的颜色 --> </shape>style_redbtn_r4
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 圆角 --> <corners android:radius="5dp" /><!-- 设置圆角半径 --> <!-- 间隔 --> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /><!-- 各方向的间隔 --> <!-- 填充 --> <solid android:color="@color/red" /><!-- 填充的颜色 --> </shape>style_edit
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 圆角 --> <corners android:radius="3dp" /><!-- 设置圆角半径 --> <!-- 间隔 --> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /><!-- 各方向的间隔 --> <!-- 大小 --> <!-- 填充 --> <solid android:color="#ffffff" /> <!-- 描边 --> <stroke android:width="1px" android:color="@color/greyborder" /> </shape>
使用
封装使用方法public class EditUtils { Activity context; private PopupForChangeInfo popup; public EditUtils(Activity activity) { context = activity; initEdit(); } EditCallBack callBack; public void showEdit(View view, String title, int isPhone, String content, int maxnum, EditCallBack callBack) { this.callBack = callBack; popup.showEdit(); popup.setTitle(title); popup.setContent(content); popup.setPhoneNum(isPhone); popup.setMaxNum(maxnum); popup.openKeyboard(); } public void initEdit() { if (popup == null) { popup = new PopupForChangeInfo(context, new PopupForChangeInfo.ChangeCallBack() { @Override public void call(String comment, float num, int intNum) { if (comment == null || comment.equals("")) { LogUtils.startToash(context, "填写的信息为空"); } else { callBack.Call(comment, intNum, num); } } }); } } public interface EditCallBack { void Call(String comment, int intcomment, float Fcomment); } } 使用:EditUtils editUtils = new EditUtils(this);editUtils.showEdit(view, "请输姓名", AppConstant.isString, null, 100, new EditUtils.EditCallBack() { @Override public void Call(String comment, int intcomment, float Fcomment) { tvShopXXDZ.setText(comment); shopModel.setShop_addr(comment); } });
带复制粘贴的底部输入框
最新推荐文章于 2021-05-26 12:02:06 发布