java-自动显示键盘
请向我解释有关软键盘的问题。例如,我的活动,对话框片段或片段活动(无论如何)上都有一个EditText。这里是:
android:id="@+id/edPswrd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
当它第一次显示时,我看不到软键盘,必须按下editText才能获得焦点,然后出现键盘。 另一个活动有所不同,当它出现在屏幕上时,将在没有任何帮助的情况下加载键盘。我认为
表示EditText将被聚焦并且键盘将出现,但是我错了。
我应该如何管理哪个组件将获得焦点并且键盘会自动出现。
9个解决方案
64 votes
我认为这是一个bug或一项功能,试图向您展示整个活动,而不会首先用软键盘掩盖它。 我曾经搜索过有关该信息的信息,但不幸的是没有发现任何来自可靠来源的信息。
无论如何,要显示软键盘,您可以执行以下操作:
EditText editText = (EditText)findViewById(R.id.edit_text_id);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
我还看到了以下代码,该代码应强制在活动开始后立即显示软键盘,但从未尝试过:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
如果要隐藏软键盘,可以执行以下操作:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
希望有帮助。
编辑:
对于DialogFragment,此方法应该起作用:在onCreateView()方法中,请执行以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_id, container);
EditText editText = (EditText)view.findViewById(R.id.edit_text_id);
// show soft keyboard
editText.requestFocus();
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return view;
}
andr answered 2020-08-10T01:53:17Z
30 votes
打开Android清单文件。
寻找这样的活动标签
android:name="com.example.framework.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateVisible" //Add this line
>
如上所示,添加以下行:
android:windowSoftInputMode =“ stateVisible”
manpreet singh answered 2020-08-10T01:53:48Z
15 votes
我知道已经回答了这个问题,但是我在onCreateDialog中找到了一种可以接受的答案,而不是在onCreateView中找到了一种接受的答案。完成构建器后,返回之前,请执行以下操作:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// blah blah blah do builder stuff here like setTitle, setView, etc
Dialog d = builder.create();
这是重要的部分:
d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;
MidasLefko answered 2020-08-10T01:54:13Z
7 votes
添加到onCreate或onStart();
myView.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Konrad Winkowski answered 2020-08-10T01:54:33Z
2 votes
试试这个
@Override
public void onResume() {
super.onResume();
final View v = getDialog().findViewById(R.id.edit_text_id);
v.post(new Runnable() {
@Override
public void run() {
v.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
});
}
ls.illarionov answered 2020-08-10T01:54:52Z
0 votes
这就是对我有用的-
创建自己的EditText类并重写以下方法-
public class FocussableEditText extends EditText {
public FocussableEditText(Context context) {
super(context);
}
public FocussableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocussableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if (hasWindowFocus) {
InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
}
}
}
[HTTP://debugging is fun.blogspot.in/2014/08/Android-show-soft-keyboard.HTML]
v01d answered 2020-08-10T01:55:21Z
0 votes
我推荐以下链接,我遵循以下链接中提到的步骤,可以正常工作,这是一个很好的答案,只需非常简单的步骤即可完成。 功劳归于被回答的人。 希望它可以帮助在对话框活动中弄乱键盘弹出框的人。
DialogFragment并强制显示键盘
MohanRaj S answered 2020-08-10T01:55:46Z
0 votes
对我来说像这样在清单中添加以下行android:
windowSoftInputMode =“ stateVisible”
**
android:name=".mmmmm.zzzzzzz.yyyyyy.xxxxxx"
android:windowSoftInputMode="stateVisible"
android:label="@string/title_activity_print_recharge">
**
这就是我解决的方法
other Tall guy answered 2020-08-10T01:56:10Z
0 votes
这就是我所做的,效果很好...
您可以使用
Utilities.showSoftKeyboard(...)
您代码中的任何地方
public final class Utilities {
// Shows the soft keyboard. V on which view (typically EditText) to focus the keyboard input
public static void showSoftKeyboard(Activity activity, EditText editText)
{
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}
/**
* Called when user clicks/touches the search button
* @param v The clicked/touched view (EditText)
*/
protected void onSearchButtonClick(View v)
{
EditText seekTopic = (EditText) findViewById(R.id.SeekTopicBox);
setActivityContent(seekTopic.getText().toString());
}
的
onSearchButtonClick()
方法属于该活动,该活动包含要编辑的EditView,因此需要软键盘。
高温超导
Sold Out answered 2020-08-10T01:57:00Z