Frage

I have 2 tabs.

When 1 is opened, I want to show the keyboard. When the other is opened, I want to make sure the keyboard is closed.

Here is my code for the fragment of the tab I want to show the keyboard (when this is opened, it auto-focuses an EditText the user will use to perform a search:

public class AddFoodSearchFragment extends Fragment {       
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        return V;
    }    
}

And the fragment to close it:

public class AddFoodFormFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View V = inflater.inflate(R.layout.fragment_add_food_form, container, false);

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        return V;
    }
}

Why is it not responding the way I want it to? I don't understand what is so difficult about this.

EDIT

I could just as well do this via adding onPause() to my 1 fragment, but its still not working:

public class AddFoodSearchFragment extends Fragment {       
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        return V;
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}
War es hilfreich?

Lösung 2

Here is the answer that works 100% consistently:

public class AddFoodSearchFragment extends Fragment {   
    EditText search;
    InputMethodManager imm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);
        search = (EditText) V.findViewById(R.id.search);
        imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);     
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

        return V;
    }

    @Override
    public void onPause() {
        super.onPause();

        imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
    }
}

Andere Tipps

Here is an example of what I believe you are trying to do:

MyEditor.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch even
}
});

You should focus on this line and implement it into your project:

MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input

for more information, check out this Link...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top