我的笔记应用程序在屏幕上有 2 个片段:一个笔记片段列表和一个用于在第二个片段中显示所选笔记的笔记详细片段,它有一个输入文本字段 ( android:inputType="textMultiLine"
)。因为我想处理隐藏的事件键盘,所以我可以保存用户在关闭键盘时所做的更改。谁能给我一个线索来完成这项任务?
问问题
2465 次
3 回答
3
我认为键盘问题的最佳和通用解决方案是在布局更改时测量 VisibleDisplayFrame:
Rect visibleRect = new Rect();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup mMain = (ViewGroup) inflater.inflate(R.layout.select_cover_album, container, false);
mMain.getWindowVisibleDisplayFrame(visibleRect);
mMain.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
Rect r = new Rect();
mMain.getWindowVisibleDisplayFrame(r);
if(r.height() < visibleRect.height()){
//keyboard shown
}
if(r.height() == visibleRect.height()){
//keyboard hidden
}
}
});
return mMain;
}
于 2016-03-09T11:10:55.713 回答
2
这是我在 Note Detail 片段上处理它的方式:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.view = inflater.inflate(R.layout.fragment_note_detail_of_style_searching, container , false);
listviewFilter = (ListView) view.findViewById(R.id.listview_filter);
noteDetailView = view.findViewById(R.id.note_detail_of_style_view);
photoDetailView = view.findViewById(R.id.gv_note_detail_photo);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = view.getRootView().getHeight() - view.getHeight();
if (heightDiff > 200) {
isWatchingKeyboard = true;
photoDetailView.setVisibility(View.GONE);
return; //must exit now
}
if(isWatchingKeyboard){
isWatchingKeyboard = false;
viewForFocus.requestFocus();
photoDetailView.setVisibility(View.VISIBLE);
}
}
});
return this.view;
}
于 2012-10-25T06:39:06.317 回答
0
我是这样做的:
private int prevBottom;
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_status, container, false);
prevBottom = view.getBottom();
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (view.getBottom() > prevBottom) {
view.requestFocus();
}
prevBottom = view.getBottom();
}
});
return view;
}
并将其添加到我正在膨胀的视图中:
android:focusableInTouchMode="true"
这与 AndroidManifest.xml 中的活动有关
android:windowSoftInputMode="adjustResize"
于 2014-06-14T16:38:20.187 回答