android - 涉及将 LinearLayout View 动态添加到 LinearLayout 容器的奇怪错误

标签 android android-linearlayout android-edittext

我得到了这个应用程序(顺便说一句,它和完成的一样好),我需要在其中动态地将 LinearLayout View 添加到 LinearLayout 容器。它的工作方式是用户在 EditText 中写一些东西然后按下键盘上的“返回”键。当检测到返回键事件时,直接在用户刚刚编辑的下方添加一个带有另一个EditText View 的LinearLayout,焦点切换到刚刚添加的LineaLayout中的EditText View 。这将创建一个 LinearLayouts 的垂直列表,其中包含 EditText View (有关说明,请参见下面的视频)。它在我拥有的两个测试设备以及我的小型 alpha 测试组的五个不同设备上都运行良好,我似乎无法在模拟器中重现该错误。但是,当在我老板的设备上进行测试时(他在另一个国家,所以我不能只给他看我的设备),新的 EditText View 永远不会出现,并且当前 EditText View 中写入的文本以非常错误的方式消失(再次请看下面的视频)。

Here是该错误的视频。预期的行为是插入另一个带有两个 EditText View 的 LinearLayout(并显示“2”而不是“1”),但是如您所见,新的 LinearLayout 不会出现并且文本已从当前 EditText 中删除看法。如果需要,我也可以录制预期行为的视频。

代码如下所示:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //Create a listener for the item fields
    TextView.OnEditorActionListener itemListener = new OnItemEditorListener();
    nextItem.setOnEditorActionListener(itemListener);
    nextItem.addTextChangedListener(new ListTextWatcher());
}

private LinearLayout addLine(int i, String text){
    LinearLayout line = createline(R.layout.list_item_add);
    EditText listTextView = (EditText) line.getChildAt(1); 
    if(!text.equals(""))
        listTextView.setText(text);
    itemContainer.addView(line);
    int index = i + 1;
    ((EditText) line.getChildAt(0)).setText(index + ".");
    listTextView.setOnEditorActionListener(new OnItemEditorListener());
    listTextView.addTextChangedListener(new ListTextWatcher());
    return line;
}

private class OnItemEditorListener implements TextView.OnEditorActionListener {
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        //Check if the current action is a keyDown event on the return button of the keyboard
        if(actionId == EditorInfo.TYPE_NULL && event.getAction() == KeyEvent.ACTION_DOWN){
            final EditText currentView = (EditText) view;
            //if there is no text in the current field, do nothing
            if(currentView.getText().toString().equals(""))
                return true;
            int childCount = itemContainer.getChildCount();
            LinearLayout newLine = null;
            //iterate through all existing item lines 
            for(int i = 0; i < childCount; i++){
                LinearLayout currentLine = (LinearLayout) itemContainer.getChildAt(i);
                //Check if the current iteration is looking at the line that had focus when return was pressed
                if(currentView.equals(currentLine.getChildAt(1))){
                    //check if the current line is the last item, and if so add a new line
                    if(i == childCount - 1)
                        newLine = addLine(i + 1, "");
                    //if current line is not the last line, give focus to the next line
                    else
                        newLine = (LinearLayout) itemContainer.getChildAt(i + 1);
                    break;
                }
            }
            nextItem = (EditText) newLine.getChildAt(1);
            nextItem.requestFocus();
        }
        return true;
    }
}

这是添加的 LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:orientation="horizontal"
    android:weightSum="1" >

<EditText
    android:id="@+id/item_index"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_weight="0.15"
    android:background="@drawable/round_corners_left_white_background"
    android:enabled="false"
    android:gravity="center"
    android:inputType="none"
    android:text="@string/one_dot"
    android:textColor="@color/ts1"
    android:textSize="18sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/list_item"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_weight="0.85"
    android:background="@drawable/round_corners_right_white_background"
    android:hint="@string/new_item"
    android:lines="1" />
</LinearLayout>

这非常令人沮丧,因为只要我无法重新创建它,我就无法解决这个错误。如前所述,我可以访问的所有设备都不会以这种方式运行,而且我也无法让模拟器执行此操作。

显示错误的两个设备:

  1. 索尼爱立信 Xperia PLAY (Android 2.3.3)
  2. 三星 Nexus S(Android 4.1.2)

应用正常运行的设备:

  1. HTC One S(安卓 4.1.1)
  2. 三星 I9000 盖乐世 S(Android 2.3.5)
  3. 三星 I9100 Galaxy S II(Android 2.3.4)
  4. HTC One X(安卓 4.0)
  5. Google Nexus 4(我认为是 Android 4.2)
  6. AVD 设置为显示错误的两个设备

我正在寻找一些建议来找出导致这个错误的原因,或者只是一些关于重新创建错误的建议。谢谢。

最佳答案

好吧,根据视频判断,问题可能出在这行在这些设备上返回 false 的地方。

 if(actionId == EditorInfo.TYPE_NULL && event.getAction() == KeyEvent.ACTION_DOWN){

尝试这样做

 if(event.getAction() == KeyEvent.ACTION_DOWN){

或者这个

 if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER){

我的理论是这些设备以某种方式以不同方式处理键盘事件?

关于android - 涉及将 LinearLayout View 动态添加到 LinearLayout 容器的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18366745/

相关文章:

android - 弹出堆栈抛出 "Fragment Already Added"异常

java - Android ICS 兼容性

android - 如何对方法进行全局覆盖?

android - 如何更改 scrollview 的高度以不重叠 android 中的线性布局?

android - 操作栏不与 ListView 一起出现

android - 如何以编程方式将 EditText 的 InputType 设置为整数或小数?

java - 在 Android 中调用电话

android - 以编程方式设置 Imageview 的高度和宽度

android - 在 Android 弹出窗口中输入数据

Android - 状态通知的EditText