我正在使用输入框AlertDialog.在EditText该对话框中本身自动对焦,当我打电话AlertDialog.show(),但软键盘未自动显示.
如何在显示对话框时自动显示软键盘?(并且没有物理/硬件键盘).与按下"搜索"按钮调用全局搜索的方式类似,将自动显示软键盘.
所以我有一个似乎常见的问题,就是我的对话框中的EditText在获得焦点时不会显示出来.我已经看过几个解决方法,比如在这个帖子中,这一个和这一个(还有更多),但我从来没有见过一个令人满意的解释,为什么这首先发生.
我更倾向于让Android使用自己的EditTexts默认行为,而不是构建我自己的行为,但似乎每个人(在那些线程中)已经接受了Dialogs中EditTexts的默认行为是只给光标而没有键盘.那为什么会这样?
为了记录,这些解决方法似乎都没有对我有用 - 我最接近的是强制键盘出现在对话框下面(使用InputMethodManager.toggleSoftKeyboard(*)).我的特殊配置是API15,EditText显示在AlertDialog中ListView的页脚中.设置了EditText android:focusable ="true",onFocusChangeListener 正在接收焦点事件.
编辑:
根据要求,这是我正在使用的特定代码段.我不打算整个布局,但在这个特定的应用程序中,EditText响应于按下对话框上的按钮而出现(类似于动作视图).它包含在RelativeLayout中,默认情况下可见性"已消失":
<RelativeLayout
android:id="@+id/relLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="gone"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<ImageButton
android:id="@+id/cancelBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@color/transparent"
android:src="@drawable/cancelButton"
android:layout_margin="5dp"/>
<ImageButton
android:id="@+id/okBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/cancelBut"
android:background="@color/transparent"
android:src="@drawable/okButton"
android:layout_margin="5dp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:focusable="true"
android:layout_toLeftOf="@id/okBut"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
构建它的代码将relativeLayout的可见性设置为"Visible"(并隐藏其他UI元素).根据我使用EditText的经验,这应该足以在EditText聚焦时拉起键盘.但是,出于某种原因,情况并非如此.我可以在onFocusChangeListener上设置以下内容:
edit_text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// For whatever reason we need to request …Run Code Online (Sandbox Code Playgroud) 我正在使用Jake Wharton出色的ActionBarSherlock库并具有可折叠的搜索操作视图.我想在展开搜索操作视图时弹出软键盘.
我们可以在Google的" Using DialogFragments "博客文章中阅读一个推荐的方法,在DialogFragment中进行此操作(为了便于阅读,稍作修改).
// Show soft keyboard automatically
mEditText.requestFocus();
int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getDialog().getWindow().setSoftInputMode(mode);
Run Code Online (Sandbox Code Playgroud)
在扩展和请求关注可折叠的EditText操作视图时,这似乎不起作用.我现在有这个.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.my_activity, menu);
MenuItem menuItem = menu.findItem(R.id.menu_search);
final EditText searchText = (EditText) menuItem.getActionView();
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchText.requestFocus();
int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getWindow().setSoftInputMode(mode);
return true; // …Run Code Online (Sandbox Code Playgroud) android android-softkeyboard android-menu actionbarsherlock android-actionbar