2013-06-30 47 views
9

我在我的应用程序中有一个基本上是问卷调查的列表视图,所以有一些用户需要填写的EditTexts。我遇到了以下问题:Android:在ListView问题中的EditText

  1. 一些EditText上都需要数字输入,所以我设置的inputType在相应的XML类型的数字,但是,当我点击的EditText如图所示的数字键盘但几乎立即消失,并显示常规键盘。

  2. 正如你可以想象的,我需要存储用户在这些EditTexts中输入的值。我遇到的问题是,在我输入了一些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; 
    } 
    
+0

也许这可以帮助你 http://stackoverflow.com/questions/8772714/edittext-loses-content-on-scroll-in-listview?rq=1 – JahShaka

+0

你是怎么解决问题#3?如果解决了,请发表回答 – desidigitalnomad

回答

0

由于问题2你可以涉及到这个帖子 EditText items in a scrolling list lose their changes when scrolled off the screen 所以似乎有没有简单的解决方案时,您向下滚动

坚持的edittexts

是的你的权利你可以试试

EditText loses content on scroll in ListView

或只是涉及到相关的职位右侧

+1

谢谢,但所有的答案都是告诉你自己回收行,但它没有解释如何有效地做到这一点,这是我想知道的。你会不会知道我在哪里可以找到类似的东西? – user2535545

+0

http://stackoverflow.com/questions/8772714/edittext-loses-content-on-scroll-in-listview?rq=1 – JahShaka

+0

谢谢,我会看看它。无论如何,我设法解决它,结果使用vh局部变量和持有人阵列是有点冗余,所以我试图消除所有冗余,它工作得很好。 – user2535545

18

解决方案问题3:

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

  2. 在清单

  3. 设置listview.setItemsCanFocus(true)

+0

您的解决方案是唯一真正有效的解决方案!谢谢! – Valdrinit

+0

你做了一个工作的人。谢谢!!! –

+1

谢谢。 1. android:descendantFocusability =“afterDescendants”。 2.编写像这样输入文本的保存:http://stackoverflow.com/a/19487574/2914140或在这里:http://stackoverflow.com/a/21404201/2914140。 – CoolMind

4

设置windowSoftInputMode="adjustPan"要扩大aaRBiyecH的回答above,这里是一个解决方案和更深入的解释您的问题#1 #3(source):

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

你可以在这样的XML做到这一点:

android:descendantFocusability="afterDescendants"

或在这样的代码:

listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS)

您还需要设定的项目要成为焦点。

你可以这样的代码这样做: listview.setItemsCanFocus(true);

另外,您需要指定显示软键盘时活动主窗口发生了什么。这是通过更改WindowSoftInputMode属性完成的。您可能希望将其设置为AdjustPan,以便您的列表视图不会调整大小,从而不会再调用GetView。这可能是当您隐藏键盘时发生的事情,GetView被再次调用并重置EditText中的原始值。

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

0

它的工作对我来说....请检查这个

listview.setItemsCanFocus(true); 
0

我有一个EDITTEXT ListView和当它我输入文本,如果在此之前,我滚动列表视图,适配器需要一些位置,而不是选中的。

我的适配器采取各种位置,而不是我的选择:

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)); 
    }}); 

一个Editext选择:

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; 
     } 
    }); 

而只把我想要的项目,它的作品给我。