java - Android:ListView 中的 EditText 问题

标签 java android

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

  1. 一些 EditText 需要数字输入,所以我将相应 xml 类型中的 inputType 设置为数字,但是,当我点击 EditText 时,数字键盘显示但几乎立即消失,常规键盘显示。

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

  3. 一开始我遇到了失去焦点的问题,但我通过查看其他问题解决了这个问题(将 descendantFocusablity="beforeDescendants"添加到 ListView 和 windowSoftInputMode="adjustPan")。工作正常,除了当 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;
    }
    

最佳答案

问题 #3 的解决方案:

  1. 在 ListView 中设置 descendantFocusablity="beforeDescendants"

  2. 在 list 中设置windowSoftInputMode="adjustPan"

  3. 设置listview.setItemsCanFocus(true)

关于java - Android:ListView 中的 EditText 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17385890/

相关文章:

android - 空白警报对话框

java - SharedPreferences 是否将传递的 Set<T> 复制到另一种类型?

java - oracle ordim 为图像添加水印

java - 在相对布局中动态添加 Imageview 后,图像不会调整大小

android - 我怎样才能运行多台安迪机器

android - 如何捕捉系统广播 BOOT_COMPLETED,我的程序不工作?

java - 从 URL 的数组中获取数组

java - Jetty Websocket 连接 - 忽略自签名证书

用于检查 JVM 选项的 Java 代码

android - 画廊在方向更改后调用 onItemSelected