Android View 完美實(shí)現(xiàn)EditText 在軟鍵盤(pán)上邊的示例
此方法基于PopupWindow,適合需要回復(fù)內(nèi)容時(shí)響應(yīng)點(diǎn)擊事件,打開(kāi)軟鍵盤(pán),編輯框在軟鍵盤(pán)上部。
優(yōu)點(diǎn),編輯框使用CleanEdittext,監(jiān)聽(tīng)輸入狀態(tài)來(lái)更改回復(fù)按鈕顏色,添加title等。
先展示效果
點(diǎn)擊評(píng)論打開(kāi)軟鍵盤(pán),編輯框在軟鍵盤(pán)上部,點(diǎn)擊其他區(qū)域消失收起軟鍵盤(pán):


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);
// 設(shè)置SelectPicPopupWindow的View
this.setContentView(popView);
// 設(shè)置SelectPicPopupWindow彈出窗體的寬
this.setWidth(LayoutParams.FILL_PARENT);
// 設(shè)置SelectPicPopupWindow彈出窗體的高
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
// this.setHeight(wm.getDefaultDisplay().getHeight() / 2);
this.setHeight(LayoutParams.WRAP_CONTENT);
// 設(shè)置SelectPicPopupWindow彈出窗體可點(diǎn)擊
this.setFocusable(true);
// 設(shè)置SelectPicPopupWindow彈出窗體動(dòng)畫(huà)效果
this.setAnimationStyle(R.style.AnimBottom);
// 實(shí)例化一個(gè)ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
// 設(shè)置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
// mMenuView添加OnTouchListener監(jiān)聽(tīng)判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
/*
* 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);
}
}
/**
* 打開(kāi)軟鍵盤(pán)
*/
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;
}
對(duì)應(yīng)的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"
style="@style/nav_side_title_text"
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"
style="@style/nav_head_title_text"
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"
style="@style/nav_side_title_text"
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.需要使用的時(shí)候
private BaseSelectPopupWindow popWiw;// 回復(fù)的 編輯框
聲明之后使用的時(shí)候初始化并調(diào)用:
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();
// /提交內(nèi)容 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())) {
// /提交內(nèi)容
String content = edt.getText().toString().trim();
popWiw.dismiss();
}
}
});
popWiw.setTitle("回復(fù)" + nickname);
popWiw.showAtLocation(refreshLayout, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL, 0, 0);
}
對(duì)應(yīng)的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="說(shuō)點(diǎn)什么..."
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>
對(duì)于ClearEditText,應(yīng)該都不陌生,
public class ClearEditText extends EditText implements
OnFocusChangeListener, TextWatcher {
/**
* 刪除按鈕的引用
*/
private Drawable mClearDrawable;
public ClearEditText(Context context) {
this(context, null);
}
public ClearEditText(Context context, AttributeSet attrs) {
//這里構(gòu)造方法也很重要,不加這個(gè)很多屬性不能再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,假如沒(méi)有設(shè)置我們就使用默認(rèn)的圖片
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);
}
/**
* 因?yàn)槲覀儾荒苤苯咏oEditText設(shè)置點(diǎn)擊事件,所以我們用記住我們按下的位置來(lái)模擬點(diǎn)擊事件
* 當(dāng)我們按下的位置 在 EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和
* EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點(diǎn)擊了圖標(biāo),豎直方向沒(méi)有考慮
*/
@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);
}
/**
* 當(dāng)ClearEditText焦點(diǎn)發(fā)生變化的時(shí)候,判斷里面字符串長(zhǎng)度設(shè)置清除圖標(biāo)的顯示與隱藏
*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
}
/**
* 設(shè)置清除圖標(biāo)的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去
* @param visible
*/
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
/**
* 當(dāng)輸入框里面內(nèi)容發(fā)生變化的時(shí)候回調(diào)的方法
*/
@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) {
}
/**
* 設(shè)置晃動(dòng)動(dòng)畫(huà)
*/
public void setShakeAnimation(){
this.setAnimation(shakeAnimation(5));
}
/**
* 晃動(dòng)動(dòng)畫(huà)
* @param counts 1秒鐘晃動(dòng)多少下
* @return
*/
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 及圓角背景就不展示了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)例講解Android中的View類以及自定義View控件的方法
這篇文章主要介紹了Android中的View類以及自定義View控件的方法,講解了如何繼承View類并且展示了一個(gè)對(duì)View進(jìn)行重繪的例子,需要的朋友可以參考下2016-04-04
Android Messenger實(shí)現(xiàn)進(jìn)程間雙向通信
這篇文章主要為大家詳細(xì)介紹了Messenger實(shí)現(xiàn)進(jìn)程間雙向通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android檢測(cè)Activity或者Service是否運(yùn)行的方法
下面小編就為大家分享一篇Android檢測(cè)Activity或者Service是否運(yùn)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
快速調(diào)試Android應(yīng)用系統(tǒng)修改ro.debuggable屬性的兩種方式
這篇文章主要為大家介紹了快速調(diào)試Android應(yīng)用系統(tǒng)修改ro.debuggable屬性的兩種方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Android開(kāi)發(fā)之ToggleButton實(shí)現(xiàn)開(kāi)關(guān)效果示例
這篇文章主要介紹了Android開(kāi)發(fā)之ToggleButton實(shí)現(xiàn)開(kāi)關(guān)效果的方法,結(jié)合實(shí)例形式分析了ToggleButton控件實(shí)現(xiàn)開(kāi)關(guān)效果的布局與功能相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android Studio 中的Gradle構(gòu)建系統(tǒng)示例
這篇文章主要介紹了Android Studio 中的Gradle構(gòu)建系統(tǒng)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
ERROR/AndroidRuntime(17121)的問(wèn)題解決
ERROR/AndroidRuntime(17121)的問(wèn)題解決,需要的朋友可以參考一下2013-05-05
Android簡(jiǎn)單音樂(lè)播放實(shí)例
這篇文章主要介紹了Android簡(jiǎn)單音樂(lè)播放實(shí)例,詳細(xì)介紹了Android Service使用方法2015-12-12
Android HorizontalScrollView滑動(dòng)與ViewPager切換案例詳解
這篇文章主要介紹了Android HorizontalScrollView滑動(dòng)與ViewPager切換案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

