InputMethodManager.showSoftInput return 为何或何时为假?
Why or when does the InputMethodManager.showSoftInput return false?
我试图在屏幕上弹出一个软键盘,首先以编程方式加载(不更改清单中的 windowSoftInputMode)。
有趣的是在屏幕上第一次加载时,它根本不起作用。这是代码块。
mEDT.requestFocus();
mEDT.requestFocusFromTouch();
mImm.showSoftInput(mEDT, InputMethodManager.SHOW_IMPLICIT);
showSoftInput 是return false,这导致软键盘没有显示。
但是当我点击 EditText 时。 showSoftInput return true 并显示软键盘。
谁能给我解释一下发生了什么事?
在您的 manifest.xml
文件中,添加
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
到你的 Activity 名称,你想在其启动时显示键盘
试试这个:
<activity
...
android:windowSoftInputMode="stateVisible" >
</activity>
或
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
您也可以像这样以编程方式执行此操作
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
你只需要给他打电话,试试这个:
EditText editText= (EditText) findViewById(R.id.editText);
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
您在使用 Fragments 吗?我发现 showSoftInput()
在 Fragments 中不可靠。
查看源代码后,我发现在onCreate()
/onCreateView()
或onResume()
中调用requestFocus()
并不会立即使对象获得焦点。这很可能是因为尚未创建内容视图。因此焦点发生在 Activity 或 Fragment.
初始化期间的某个时间
我在 onViewCreated()
中调用 showSoftInput()
更成功。
public class MyFragment extends Fragment {
private InputMethodManager inputMethodManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
EditText text1 = (EditText) view.findViewById(R.id.text1);
text1.requestFocus();
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view.findFocus(), InputMethodManager.SHOW_IMPLICIT);
super.onViewCreated(view, savedInstanceState);
}
}
即使您不使用片段,我敢打赌同样的规则也适用。因此,请确保在调用 showSoftInput() 之前创建了视图。
我试图在屏幕上弹出一个软键盘,首先以编程方式加载(不更改清单中的 windowSoftInputMode)。
有趣的是在屏幕上第一次加载时,它根本不起作用。这是代码块。
mEDT.requestFocus();
mEDT.requestFocusFromTouch();
mImm.showSoftInput(mEDT, InputMethodManager.SHOW_IMPLICIT);
showSoftInput 是return false,这导致软键盘没有显示。
但是当我点击 EditText 时。 showSoftInput return true 并显示软键盘。
谁能给我解释一下发生了什么事?
在您的 manifest.xml
文件中,添加
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
到你的 Activity 名称,你想在其启动时显示键盘
试试这个:
<activity
...
android:windowSoftInputMode="stateVisible" >
</activity>
或
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
您也可以像这样以编程方式执行此操作
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
你只需要给他打电话,试试这个:
EditText editText= (EditText) findViewById(R.id.editText);
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
您在使用 Fragments 吗?我发现 showSoftInput()
在 Fragments 中不可靠。
查看源代码后,我发现在onCreate()
/onCreateView()
或onResume()
中调用requestFocus()
并不会立即使对象获得焦点。这很可能是因为尚未创建内容视图。因此焦点发生在 Activity 或 Fragment.
我在 onViewCreated()
中调用 showSoftInput()
更成功。
public class MyFragment extends Fragment {
private InputMethodManager inputMethodManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
EditText text1 = (EditText) view.findViewById(R.id.text1);
text1.requestFocus();
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view.findFocus(), InputMethodManager.SHOW_IMPLICIT);
super.onViewCreated(view, savedInstanceState);
}
}
即使您不使用片段,我敢打赌同样的规则也适用。因此,请确保在调用 showSoftInput() 之前创建了视图。