【问题标题】:Android: EditText in ListView issueAndroid:ListView 中的 EditText 问题
【发布时间】:2013-06-27 11:27:27
【问题描述】:

我的应用程序中有一个列表视图,它基本上是一个问卷,因此用户需要填写一些 EditText。我遇到了以下问题:

  1. 某些 EditText 需要数字输入,因此我将相应 xml 类型中的 inputType 设置为数字,但是,当我单击 EditText 时,会显示数字键盘,但它几乎立即消失并且常规键盘显示出来了。

  2. 如您所想,我需要将用户输入的值存储在这些 EditText 中。我遇到的问题是,在我在一些 EditTexts 中输入一些文本并向下滚动后,当我返回时,文本就消失了。你可以在我的代码中看到我试图阻止这种情况,我应该提到它与复选框完美配合。

  3. 一开始我遇到了失去焦点的问题,但我通过查看其他问题解决了它(在 ListView 和 windowSoftInputMode="adjustPan" 中添加了 descendantFocusablity="beforeDescendants")。工作正常,除了当 EditText 低于屏幕的一半时,一旦我开始输入它就会失去焦点。 列表适配器的getView方法:

    public View getView(final int position,View result,ViewGroup parent)
    {
    final ViewHolder vh;
    final Question question = values.get(position);
    if(holders[position]==null)
        vh = new ViewHolder();
    else
        vh = holders[position];
    
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(question.getQuestionType().equals(Question.TYPE_CLOSED))
    {
        result = inflater.inflate(R.layout.closed_question_list_item, null);
        vh.cb  = (CheckBox)result.findViewById(R.id.checkbox);
        vh.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                vh.isChecked      = isChecked;
                holders[position] = vh;
            }
        });
        vh.cb.setChecked(vh.isChecked);
    }
    
    else
    {
        result = inflater.inflate(R.layout.numeric_question_list_item, null);
        vh.et  = (EditText)result.findViewById(R.id.question_et);
        vh.et.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void afterTextChanged(Editable s) 
            {
                vh.tvValue = s.toString();
                holders[position] = vh;
                Log.i(TAG,"Entered afterTextChanged for "+ question.getText());
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start,int count, int after) 
            {   
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) 
            {
            }
        });
        vh.et.setText(vh.tvValue);
    }
    vh.tv = (TextView)result.findViewById(R.id.question_text);
    
    vh.tv.setText(question.getText());
    
    holders[position] = vh;
    result.setTag(vh);
    return result;
    }
    

【问题讨论】:

标签: java android


【解决方案1】:

问题 #3 的解决方案:

  1. 在列表视图中设置descendantFocusablity="beforeDescendants"

  2. 在清单中设置windowSoftInputMode="adjustPan"

  3. 设置listview.setItemsCanFocus(true)

【讨论】:

【解决方案2】:

要扩展aaRBiyecH 的答案above,这里有一个解决方案和对您的问题#1 和#3 (source) 的更深入解释:

您需要将 ListView 的 DescendantFocusability 属性设置为“afterDescendants”。

您可以像这样在 XML 中执行此操作:

android:descendantFocusability="afterDescendants"

或在这样的代码中:

listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS)

您还需要将项目设置为可聚焦。

您可以在如下代码中执行此操作: listview.setItemsCanFocus(true);

此外,您还需要指定在显示软键盘时 Activity 的主窗口发生的情况。这是通过更改WindowSoftInputMode 属性来完成的。您可能希望将其设置为 AdjustPan 以便您的列表视图不会调整大小,因此不会再次调用 GetView。这可能是现在隐藏键盘时发生的情况,再次调用 GetView 并重置 EditText 中的原始值。

您可以使用主要活动类的 XML 上的属性 android:windowSoftInputMode="adjustPan" 或以编程方式使用 getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_PAN) 来执行此操作。

【讨论】:

    【解决方案3】:

    由于问题 2,您可以与这篇文章相关联 EditText items in a scrolling list lose their changes when scrolled off the screen 所以当你向下滚动时,似乎没有简单的解决方案来保留编辑文本

    是的,你可以试试

    EditText loses content on scroll in ListView

    或只是与右侧的相关帖子相关

    【讨论】:

    • 谢谢,但所有答案都是告诉你自己回收行,但它没有解释如何有效地做到这一点,这是我想知道的。你知道我在哪里可以找到这样的东西吗?
    • 谢谢,我去看看。无论如何,我设法解决了这个问题,结果证明使用 vh 局部变量和 holder 数组有点多余,所以我尝试删除所有这些冗余,它工作得很好。
    【解决方案4】:

    它对我有用....请检查这个

    listview.setItemsCanFocus(true);
    

    【讨论】:

      【解决方案5】:

      我有一个带有 Edittext 的列表视图,当我在其中输入文本时,如果在此之前,我滚动列表视图,适配器会占据一些位置,而不是选定的位置。

      我的适配器占据了不同的位置,而不是我的选择:

      etCantidad.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
           @Override
           public void onTextChanged(CharSequence s, int start, int before, int count) {}
      
           @Override
           public void afterTextChanged(Editable s) {
               String cantidadTMP = etCantidad.getText().toString();
               int cantidad;
               try {
                    cantidad = Integer.parseInt(cantidadTMP);
                    item.setCantidad(cantidad);
                    Log.i("Name",""+item.getNombre());
                   }catch (Exception e){
                          cantidad = 0;
                   }
               Log.i("QuantityArrayAdapter",String.valueOf(cantidad));
           }});
      

      一个编辑文本选择:

      09-25 09:22:10.458 12719-12719/? I/QuantityArrayAdapter: 1
      09-25 09:22:10.458 12719-12719/? I/QuantityArrayAdapter: 1
      09-25 09:22:10.458 12719-12719/? I/QuantityArrayAdapter: 1
      

      所以,我在 onTouchListener 中添加了这个方法,只获取选择位置:

      etCantidad.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  etCantidad.addTextChangedListener(new TextWatcher() {
                      @Override
                      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                      }
      
                      @Override
                      public void onTextChanged(CharSequence s, int start, int before, int count) {
                      }
      
                      @Override
                      public void afterTextChanged(Editable s) {
                          String cantidadTMP = etCantidad.getText().toString();
                          int cantidad;
                          try {
                              cantidad = Integer.parseInt(cantidadTMP);
                              item.setCantidad(cantidad);
                              Log.i("Name",""+item.getNombre());
                          }catch (Exception e){
                              cantidad = 0;
                          }
                          Log.i("QuantityArrayAdapter",String.valueOf(cantidad));
                      }
                  });
                  return false;
              }
          });
      

      只拿我想要的东西,它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-24
        相关资源
        最近更新 更多