简体   繁体   中英

Different SoftInputMode in the same Activity, is it possible?

I try to set the adjustment of my fragments differently when the keyboard opens, but for the moment I have no positive results.enter code here

The objective would be to readjust only one of the two fragments.

Here is an example

Activity

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();
        }
    }
...

fragments:

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;
    }
} 

main layout of the 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>

ok saw that I redefined twice SoftInputMode on the activity, this is unlikely to succeed. so, is it possible to do something like that fragment.getWindows.setSoftInputMode (...

sorry for my english ...

Are both your Fragments are on screen at the same time? If so it isn't possible to have different behavior depending on which Fragment triggered the soft input to open since the setting is a applicable to the whole window only.

On the other hand if you only have one of these Fragments on screen at a time then the method onCreateView() is probably not the right place for this code. You might want to try putting it in the Fragment methods onResume() and onPause() . In onResume() save the existing soft input mode and set it to what you want and in onPause() revert the soft input mode to what it was before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM