AndroidView完美实现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 ) {
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(t);
view=inflater.inflate(layoutId, null);
contentView.addView(LayoutParams());
// 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.DefaultDisplay().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.setOnTouchListener(new OnTouchListener() {
*
* public boolean onTouch(View v, MotionEvent event) {
*
* int height = popView.findViewById(R.id.pop_layout).getTop(); int
* y=(int) Y(); Action()==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)Parent()).setVisibility(View.GONE);
}
}
/**
* 打开软键盘
*/
private void openKeyboard() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) SystemService(Context.INPUT_METHOD_SERVICE); leSoftInput(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) {
}
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="schemas.android/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);
final Button send = (Button) ContentView().findViewById(
R.id.btn_send);
final EditText edt = (EditText) ContentView().findViewById(
R.id.edt_content);
edt.setInputType(EditorInfo.TYPE_CLASS_TEXT);
edt.setImeOptions(EditorInfo.IME_ACTION_SEND);
edt.addTextChangedListener(new TextWatcher() {
@Override
android layout布局public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (TextUtils.Text())) {
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 && KeyCode() == KeyEvent.KEYCODE_ENTER)) { if (!TextUtils.Text().toString().trim())) {
String content = Text().toString().trim();
// /提交内容 sumbit(content)
popWiw.dismiss();
}
return true;
}
return false;
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!TextUtils.Text().toString().trim())) {
// /提交内容
String content = Text().toString().trim();
popWiw.dismiss();
}
}
});
popWiw.setTitle("回复" + nickname);
popWiw.showAtLocation(refreshLayout, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL, 0, 0);
}
对应的edit_data xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/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"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论