最近在做即时通讯类型的app,界面搭建发现的难点:
1.项目需求,软键盘自动将edittext控件所在布局顶起;
2.类似于微信,点击右下角的加号,弹出软键盘并不会将功能布局顶起;
3.监听edittext,当有内容的时候,改变加号图标为发送;
4.监听软键盘的弹出与关闭
解决方案(期望对您有所帮助):
1.需要在清单文件中设置“windowSoftInputMode”属性,其中具体的值不在讲述,但是尝试多次并不起作用,包括在布局中设置scrollview也不起作用,不知道你们是否也遇到这个问题,最后通过不断的尝试,发现这个属性会与标题栏的设置相冲突,如果遇到这个问题,就多在activity设置中找一下有哪些属性会与这个属性相冲突;
2.第二个问题就是通过代码的方式设置“windowSoftInputMode”属性;
3.给edittext设置addTextChangedListener监听;
4.通过给listview外层设置一层父布局,监听左上右下位置的改变(addOnLayoutChangeListener),达到监听软键盘的问题;
废话不多说,上代码
public class FunctionHidePlus extends LinearLayout implements View.OnClickListener {
private View bottom;
private int bottomHeiht;
//是否显示底部功能布局
private boolean isShow = false;
//是否显示ptt bt
private boolean isShowBt = true;
private EditText groupCallNewsEt;
private ImageView hideFunction;
private ImageView groupCallNewsKeyboard;
private Button button;
private Button send;
private LinearLayout videoPostback;
public FunctionHidePlus(Context context) {
this(context, null);
}
public FunctionHidePlus(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public FunctionHidePlus(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOrientation(LinearLayout.VERTICAL);
addView();
}
private void addView() {
View top = View.inflate(getContext(), R.layout.funcation_hide_plus_top, null);
bottom = View.inflate(getContext(), R.layout.funcation_hide_plus_bottom, null);
hideFunction = (ImageView) top.findViewById(R.id.hide_function);
groupCallNewsEt = (EditText) top.findViewById(R.id.group_call_news_et);
groupCallNewsKeyboard = (ImageView) top.findViewById(R.id.group_call_news_keyboard);
button = (Button) top.findViewById(R.id.button);
send = (Button) top.findViewById(R.id.bt_send);
videoPostback = (LinearLayout) bottom.findViewById(R.id.video_postback);
this.addView(top);
this.addView(bottom);
this.measure(0, 0);
bottomHeiht = bottom.getMeasuredHeight();
//隐藏底部条目
this.setPadding(0, 0, 0, -bottomHeiht);
hideFunction.setOnClickListener(this);
groupCallNewsKeyboard.setOnClickListener(this);
groupCallNewsEt.addTextChangedListener(new TextWatcher() {
//这里的s表示改变之前的内容,通常start和count组合,可以在s中读取本次改变字段中被改变的内容。而after表示改变后新的内容的数量。
//start开始位置,count改变前的内容数量,after改变后的内容数量
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
//这里的s表示改变时的内容,通常start和count组合,可以在s中读取本次改变字段中新的内容。而before表示被改变的内容的数量。
//start开始位置,before改变前的内容数量,count改变的内容数量
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) { //表示et输入框最终的结果
if(s.length() == 0){
hideFunction.setVisibility(VISIBLE);
send.setVisibility(GONE);
}else{
hideFunction.setVisibility(GONE);
send.setVisibility(VISIBLE);
}
}
});
}
InputMethodManager inputMethodManager = (InputMethodManager) MyApplication.instance
.getSystemService(Context.INPUT_METHOD_SERVICE);
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.hide_function:
if(isShowBt){
if (isShow) {
//隐藏
this.setPadding(0, 0, 0, -bottomHeiht);
isShow = false;
} else {
//显示
this.setPadding(0, 0, 0, 0);
isShow = true;
}
}else{
if(activity !=null){
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
if(!isShow){
//显示
this.setPadding(0, 0, 0, 0);
isShow = true;
inputMethodManager.hideSoftInputFromWindow(totalRelativeLayout.getWindowToken(), 0); //强制隐藏键盘
return;
}
if(bottomDistance < oldBottomDistance){
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//显示
this.setPadding(0, 0, 0, 0);
isShow = true;
}else{
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
break;
case R.id.group_call_news_keyboard:
if(activity !=null){
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
if(isShowBt){
//显示et
groupCallNewsEt.setVisibility(VISIBLE);
button.setVisibility(GONE);
forcePop();
groupCallNewsKeyboard.setBackground(MyApplication.instance.getResources().getDrawable(R.drawable.recording));
if(isShow){
//隐藏
this.setPadding(0, 0, 0, -bottomHeiht);
isShow = false;
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
isShowBt = false;
}else {
//显示bt
hideKeyboard();
groupCallNewsEt.setVisibility(GONE);
button.setVisibility(VISIBLE);
hideFunction.setVisibility(VISIBLE);
send.setVisibility(GONE);
groupCallNewsKeyboard.setBackground(MyApplication.instance.getResources().getDrawable(R.drawable.soft_keyboard));
isShowBt = true;
}
break;
}
}
/**
* 显示et后,判断软键盘是否显示,没有则显示
*
*/
public void forcePop(){
if(bottomDistance == oldBottomDistance || bottomDistance > oldBottomDistance){
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
if(isShow){
//隐藏
this.setPadding(0, 0, 0, -bottomHeiht);
isShow = false;
}
}
}
/**
* 判断软键盘是否显示,显示就隐藏
*/
public void hideKeyboard(){
inputMethodManager.hideSoftInputFromWindow(totalRelativeLayout.getWindowToken(), 0); //强制隐藏键盘
if(isShow){
//隐藏
this.setPadding(0, 0, 0, -bottomHeiht);
isShow = false;
}
}
/**
* 提供给外界是否隐藏图像回传按钮
*/
public void hideVideoPostback(boolean ishowVideoPostback){
if(ishowVideoPostback){
videoPostback.setVisibility(VISIBLE);
}else{
videoPostback.setVisibility(INVISIBLE);
}
}
public void dataZero(){
//是否显示底部功能布局
isShow = false;
//是否显示ptt bt
isShowBt = true;
}
private int bottomDistance ;
private int oldBottomDistance;
private Activity activity;
private View totalRelativeLayout;
public void receviceRelativeLayout(Activity activity, View rlIncludeListview,View totalRelativeLayout){
this.activity = activity;
this.totalRelativeLayout = totalRelativeLayout;
rlIncludeListview.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
bottomDistance = bottom;
oldBottomDistance = oldBottom;
}
});
}
}
文章最后,希望对您有所帮助