请解释一下软键盘的问题。

例如,我在我的活动或对话片段或片段活动上有一个EditText,无论如何。

这里是:

android:id="@+id/edPswrd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword">

当它第一次显示我没有看到软键盘并且必须按editText才能获得焦点并且键盘出现。 另一个活动是不同的,当它出现在屏幕上时,键盘在没有任何帮助的情况下加载。

我以为

< requestFocus />

意味着EditText将被聚焦并且键盘将出现,但我错了。

如何管理哪些组件将获得焦点,键盘将自动显示。

我发现最好不要在TextEdit上检查Focusable。

如何在关注edittext时显示软键盘可能重复

我认为这是一个错误或一个功能,它试图向你展示整个活动,而不是先用软键盘遮住它。我曾经搜索过一次有关这方面的信息,但遗憾的是没有发现任何真正可靠的来源。

无论如何,要显示软键盘,您可以这样做:

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

如果你想隐藏软键盘,你可以这样做:

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

我尝试过,但这里的方法不适用于我的dialogfragment。我这样做了:InputMethodManager imm =(InputMethodManager)getActivity()。getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(edPswrd,InputMethodManager.SHOW_IMPLICIT);它不起作用:(

@Foenix请看编辑DialogFragment :)

谢谢。我导入了android.view.ViewGroup.LayoutParams但仍有错误SOFT_INPUT_STATE_VISIBLE无法解析或不是字段

我也做了这个:getActivity()。getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);比如下面的建议,但键盘仍然没有出现

我也试过你在其他活动上"??隐藏键盘",写了这个InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(),0);在onCreate()但键盘出现:(

我也让它在onCreateDialog上工作。看看我的回复

在你关闭对话框后如何让键盘消失?如果使用getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);,即使在关闭对话框后键盘也会保持不变。

打开Android Manifest文件。

寻找像这样的活动标签

android:name="com.example.framework.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateVisible"       //Add this line
>

添加android:windowSoftInputMode ="stateVisible"行,如上所示

好的,谢谢,它适用于活动,它不适合对话。

那个viewpager怎么样?!

我知道这已经得到了解答,但我找到了一种方法来在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;

我不得不使用SOFT_INPUT_STATE_ALWAYS_VISIBLE来显示键盘(Nexus 5)。无论如何,感谢onCreateView()方法中的解决方案。

如果使用这种显示键盘的方法,如何关闭键盘?

一如往常。检查这个和这个

添加到onCreate或onStart();

myView.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

我有一个dialogfragment并做了这个:edPswrd.requestFocus(); getActivity()getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)。;它不起作用:(

哪一部分,重点放在dialogfrag或keyboad上?

出现对话框时屏幕上仍然没有键盘

它的工作方式如下:getDialog()。getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);谢谢!

试试这个

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

这就是我做的,它很好用......

你可以使用

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()

方法属于activity,它包含我想要编辑的EditView,因此我需要软键盘..

HTH

对我来说,在你的清单中添加这行android:windowSoftInputMode ="stateVisible"就像这样

**
android:name=".mmmmm.zzzzzzz.yyyyyy.xxxxxx"
android:windowSoftInputMode="stateVisible"
android:label="@string/title_activity_print_recharge">
**


这就是我解决它的方式

我推荐以下链接,我按照下面提到的步骤链接,完美的工作,这是非常好的答案,完成非常简单的步骤。信用归于回答的人。希望它能帮助那些搞砸键盘弹出对话活动的人。

DialogFragment和强制显示键盘

这对我有用 -

创建自己的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://debuggingisfun.blogspot.in/2014/08/android-show-soft-keyboard.html