求问各位大神关于fragment切换,软键盘隐藏的问题

============问题描述============


问题背景,一个顶层linearlayout下面有个两个子linearlayout,第一个子linearlayout里面给一排button,可以水平滑动,第二个子linearlayout里面用来放fragment,xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/screen" >
    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#ffff00"
                android:orientation="horizontal" >
                <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
                <Button
                    android:id="@+id/button2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/center"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical" >
    </LinearLayout>
</linearlayout>
默认载入一个fragment,里面放几个edittext,用于输入一些信息。在mainactivity里面给顶级linearlayout注册一个ontouchlistener:
 screen.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub
                InputMethodManager imm = (InputMethodManager)VillageActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(screen.getWindowToken(), 0);
                //imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);   
                return false;
            }
        });
同时给几个button注册click事件,点击不同button切换不同fragment,fragment里面也都是一些edittext。

问题是:
1、当程序开始运行,载入默认fragment后,光标会停留在第一个edittext中,点击这个edittext,输入框会调出,点击空白处,输入框消失。但是当上方按钮后,首先输入框没有隐藏掉,在点击空白处,也没有隐藏。。这是为什么?观察后发现,当点击上方按钮后,edittext中的光标已消失,这个时候焦点集中在button上。。难道输入框的隐藏,前提是当前焦点必须在edittext上?
2、当点击其他按钮,切换到其他fragment后,这个screen的ontouchlistener完全失效,这又是为啥呢?按道理不管第二个子linearlayout里面换什么fragment,都是属于顶级linearlayout的啊,只要监听顶级linearlayout的事件,应该都可以捕获到啊。。

求各位大神解答。。

============解决方案1============


http://blog.csdn.net/yskang/article/details/39012127

============解决方案2============


看着头都昏了,把这个setOnTouchListener写进父类fragment里面把,然后子fragment继承他

转载于:https://www.cnblogs.com/hugangyan86/p/4038758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值