栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android View 完美实现EditText 在软键盘上边的示例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android View 完美实现EditText 在软键盘上边的示例

此方法基于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);
  // 设置SelectPicPopupWindow弹出窗体的宽
  this.setWidth(LayoutParams.FILL_PARENT);
  // 设置SelectPicPopupWindow弹出窗体的高

  WindowManager wm = (WindowManager) context
      .getSystemService(Context.WINDOW_SERVICE);
//  this.setHeight(wm.getDefaultDisplay().getHeight() / 2);
  this.setHeight(LayoutParams.WRAP_CONTENT);
  // 设置SelectPicPopupWindow弹出窗体可点击
  this.setFocusable(true);
  // 设置SelectPicPopupWindow弹出窗体动画效果
  this.setAnimationStyle(R.style.AnimBottom);
  // 实例化一个ColorDrawable颜色为半透明
  ColorDrawable dw = new ColorDrawable(0xb0000000);
  // 设置SelectPicPopupWindow弹出窗体的背景
  this.setBackgroundDrawable(dw);
  // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框

  

  (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 布局:






  

  

  



 



其中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 start, 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, 0);
}

对应的edit_data xml布局






  
  

  

对于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, attrs, defStyle);
  init();
}


private void init() { 
  //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
  mClearDrawable = getCompoundDrawables()[2]; 
  if (mClearDrawable == null) { 
    mClearDrawable = getResources() 
 .getDrawable(R.drawable.icon_edit_del); 
  } 
  mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); 
  setClearIconVisible(false); 
  setonFocusChangeListener(this); 
  addTextChangedListener(this); 
} 


@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); 
} 


@Override 
public void onFocusChange(View v, boolean hasFocus) { 
  if (hasFocus) { 
    setClearIconVisible(getText().length() > 0); 
  } else { 
    setClearIconVisible(false); 
  } 
} 



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 start, int count, 
    int after) { 
  setClearIconVisible(s.length() > 0); 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count, 
    int after) { 

} 

@Override 
public void afterTextChanged(Editable s) { 

} 



public void setShakeAnimation(){
  this.setAnimation(shakeAnimation(5));
}



public static Animation shakeAnimation(int counts){
  Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
  translateAnimation.setInterpolator(new CycleInterpolator(counts));
  translateAnimation.setDuration(1000);
  return translateAnimation;
}
}

一些icon 及圆角背景就不展示了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号