SoftInput键盘隐藏在启动活动中,但在使用viewpager后始终显示自己

时间:2012-08-06 11:50:55

标签: android android-viewpager android-softkeyboard

我已经看过很多关于这个主题的话题,但仍然无法弄清楚它为什么会发生。 我在左侧有一个片段,它显示一个列表,右侧有一个视图寻呼机(有2页)。

当我第一次启动时,我的活动键盘没有显示。那很棒。 但是,当我点击一个列表项(这将在viewpager => myPager.setAdapter(..)中显示一个新东西),或者当我向右滑动(这里它还没有显示)时,然后返回到显示第一页,键盘开始再次显示,并在剩下的时间内继续显示我做的任何动作。

我已尝试将android:focusableInTouchMode="true" 放入我可以放入的每个布局中但仍然......

一件奇怪的事情是,我的布局中的编辑文本不是焦点,而是其他东西。布局或许,但我不知道(并且不知道如何知道=)。 这是有点奇怪,因为我尝试了很多解决方案,但它仍然无法正常工作。

如果有人有任何想法......

谢谢!!!!

这是我的代码的相关部分。

的manifest.xml

<activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="stateHidden" >

main_activity.xml

(DateList包含列表视图和下面的其他内容(不需要关注))

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<fragment android:name="com.example.main.courante.e.DateList"
 android:id="@+id/list_frag"
 android:tag="list"
 android:layout_width="0dip"
 android:layout_height="fill_parent"
 android:layout_weight="1"
/> 

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="3"
    android:paddingLeft="5dp"
    android:paddingTop="5dp" >  

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_gravity="center">

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"  
            android:layout_gravity="center" 
            android:tabStripEnabled="true"
             />

          <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>

viewpager中显示的布局:

(有点凌乱,我会承认,但只是为了告诉你是否有焦点模式等)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical"
android:scrollbars="vertical"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/linear"
android:paddingLeft="30dp"
android:paddingRight="30dp"
 >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_small"
android:layout_marginBottom="@dimen/padding_small"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/top_lin">
<!--     android:background="@drawable/rounded_border_fiche_element" -->


 <ImageView 
     android:id="@+id/perm_img"
     android:layout_width="75dp"
     android:layout_height="75dp"
     android:layout_marginLeft="50dp"
     android:padding="@dimen/padding_small"

     android:src="@android:drawable/ic_notification_overlay" 
     android:contentDescription="TODO"
     android:background="@drawable/image_bg"/> 

   <TextView
        android:id="@+id/perm_nom"
        style="@style/textview_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
         />

    <TextView
        style="@style/textview_info"
        android:id="@+id/perm_prenom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
         />

    <TextView
        style="@style/textview_info"
        android:id="@+id/perm_matricule"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingRight="@dimen/padding_medium"
         />

</LinearLayout>

<View
     android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp" />

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    android:id="@+id/table_check">
<!--         android:background="@drawable/rounded_border_fiche_element" -->


    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
       android:gravity="center">           

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m1"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m2"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:gravity="center">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m3"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m4"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m5"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m6"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    </TableRow>

</TableLayout>

<View
     android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:id="@+id/bot_lin">
<!--     android:background="@drawable/rounded_border_fiche_element"> -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bot_lin_lin"
    android:focusableInTouchMode="true"
    android:focusable="true" >

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/securite" />

    <EditText
        android:id="@+id/securite"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textMultiLine"
        android:lines="5" >

    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bot_lin_lin2"
    android:focusableInTouchMode="true"
    android:focusable="true" >

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/client" />

    <EditText
        android:id="@+id/clients"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textMultiLine"
        android:lines="5"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bot_lin_lin3"
    android:focusableInTouchMode="true"
    android:focusable="true" >

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/entretien" />

    <EditText
        android:id="@+id/entretien"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textMultiLine" 
        android:lines="5"/>

</LinearLayout>

</LinearLayout>

</LinearLayout>
</ScrollView>

编辑:

我注意到了一件事。当我轻扫我的寻呼机时,键盘空格键左侧的第二个按钮从“:-)”快速变为“:”,并在滚动结束时返回“:-)”。第二个“:”来自我在操作栏上的searchwidget。也许如果我能阻止这个人获得焦点就可以了吗?我该怎么办?因为它定义如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
    android:title="@string/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never" />

<item android:id="@+id/menu_search"
      android:title="@string/menu_search"
      android:icon="@drawable/ic_menu_search"
      android:showAsAction="ifRoom"
      android:actionViewClass="android.widget.SearchView"
      android:inputType="number" />

编辑2:

为了防止searchwidget获得焦点,我已经把这两行代码放在了

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(searchView.getWindowToken(),0); 

无处不在我调用myPager.setAdapter()即当我点击列表项时,我还要把它放在publicpout的onPageScrolled()中...但它真的不是很流利,因为你很快就看到了键盘显示并在不到一秒钟内消失......如果有人有其他想法..

1 个答案:

答案 0 :(得分:1)

我有一个合理的想法是避免在

上打开SoftInput键盘

将此视图添加到viewpager中显示的布局:

<LinearLayout
        android:id="@+id/ll_focus"
        android:layout_width="0px"
        android:layout_height="0px"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:visibility="visible" >
    </LinearLayout>

这很好用。试试这个亲爱的朋友