2013-08-21 79 views
1

的奇怪的错误,我得到这个应用程序(也就是不如顺便说一句做),我需要一个的LinearLayout视图动态地添加到的LinearLayout容器。它的工作方式是用户在EditText中写入内容并按下键盘上的“返回”键。当检测到返回键事件时,带有另一个EditText视图的LinearLayout直接添加到用户刚编辑的视图下,焦点切换到刚添加的LineaLayout内的EditText视图。这将在里面创建一个带有EditText视图的LinearLayouts垂直列表(请参阅下面的视频以获取插图)。它在我的两个测试设备上以及我的小型alpha测试组的五个不同设备上运行良好,我似乎无法在模拟器中重新创建该错误。然而,当测试我老板的设备时(他在另一个国家,所以我不能向他展示我的设备),新的EditText视图从不会出现,并且写在当前EditText视图中的文本以非常错误的方式消失见下面的视频)。涉及动态添加的LinearLayout视图的LinearLayout容器

Here是一个bug的视频。预期的行为是插入另一个带有两个EditText视图的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(2.3.3的Android)
  2. 三星Nexus S(的Android 4.1.2)

设备,其中应用程序正在按预期工作:

  1. HTC One S(Android 4.1.1)
  2. 三星I9000 Galaxy S的(安卓2.3.5)
  3. 三星I9100 GALAXY S II(安卓2.3.4)
  4. 的HTC One X(Android 4.0版本)
  5. 谷歌Nexus 4(的Android 4.2,我认为)
  6. 自动真空淀积系统设置为两个设备显示我正在寻找一些建议,以找到是什么原因造成这个错误,或只是一些建议,以重现错误的bug

。谢谢。

回答

1

那么通过视频判断问题可能发生在这条线上,在这些设备上返回false。

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

尝试这样做

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

或者这

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

我的理论是,这些设备处理键盘事件的不同不知?

+1

感谢您的反馈。您可能是对的(现在上传更改以进行测试)。我从未想到'文本消失'只是在EditText视图中创建新行的返回键,我认为这是一种奇怪的错误。 – AHaahr

+0

该死的,没有工作。确信就是这样。不过,我认为你是对的,问题是正确地捕捉关键事件。 – AHaahr

相关问题