gpt4 book ai didi

java - Android:ListView 中的 EditText 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:24 24 4
gpt4 key购买 nike

我的应用程序中有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com