软键盘弹出时,位于碎片中的EditText被隐藏了。

6

我的 Activity 包含两个 Fragment,在下方有一个控制栏,在上方是带有两个 EditText 的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/invested_grey_color" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="5dp"
    android:background="@color/transperent_black" >

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/choose_a_set_LeagueName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/view_tournament_rules"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/choose_a_set_view_tournamnet_rules_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/choose_a_set_LeagueName"
        android:clickable="true"
        android:src="@drawable/question" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/Choose_a_set_rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relativeLayout1"
    android:layout_marginTop="15dp"
    android:background="@color/transperent_sportzone_green" >

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/choose_a_set_stage_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/choose_a_set"
        android:textColor="@color/sportzone_green"
        android:textSize="35sp" />

    <ImageView
        android:id="@+id/choose_a_set_vi_sign_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/make_it_your_own"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Choose_a_set_rl"
    android:layout_marginTop="1dp"
    android:background="@color/transperent_sportzone_green" >

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/make_it_your_own_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/make_it_your_own"
        android:textColor="@color/sportzone_green"
        android:textSize="35sp" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/add_tournament_name_ll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/edit_text_wraper"
    android:layout_marginTop="14dp"
    android:background="@color/add_tournament_red_color"
    android:visibility="invisible" >

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/information_to_user_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp"
        android:text="@string/add_tournamnet_name"
        android:textColor="@color/white"
        android:textSize="20sp" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/tournamnet_make_it_your_own_next_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="5dp"
    android:layout_below="@+id/add_tournament_name_ll"
    android:background="@color/transperent_black"
    android:clickable="true"
    android:enabled="false" >

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/make_iyt_your_own_next_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp"
        android:text="@string/next"
        android:textColor="@color/disables_btn_text_color"
        android:textSize="@dimen/next_btn_font" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/invite_friends_rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tournamnet_make_it_your_own_next_btn"
    android:background="@color/transperent_sportzone_green" >

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/invites_friends_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/invite_your_friends"
        android:textColor="@color/sportzone_green"
        android:textSize="35sp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/edit_text_wraper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/make_it_your_own" >

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:text="@string/tournament_name"
        android:textColor="@color/white"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/tournament_name_et"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginRight="20dp"
        android:background="@drawable/rounded_et"
        android:ems="10"
        android:inputType="text"
        android:maxLength="30"
        android:paddingLeft="20dp"
        android:textSize="30sp" >

        <requestFocus />
    </EditText>

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tournament_name_et"
        android:layout_below="@+id/tournament_name_et"
        android:layout_marginTop="26dp"
        android:text="@string/jeckpot"
        android:textColor="@color/white"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/jeckpot_amont_et"
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:background="@drawable/rounded_et"
        android:ems="10"
        android:inputType="number"
        android:maxLength="9"
        android:paddingLeft="20dp"
        android:textSize="35sp" />

    <ImageView
        android:id="@+id/tournamentt_type_instructions_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/jeckpot_amont_et"
        android:layout_alignRight="@+id/tournament_name_et"
        android:clickable="true"
        android:src="@drawable/question" />

    <com.threefishmedia.sportzone.views.TextViewCustomFont
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tournamentt_type_instructions_iv"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/tournament_type"
        android:textColor="@color/white"
        android:textSize="30sp" />

    <Switch
        android:id="@+id/basic_expert_switcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/jeckpot_amont_et"
        android:layout_alignBottom="@+id/jeckpot_amont_et"
        android:layout_toRightOf="@+id/textView1"
        android:textOff="@string/basic"
        android:textOn="@string/expert"
        android:thumb="@drawable/thumb_selector"
        android:track="@drawable/switch_bg" />

</RelativeLayout>

问题是当用户点击第二个EditText时,键盘会将其隐藏,用户无法看到自己输入的内容。 我已经花费了很长时间来解决这个问题……我已经添加了

android:windowSoftInputMode="adjustResize"

在清单文件中使用adjustpanadjustResize都不能解决问题。是否有其他想法?

尝试将 android:layout_gravity="bottom" 设置到 RelativeLayout - Lal
将所有代码放在ScrollView中,然后添加android:windowSoftInputMode="adjustResize"。 - Akash Moradiya
4个回答

15

将这行代码放在 onCreate 方法中。

 getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

对我最终奏效的方法是将 getActivity().getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 放在片段的onCreateView中。仅将代码放入活动的onCreate中不起作用。据我所知,活动的“this”和片段中的getActivity()应该是相同的,但这并没有起作用。谢谢! - steven smith

0
根据文档:

"adjustResize"

活动的主窗口总是被调整大小以为软键盘腾出空间。

"adjustPan"

活动的主窗口不会被调整大小以为软键盘腾出空间。相反,窗口的内容会自动平移,以便当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小好,因为用户可能需要关闭软键盘才能访问和与窗口中被遮挡的部分交互。

因此,在片段中,您应该在清单中使用 adjustPan:

<activity
    ...
    android:windowSoftInputMode="adjustPan"/>

请注意,如果EditText没有被聚焦(您没有在其中输入),键盘仍然会隐藏EditText。

0

你也可以将这行代码放在清单文件中

   android:windowSoftInputMode="adjustResize|stateHidden"

该解决方案在此情况下不适用。OP已经提到他正在使用片段,在这种情况下@Sreedhu Madhu的解决方案有效。 - Salman Younas

0

我曾经遇到过类似的问题,将这个添加到清单中对我有帮助。

<activity
    ...
    android:windowSoftInputMode="adjustPan"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接