Android开发中关于如何解决Fragment中弹出键盘会把底部菜单栏顶上去的问题以及返回键的监听问题
2014-12-23 15:20
971 查看
一,首先为了保证不被键盘顶上去,需要在Manifest中设置定义的FragmentActivity属性
android:windowSoftInputMode="stateHidden|adjustPan",这样会保证底部栏保持不变。
这个是我自己的FragmentActivity
<activity
android:name=".MainIndex"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden|adjustPan"
android:configChanges="keyboard">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
/>
<category
android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
二,如果是将需要输入EditText的布局写在当前的frgment中,就会出现问题,虽然设置了android:windowSoftInputMode,但是当键盘消失时,底部栏还是能够明显的看到高度变化了。
因此,个人建议将输入的布局写成PopupWindow的形式,从屏幕底部弹出来,然后键盘会将PopupWindow顶上去,不会影响到fragment的布局。
以下是输入布局实现
<?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="match_parent"
>
<TextView
android:id="@+id/message_replytop"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@id/message_reply"
/>
<LinearLayout
android:id="@+id/message_reply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:orientation="horizontal"
>
//此处的EditTextSpuer下面会提到
<com.app.function.EditTextSpuer
android:id="@+id/message_replybox_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_weight="7"
android:maxLines="3"
android:background="@drawable/comment_shape"
android:textColor="#646c78"
android:textSize="18sp"
/>
<TextView
android:id="@+id/message_replybox_send"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/comment_sendbg"
android:gravity="center"
android:text="确定"
android:textSize="20sp"
android:textColor="@color/white"
/>
</LinearLayout>
</RelativeLayout>
三,接下来就需要在fragment中设置PopupWindow的弹出事件。
以下是本人实现的代码
public void ReplyBox() {
final View view = View.inflate(getActivity(),
R.layout.message_replybox,
null);
comment_edit = (EditTextSpuer) view.findViewById(R.id.message_replybox_edit);
send = (TextView) view.findViewById(R.id.message_replybox_send);
TextView dismiss = (TextView) view.findViewById(R.id.message_replytop);
send.setOnClickListener(new OnClickListener() {
@Override
public
void onClick(View v) {
//
TODO Auto-generated method stub
if (TextUtils.isEmpty(comment_edit.getText().toString().trim())) {
Toast.makeText(getActivity(),
"请填写回复内容", Toast.LENGTH_SHORT)
.show();
} else {
postMessage();
}
}
});
dismiss.setO
f374
nClickListener(new OnClickListener() {
@Override
public
void onClick(View v) {
InputMethodManager imm = (InputMethodManager)
comment_edit
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
mpopupWindow.dismiss();
}
});
view.startAnimation(AnimationUtils.loadAnimation(getActivity(),
R.anim.fade_in));
if (mpopupWindow ==
null) {
mpopupWindow =
new PopupWindow(getActivity());
mpopupWindow.setWidth(LayoutParams.MATCH_PARENT);
mpopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
mpopupWindow.setBackgroundDrawable(new ColorDrawable());
mpopupWindow.setFocusable(true);
mpopupWindow.setTouchable(true);
mpopupWindow.setOutsideTouchable(true);
mpopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
//这行代码非常关键,如果不加上,弹出的键盘会讲mpopupWindow的布局遮挡一部分,一定要加,切记切记。。。。。
mpopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
mpopupWindow.setContentView(view);
comment_edit.requestFocus();
comment_edit.setText("");
comment_edit.setHint(texthint);
comment_edit.setHintTextColor(getResources().getColor(R.color.hintcolor));
mpopupWindow.showAtLocation(mylistview, Gravity.BOTTOM, 0, 0);
mpopupWindow.update();
}
这里给大家一个弹出和隐藏软键盘的代码,非常实用。主要是要保持mpopupWindow和键盘同时出现和消失。
private void popupInputMethodWindow() {
Handler handler =
new Handler();
handler.postDelayed(new Runnable() {
@Override
public
void run() {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}, 0);
}
四,最后还会有个问题,当键盘弹出后,按下返回键,键盘会消失,但是输入布局不会消失,这里需要重写edittext,实现返回键的监听,本人试了很多方法,
只用这个可以很方便的实现。
package com.app.function;
import com.app.fragment.FragmentMessage;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class EditTextSpuer extends EditText{
InputMethodManager imm;
public EditTextSpuer(Context context,AttributeSet attributeSet) {
super(context,attributeSet);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER ) {
Log.v("xpf","--------------------+onKeyDown---------enter");
return true;
}
return super.onKeyDown(keyCode, event);
}
public boolean onKeyPreIme (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN ) {
//调用了fragment中的静态方法,关闭mpopupWindow和键盘,这里最为关键。
FragmentMessage.hiddenpopupWindow();
Log.v("xpf","--------------------+onKeyPreIme---------back");
return true;
}
return super.onKeyPreIme(keyCode, event);
}
}
public
static void hiddenpopupWindow(){
if(mpopupWindow!=null&&mpopupWindow.isShowing()){
InputMethodManager imm = (InputMethodManager)
comment_edit
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(mpopupWindow
.getContentView().getWindowToken(), 0);
}
mpopupWindow.dismiss();
mpopupWindow=null;
}
}
以上,就可以很好的解决fragment中关于键盘弹出的各种问题,代码时本人亲自编写,经过实际验证,没有问题。以及让人头疼的返回键的监听的问题也可以一并解决。
希望对大家能有所帮助,也希望大家给予意见。
android:windowSoftInputMode="stateHidden|adjustPan",这样会保证底部栏保持不变。
这个是我自己的FragmentActivity
<activity
android:name=".MainIndex"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden|adjustPan"
android:configChanges="keyboard">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
/>
<category
android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
二,如果是将需要输入EditText的布局写在当前的frgment中,就会出现问题,虽然设置了android:windowSoftInputMode,但是当键盘消失时,底部栏还是能够明显的看到高度变化了。
因此,个人建议将输入的布局写成PopupWindow的形式,从屏幕底部弹出来,然后键盘会将PopupWindow顶上去,不会影响到fragment的布局。
以下是输入布局实现
<?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="match_parent"
>
<TextView
android:id="@+id/message_replytop"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@id/message_reply"
/>
<LinearLayout
android:id="@+id/message_reply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:orientation="horizontal"
>
//此处的EditTextSpuer下面会提到
<com.app.function.EditTextSpuer
android:id="@+id/message_replybox_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_weight="7"
android:maxLines="3"
android:background="@drawable/comment_shape"
android:textColor="#646c78"
android:textSize="18sp"
/>
<TextView
android:id="@+id/message_replybox_send"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/comment_sendbg"
android:gravity="center"
android:text="确定"
android:textSize="20sp"
android:textColor="@color/white"
/>
</LinearLayout>
</RelativeLayout>
三,接下来就需要在fragment中设置PopupWindow的弹出事件。
以下是本人实现的代码
public void ReplyBox() {
final View view = View.inflate(getActivity(),
R.layout.message_replybox,
null);
comment_edit = (EditTextSpuer) view.findViewById(R.id.message_replybox_edit);
send = (TextView) view.findViewById(R.id.message_replybox_send);
TextView dismiss = (TextView) view.findViewById(R.id.message_replytop);
send.setOnClickListener(new OnClickListener() {
@Override
public
void onClick(View v) {
//
TODO Auto-generated method stub
if (TextUtils.isEmpty(comment_edit.getText().toString().trim())) {
Toast.makeText(getActivity(),
"请填写回复内容", Toast.LENGTH_SHORT)
.show();
} else {
postMessage();
}
}
});
dismiss.setO
f374
nClickListener(new OnClickListener() {
@Override
public
void onClick(View v) {
InputMethodManager imm = (InputMethodManager)
comment_edit
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
mpopupWindow.dismiss();
}
});
view.startAnimation(AnimationUtils.loadAnimation(getActivity(),
R.anim.fade_in));
if (mpopupWindow ==
null) {
mpopupWindow =
new PopupWindow(getActivity());
mpopupWindow.setWidth(LayoutParams.MATCH_PARENT);
mpopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
mpopupWindow.setBackgroundDrawable(new ColorDrawable());
mpopupWindow.setFocusable(true);
mpopupWindow.setTouchable(true);
mpopupWindow.setOutsideTouchable(true);
mpopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
//这行代码非常关键,如果不加上,弹出的键盘会讲mpopupWindow的布局遮挡一部分,一定要加,切记切记。。。。。
mpopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
mpopupWindow.setContentView(view);
comment_edit.requestFocus();
comment_edit.setText("");
comment_edit.setHint(texthint);
comment_edit.setHintTextColor(getResources().getColor(R.color.hintcolor));
mpopupWindow.showAtLocation(mylistview, Gravity.BOTTOM, 0, 0);
mpopupWindow.update();
}
这里给大家一个弹出和隐藏软键盘的代码,非常实用。主要是要保持mpopupWindow和键盘同时出现和消失。
private void popupInputMethodWindow() {
Handler handler =
new Handler();
handler.postDelayed(new Runnable() {
@Override
public
void run() {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}, 0);
}
四,最后还会有个问题,当键盘弹出后,按下返回键,键盘会消失,但是输入布局不会消失,这里需要重写edittext,实现返回键的监听,本人试了很多方法,
只用这个可以很方便的实现。
package com.app.function;
import com.app.fragment.FragmentMessage;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class EditTextSpuer extends EditText{
InputMethodManager imm;
public EditTextSpuer(Context context,AttributeSet attributeSet) {
super(context,attributeSet);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER ) {
Log.v("xpf","--------------------+onKeyDown---------enter");
return true;
}
return super.onKeyDown(keyCode, event);
}
public boolean onKeyPreIme (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN ) {
//调用了fragment中的静态方法,关闭mpopupWindow和键盘,这里最为关键。
FragmentMessage.hiddenpopupWindow();
Log.v("xpf","--------------------+onKeyPreIme---------back");
return true;
}
return super.onKeyPreIme(keyCode, event);
}
}
public
static void hiddenpopupWindow(){
if(mpopupWindow!=null&&mpopupWindow.isShowing()){
InputMethodManager imm = (InputMethodManager)
comment_edit
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(mpopupWindow
.getContentView().getWindowToken(), 0);
}
mpopupWindow.dismiss();
mpopupWindow=null;
}
}
以上,就可以很好的解决fragment中关于键盘弹出的各种问题,代码时本人亲自编写,经过实际验证,没有问题。以及让人头疼的返回键的监听的问题也可以一并解决。
希望对大家能有所帮助,也希望大家给予意见。
相关文章推荐
- 如何解决 Android 软键盘弹出,会把原来的界面挤上去的问题?
- Fragment中弹出键盘会把底部菜单栏顶上去
- android开发中有EditText,当输入时输入法会把底部的导航栏顶上去。如何解决?
- Android开发问题:底部弹出popwindow,会被某些手机底部的菜单栏挡住
- Android开发ViewPager的预加载和Fragment的销毁问题,以及tabLayout+ViewPager的使用,tablayout平板适配问题解决
- Android App监听软键盘按键的三种方式 以及 输入法删除退格键的冲突问题解决
- 【Android基础】Android开发键盘把底部导航顶上去了解决办法
- [Android初级]关于如何解决软键盘弹出后覆盖界面布局的问题
- android开发步步为营之105:解决键盘弹起页面被顶上去问题
- Android开发中如何解决Fragment +Viewpager滑动页面重复加载的问题
- Android底部弹出PopupWindow被菜单栏遮挡问题以及窗体亮度设置
- 如何部署Android开发环境以及解决部署中遇到的问题
- android技术--------如何解决弹出键盘只影响当前fragment输入的窗口
- Android App监听软键盘按键的三种方式 以及 输入法删除退格键的冲突问题解决
- 关于Android原生集成5+webview,监听webview返回时,执行两次onkey方法问题的解决
- Android开发:底部Tab菜单栏 FragmentTabHost 问题总结
- android软键盘弹出,会把原来的界面挤上去的问题 处理方法
- Android中如何解决输入法键盘和activity页面遮挡的问题
- Android 如何监听返回键,弹出一个退出对话框
- Android 如何监听返回键,弹出一个退出对话框