此方法基于PopupWindow,适合需要回复内容时响应点击事件,打开软键盘,编辑框在软键盘上部。
优点,编辑框使用CleanEdittext,监听输入状态来更改回复按钮颜色,添加Title等。
先展示效果
点击评论打开软键盘,编辑框在软键盘上部,点击其他区域消失收起软键盘:
1.BaseSelectPopupWindow 的代码。
public class BaseSelectPopupWindow extends PopupWindow {private VIEw popVIEw;private VIEw vIEw;private OnheadClickListener onheadClickListener;private TextVIEw tv_head;protected Context context;private boolean isOpenKeyboard=false;;private boolean isShowTitle=true;private boolean isOkClose=true;protected int maxTextSize = 24;protected int minTextSize = 14;public BaseSelectPopupWindow(Context context,int layoutID ) { this.context=context; LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); popVIEw = inflater.inflate(R.layout.pop_vIEw,null); tv_head=(TextVIEw) popVIEw.findVIEwByID(R.ID.tv_head); linearLayout contentVIEw = (linearLayout) popVIEw .findVIEwByID(R.ID.content); vIEw=inflater.inflate(layoutID,null); contentVIEw.addVIEw(vIEw,contentVIEw.getLayoutParams()); // btn_take_photo.setonClickListener(itemsOnClick); // 设置SelectPicPopupWindow的VIEw this.setContentVIEw(popVIEw); // 设置SelectPicPopupWindowd出窗体的宽 this.setWIDth(LayoutParams.FILL_PARENT); // 设置SelectPicPopupWindowd出窗体的高 WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE);// this.setHeight(wm.getDefaultdisplay().getHeight() / 2); this.setHeight(LayoutParams.WRAP_CONTENT); // 设置SelectPicPopupWindowd出窗体可点击 this.setFocusable(true); // 设置SelectPicPopupWindowd出窗体动画效果 this.setAnimationStyle(R.style.AnimBottom); // 实例化一个colorDrawable颜色为半透明 colorDrawable DW = new colorDrawable(0xb0000000); // 设置SelectPicPopupWindowd出窗体的背景 this.setBackgroundDrawable(DW); // mMenuVIEw添加OntouchListener监听判断获取触屏位置如果在选择框外面则销毁d出框 /* * popVIEw.setontouchListener(new OntouchListener() { * * public boolean ontouch(VIEw v,MotionEvent event) { * * int height = popVIEw.findVIEwByID(R.ID.pop_layout).gettop(); int * y=(int) event.getY(); if(event.getAction()==MotionEvent.ACTION_UP){ * if(y<height){ dismiss(); } } return true; } }); */ (popVIEw.findVIEwByID(R.ID.btn_back)).setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { dismiss(); } }); (popVIEw.findVIEwByID(R.ID.btn_right)).setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if(onheadClickListener!=null){ onheadClickListener.okListener(); } if(isOkClose){ dismiss(); } } }); if(isOpenKeyboard){ openKeyboard(); }}public boolean isShowTitle() { return isShowTitle;}public voID setShowTitle(boolean isShowTitle) { this.isShowTitle = isShowTitle; if(!isShowTitle){ ((relativeLayout)tv_head.getParent()).setVisibility(VIEw.GONE); }}/** * 打开软键盘 */private 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); } },1000);}public boolean isOpenKeyboard() { return isOpenKeyboard;}public voID setopenKeyboard(boolean isOpenKeyboard) { this.isOpenKeyboard = isOpenKeyboard;}public OnheadClickListener getonheadClickListener() { return onheadClickListener;}public voID setonheadClickListener(OnheadClickListener onheadClickListener) { this.onheadClickListener = onheadClickListener;}public interface OnheadClickListener{ public voID okListener();}public voID setTitle(String value){ if(tv_head!=null){ tv_head.setText(value); }}public boolean isOkClose() { return isOkClose;}public voID setokClose(boolean isOkClose) { this.isOkClose = isOkClose;}public Context getContext() { return context;}
对应的XML 布局:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:background="@color/white_gray"androID:gravity="center_vertical" ><relativeLayout androID:ID="@+ID/head" androID:layout_wIDth="match_parent" androID:layout_height="44dp" androID:layout_alignParenttop="true" androID:background="@color/head_yellow" > <TextVIEw androID:ID="@+ID/btn_back" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_centerVertical="true" androID:layout_marginleft="15dp" androID:text="@string/cancel" androID:textcolor="@color/black_light_color" androID:textSize="@dimen/TitleTextSize" /> <TextVIEw androID:ID="@+ID/tv_head" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:ellipsize="end" androID:gravity="center" androID:visibility="gone" /> <TextVIEw androID:ID="@+ID/btn_right" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:layout_marginRight="15dp" androID:text="@string/sure" androID:textcolor="@color/black" androID:textSize="@dimen/TitleTextSize" /></relativeLayout><linearLayout androID:ID="@+ID/content" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@ID/head" androID:gravity="center" androID:orIEntation="vertical" > </linearLayout></relativeLayout>
其中style:nav_sIDe_Title_text是定义文字大小的。
2.需要使用的时候
private BaseSelectPopupWindow popWiw;// 回复的 编辑框
声明之后使用的时候初始化并调用:
private voID popWiw( ) { popWiw = new BaseSelectPopupWindow(context,R.layout.edit_data); // popWiw.setopenKeyboard(true); popWiw.setinputMethodMode(PopupWindow.input_METHOD_NEEDED); popWiw.setFocusable(true); popWiw.setSoftinputMode(WindowManager.LayoutParams.soFT_input_ADJUST_RESIZE); popWiw.setShowTitle(false); inputMethodManager im = (inputMethodManager) context .getSystemService(Context.input_METHOD_SERVICE); im.toggleSoftinput(0,inputMethodManager.HIDE_NOT_ALWAYS); final button send = (button) popWiw.getContentVIEw().findVIEwByID( R.ID.btn_send); final EditText edt = (EditText) popWiw.getContentVIEw().findVIEwByID( R.ID.edt_content); edt.setinputType(EditorInfo.TYPE_CLASS_TEXT); edt.setImeOptions(EditorInfo.IME_ACTION_SEND); edt.addTextChangedListener(new TextWatcher() { @OverrIDe public voID onTextChanged(CharSequence s,int start,int before,int count) { if (TextUtils.isEmpty(edt.getText())) { send.setEnabled(false); } else { send.setEnabled(true); } } @OverrIDe public voID beforeTextChanged(CharSequence s,int count,int after) { } @OverrIDe public voID afterTextChanged(Editable s) { // Todo auto-generated method stub } }); edt.setonEditorActionListener(new TextVIEw.OnEditorActionListener() { @OverrIDe public boolean onEditorAction(TextVIEw v,int actionID,KeyEvent event) { if (actionID == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { if (!TextUtils.isEmpty(edt.getText().toString().trim())) { String content = edt.getText().toString().trim(); // /提交内容 sumbit(content) popWiw.dismiss(); } return true; } return false; } }); send.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (!TextUtils.isEmpty(edt.getText().toString().trim())) { // /提交内容 String content = edt.getText().toString().trim(); popWiw.dismiss(); } } }); popWiw.setTitle("回复" + nickname); popWiw.showAtLocation(refreshLayout,Gravity.BottOM | Gravity.CENTER_HORIZONTAL,0);}
对应的edit_data 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="55dp" ><relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="44dp" androID:background="@drawable/bg_search" androID:gravity="clip_vertical" androID:orIEntation="vertical" androID:paddingleft="12dp" androID:paddingRight="12dp" > <com.myapp.vIEw.ClearEditText androID:ID="@+ID/edt_content" androID:layout_wIDth="match_parent" androID:layout_height="55dp" androID:layout_alignParentleft="true" androID:layout_centerVertical="true" androID:layout_marginBottom="5dp" androID:layout_marginRight="65dp" androID:layout_margintop="5dp" androID:background="@drawable/edittext_back" androID:focusable="true" androID:hint="说点什么..." androID:paddingleft="10dp" androID:imeOptions="actionSend" androID:paddingRight="10dp" androID:textcolor="@color/top_bg_shadow" androID:textSize="@dimen/normalTextSize" > </com.myapp.vIEw.ClearEditText> <button androID:ID="@+ID/btn_send" androID:layout_wIDth="60dp" androID:layout_height="34dp" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:background="@drawable/btn_send_rounded" androID:enabled="false" androID:paddingleft="12dp" androID:paddingRight="12dp" androID:text="@string/send" androID:textcolor="@color/white" androID:textSize="@dimen/normalTextSize" /></relativeLayout></linearLayout>
对于ClearEditText,应该都不陌生,
public class ClearEditText extends EditText implements OnFocuschangelistener,TextWatcher { /** * 删除按钮的引用 */private Drawable mClearDrawable; public ClearEditText(Context context) { this(context,null); } public ClearEditText(Context context,AttributeSet attrs) { //这里构造方法也很重要,不加这个很多属性不能再XML里面定义 this(context,attrs,androID.R.attr.editTextStyle); } public ClearEditText(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle); init();}private voID init() { //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { mClearDrawable = getResources() .getDrawable(R.drawable.icon_edit_del); } mClearDrawable.setBounds(0,mClearDrawable.getIntrinsicWIDth(),mClearDrawable.getIntrinsicHeight()); setClearIconVisible(false); setonFocuschangelistener(this); addTextChangedListener(this); } /** * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 * 当我们按下的位置 在 EditText的宽度 - 图标到控件右边的间距 - 图标的宽度 和 * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑 */@OverrIDe public boolean ontouchEvent(MotionEvent event) { if (getCompoundDrawables()[2] != null) { if (event.getAction() == MotionEvent.ACTION_UP) { boolean touchable = event.getX() > (getWIDth() - getpaddingRight() - mClearDrawable.getIntrinsicWIDth()) && (event.getX() < ((getWIDth() - getpaddingRight()))); if (touchable) { this.setText(""); } } } return super.ontouchEvent(event); } /** * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏 */@OverrIDe public voID onFocusChange(VIEw v,boolean hasFocus) { if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } /** * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去 * @param visible */protected voID setClearIconVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],right,getCompoundDrawables()[3]); } /** * 当输入框里面内容发生变化的时候回调的方法 */@OverrIDe public voID onTextChanged(CharSequence s,int after) { setClearIconVisible(s.length() > 0); } @OverrIDe public voID beforeTextChanged(CharSequence s,int after) { } @OverrIDe public voID afterTextChanged(Editable s) { } /** * 设置晃动动画 */public voID setShakeAnimation(){ this.setAnimation(shakeAnimation(5));}/** * 晃动动画 * @param counts 1秒钟晃动多少下 * @return */public static Animation shakeAnimation(int counts){ Animation translateAnimation = new TranslateAnimation(0,10,0); translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(1000); return translateAnimation;}}
一些icon 及圆角背景就不展示了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android View 完美实现EditText 在软键盘上边的示例全部内容,希望文章能够帮你解决Android View 完美实现EditText 在软键盘上边的示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)