조각 내부에서 소프트 키보드를 숨기는 방법은 무엇입니까?
나는 여러 조각을 제공하기 위해 FragmentActivity
사용하고 ViewPager
있습니다. 각각의 ListFragment
레이아웃은 다음과 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<EditText android:id="@+id/entertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
활동을 시작하면 소프트 키보드가 표시됩니다. 이 문제를 해결하기 위해 조각 내에서 다음을 수행했습니다.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Save the container view so we can access the window token
viewContainer = container;
//get the input method manager service
imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
. . .
}
@Override
public void onStart() {
super.onStart();
//Hide the soft keyboard
imm.hideSoftInputFromWindow(viewContainer.getWindowToken(), 0);
}
기본 활동에 대한 창 토큰에 액세스하는 방법으로 들어오는 ViewGroup container
매개 변수를 저장합니다 onCreateView
. 이것은 오류없이 실행되지만 키보드는 hideSoftInputFromWindow
in에 대한 호출에서 숨겨지지 않습니다 onStart
.
원래는 대신 부풀린 레이아웃을 사용해 보았습니다 container
.
imm.hideSoftInputFromWindow(myInflatedLayout.getWindowToken(), 0);
그러나 이것은 NullPointerException
아마도 조각 자체가 활동이 아니고 고유 한 창 토큰이 없기 때문에.
조각 내에서 소프트 키보드를 숨기는 방법이 FragmentActivity
있습니까? 아니면에서 메서드를 만들고 조각 내에서 호출해야합니까?
프래그먼트가 뷰를 생성하는 한 해당 뷰 가 연결된 후 해당 뷰에서 IBinder (창 토큰)를 사용할 수 있습니다 . 예를 들어 Fragment에서 onActivityCreated를 재정의 할 수 있습니다.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
다음 코드 줄 외에는 아무것도 없었습니다.
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
활동의 매니페스트 정의에 다음 속성을 추가하면 활동이 열릴 때 키보드가 터지는 것을 완전히 억제합니다. 도움이 되었기를 바랍니다.
(활동의 매니페스트 정의에 추가) :
android:windowSoftInputMode="stateHidden"
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container,
false);
someClass.onCreate(rootView);
return rootView;
}
내 클래스에 내 루트 뷰의 인스턴스를 유지
View view;
public void onCreate(View rootView) {
view = rootView;
보기를 사용하여 키보드 숨기기
public void removePhoneKeypad() {
InputMethodManager inputManager = (InputMethodManager) view
.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = view.getWindowToken();
inputManager.hideSoftInputFromWindow(binder,
InputMethodManager.HIDE_NOT_ALWAYS);
}
Exception for DialogFragment
though, focus of the embedded Dialog
must be hidden, instead only the first EditText
within the embedded Dialog
this.getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
in Fragemnt Working this Code
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
this will be work in my case when in tabs i switch from one fragment to another fragments
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
try {
InputMethodManager mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
Log.e(TAG, "setUserVisibleHint: ", e);
}
}
}
Use this static method, from anywhere (Activity / Fragment) you like.
public static void hideKeyboard(Activity activity) {
try{
InputMethodManager inputManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocusedView = activity.getCurrentFocus();
if (currentFocusedView != null) {
inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}catch (Exception e){
e.printStackTrace();
}
}
If you want to use for fragment just call hideKeyboard(((Activity) getActivity()))
.
Nothing of this worked on API27. I had to add this in the container of the layout, for me it was a ConstraintLayout:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:focusedByDefault="true">
//Your layout
</android.support.constraint.ConstraintLayout>
Just add this line in you code:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
In Kotlin:
(activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(view?.windowToken,0)
참고URL : https://stackoverflow.com/questions/7940765/how-to-hide-the-soft-keyboard-from-inside-a-fragment
'your programing' 카테고리의 다른 글
내 ListBox에서 수직 스크롤바를 어떻게 얻을 수 있습니까? (0) | 2020.10.09 |
---|---|
버퍼 끝까지 vim 매크로 재생 (0) | 2020.10.09 |
Ruby에서 현재 시간을 13 자리 정수로 얻는 방법은 무엇입니까? (0) | 2020.10.08 |
CollapsingToolbarLayout 서체 및 크기를 변경하는 방법은 무엇입니까? (0) | 2020.10.08 |
문자열 값으로 열거 형을 정의하는 방법은 무엇입니까? (0) | 2020.10.08 |