【问题标题】:Different SoftInputMode in the same Activity, is it possible?同一个Activity中不同的SoftInputMode,可以吗?
【发布时间】:2024-01-12 20:08:01
【问题描述】:

我尝试在键盘打开时设置不同的片段调整,但目前我没有积极的结果。在此处输入代码

目标是仅重新调整两个片段中的一个。

这是一个例子

活动

public class MainActivity extends Activity {
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.frame1, new PlaceholderFragment())
                    .commit();
            getFragmentManager().beginTransaction()
                    .add(R.id.frame2, new PlaceholderFragment1())
                    .commit();
        }
    }
...

片段:

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
     //   Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.noresize);
     //   LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);


        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        return inflater.inflate(R.layout.fragment_main, container, false);

    }
}

public static class PlaceholderFragment1 extends Fragment {

    public PlaceholderFragment1() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        View rootView = inflater.inflate(R.layout.fragment_main_2, container, false);
        return rootView;
    }
} 

Activity的主要布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame"
    android:orientation="horizontal"
    >
<FrameLayout
        android:id="@+id/frame1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">
<FrameLayout
        android:id="@+id/frame2"
        android:background="#30000000"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">

</LinearLayout>

好的,看到我在活动上重新定义了两次 SoftInputMode,这不太可能成功。 那么,是否可以做类似 fragment.getWindows.setSoftInputMode (...

对不起我的英语......

【问题讨论】:

    标签: android fragment window-soft-input-mode


    【解决方案1】:

    你的两个片段是否同时出现在屏幕上?如果是这样,则不可能有不同的行为,具体取决于哪个 Fragment 触发了软输入以打开,因为该设置仅适用于整个窗口。

    另一方面,如果您一次只在屏幕上显示这些片段中的一个,那么方法 onCreateView() 可能不适合此代码。您可能想尝试将其放入 Fragment 方法 onResume()onPause()。在onResume() 中保存现有的软输入模式并将其设置为您想要的,在onPause() 中将软输入模式恢复为之前的模式。

    【讨论】:

    • 是的,目标是同时显示两个片段。我会看到另一个解决方案。谢谢
    • 嗨,Satur9nine。你有没有解决上述问题。如果您有任何解决方案,请提及。