我在应用程序中有一个列表视图,基本上是一个问卷,所以有一些EditText需要用户填写。我遇到了以下问题:
一些EditText需要数字输入,因此我在相应的xml类型中设置了inputType为numeric,但是当我点击EditText时,数字键盘出现后几乎立即消失,普通键盘显示。
可以想象,我需要存储用户输入这些EditText的值。我的问题是,在我输入某些文本到SOME EditTexts中并滚动后,当我返回时文本就会消失。您可以在我的代码中看到,我尝试防止这种情况发生,我应该提到它在复选框方面完全正常。
一开始我遇到了失去焦点的问题,但通过查看其他问题得到解决(将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;
}