[tablet] [片段]如何处理隐藏的事件软键盘

时间:2012-07-16 06:56:25

标签: android input fragment tablet android-softkeyboard

我的笔记应用程序在屏幕上有2个片段:一个音符片段列表和一个音符细节片段,用于在第二个片段中显示所选音符,它有一个输入文本字段(android:inputType =“textMultiLine”)。因为我想隐藏处理事件键盘,所以我可以保存用户关闭键盘时所做的更改。谁能给我一个完成这项任务的线索?

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

我认为键盘问题的最佳和通用解决方案是在布局更改时测量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;
}

答案 2 :(得分: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"