出现键盘时,EditText失去焦点;需要触摸两次才能进行编辑 [英] EditText loses focus when keyboard appears; requires touching twice to edit
问题描述
我一直在设计一个包含可扩展列表的应用程序.在每个列表的末尾,一个空的EditText
准备接收评论.我有以下问题;当我触摸EditText
时,屏幕会略微调整大小(这并不成问题,因为调整大小并不总是会发生,具体取决于我的布局和列表中EditText
的位置),并出现软键盘.但是,在这些事件中,EditText
会失去焦点,并且不会重新获得焦点.这非常不方便,因为没有焦点,尽管有键盘可用,但键盘输入仍未到达EditText
.再次触摸它后,EditText
的行为就会像预期的那样.
I have been designing an application which holds an expandable list. At the end of every list, an empty EditText
is ready to receive comments. I have the following problem; when I touch the EditText
, the screen resizes slightly (not a problem as the resizing does not always happen, depending on my layout and the position of the EditText
int he list) and the soft-keyboard appears. However, during these events, the EditText
loses focus and does not regain it. This is quite inconvenient as without focus, despite the keyboard being available, the input from the keyboard does not reach the EditText
. Once I touch it again, the EditText
behaves as expected.
它变得更加陌生.在仍然显示键盘的情况下,我可以触摸不同的EditText
,并且会发生同样的情况.它失去焦点.但是,一旦我通过了最初的失去焦点,就可以在先前接触过的EditText
之间自由移动,而不会遇到与焦点有关的任何问题.仅当我隐藏软键盘时,状态"才会消失,并且需要先点按两次EditText
才可以对其进行编辑.
It gets even stranger. With the keyboard still displayed, I can touch a different EditText
and the same happens; it loses focus. However, once I passed the initial loss of focus, I can move freely between previously touched EditText
s without any focus related issues. Only when I hide the soft-keyboard will the "state" disappear and I would need to tap EditText
s twice before being able to edit them.
以下是我的代码的摘录.首先,empty_comment.xml
:
Here are excerpts from my code. First, the empty_comment.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/blank_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:imeOptions="actionDone"
android:hint="@string/hint"
android:layout_marginLeft="30dip" >
</EditText>
</LinearLayout>
以下是我使用相关布局的片段摘录:
Here an excerpt from the fragment where I use the layout in question:
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_empty, null);
final EditText editText = (EditText) convertView.findViewById(R.id.blank_box);
editText.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
// Unrelated code
}
return true;
}
});
我没有在清单文件中指定任何特定的输入,但是如果认为有帮助,我可以提供它.
I am not specifying anything input specific in my manifest file but I could provide it if deemed helpful.
更新:
在Activity
中添加一条线以调整声相android:windowSoftInputMode="adjustPan"
确实可以部分解决问题.它用EditText
视图可能被软键盘隐藏的问题代替了当前的意外行为.
Adding a line to the Activity
to adjust the pan android:windowSoftInputMode="adjustPan"
does partially solve the problem. It replaces the current unexpected behaviour with the problem that EditText
views might get hidden by the soft-keyboard.
到目前为止,还没有找到理想的解决方案,但是已经找到了解决方案.看看已接受答案中的评论,它们可能对您有用!
As of now, no ideal solution has been found but approached. Have a look at the comments in the accepted answer, they might prove useful to you!
推荐答案
您需要在AndroidManifest.xml中进行更改
You need to change in your AndroidManifest.xml
在包含列表视图的活动中添加android:windowSoftInputMode="adjustPan"
.这样可以解决您的问题.
Add android:windowSoftInputMode="adjustPan"
in the activity holding the listview. This will solve your problem.
<activity android:name=".MyEditTextInListView"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan">
这篇关于出现键盘时,EditText失去焦点;需要触摸两次才能进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!