Eri*_*ric 29 android focus android-edittext
我一直在设计一个包含可扩展列表的应用程序.在每个列表的末尾,空白EditText已准备好接收注释.我有以下问题; 当我触摸时EditText,屏幕稍微调整大小(不是问题因为调整大小并不总是发生,这取决于我的布局和EditText列表中的位置)并且出现软键盘.然而,在这些事件中,EditText失去焦点并且不会重新获得它.这是非常不方便的,因为没有焦点,尽管键盘可用,键盘输入无法到达EditText.一旦我再次触摸它,EditText行为就像预期的那样.
它变得更加陌生.键盘仍然显示,我可以触摸不同EditText的相同的情况; 它失去了焦点.然而,一旦我通过最初失去焦点,我可以在之前触摸的EditTexts 之间自由移动而没有任何焦点相关的问题.只有当我隐藏软键盘时,"状态"才会消失,我需要点击EditText两次才能编辑它们.
以下是我的代码的摘录.一,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>
Run Code Online (Sandbox Code Playgroud)
这里是我使用相关布局的片段的摘录:
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;
}
});
Run Code Online (Sandbox Code Playgroud)
我没有在清单文件中指定任何特定输入,但如果认为有用,我可以提供.
更新:
添加一行来Activity调整平移android:windowSoftInputMode="adjustPan"可以部分解决问题.它将EditText视图可能被软键盘隐藏的问题替换当前的意外行为.
到目前为止,还没有找到理想的解决方案,但接近了.看看接受的答案中的评论,它们可能对您有用!
jlo*_*pez 43
您需要更改AndroidManifest.xml
添加android:windowSoftInputMode="adjustPan"包含列表视图的活动.这将解决您的问题.
<activity android:name=".MyEditTextInListView"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan">
Run Code Online (Sandbox Code Playgroud)
一个解决方案是发布一个延迟的runnable,它检查EditText视图是否仍然是聚焦的,如果不是,则将其聚焦.
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(final View v, boolean hasFocus) {
if (hasFocus) {
// Request focus in a short time because the
// keyboard may steal it away.
v.postDelayed(new Runnable() {
@Override
public void run() {
if (!v.hasFocus()) {
v.requestFocus();
}
}
}, 200);
}
}
});
Run Code Online (Sandbox Code Playgroud)