安卓的EditText中的ListView问题 [英] Android: EditText in ListView issue

查看:176
本文介绍了安卓的EditText中的ListView问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 部分的EditText都需要数字输入,所以,不管我在相应的XML类型数值设置inputType,因为当我点击的EditText的数字键盘被显示,但几乎立即消失和普通键盘图所示。

  2. 正如你可以想像,我需要存储的值,在这些EditTexts用户输入。这个问题我已经与这是在我输入一些EditTexts一些文字,我向下滚动,文本消失了,当我回去。你可以在我的code,我尝试去prevent这一点,我应该指出,这工作完全正常使用复选框见。

  3. 目前,我有问题失去了焦点,但我解决它寻找其他的问题(加descendantFocusablity =beforeDescendants到ListView和windowSoftInputMode =adjustPan)开始。工作正常,只是,当一个EditText低于它失去,一旦集中,我开始输入到它的屏幕的一半。 getView方法列表适配器:

     公开查看getView(最终诠释的位置,查看结果,父母的ViewGroup)
    {
    最后ViewHolder VH;
    最后一个问题的问题= values​​.get(位置);
    如果(持有者[位置] == NULL)
        VH =新ViewHolder();
    其他
        VH =持有人[位置]
    
    LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    如果(question.getQuestionType()。等于(Question.TYPE_CLOSED))
    {
        结果= inflater.inflate(R.layout.closed_question_list_item,NULL);
        vh.cb =(复选框)result.findViewById(R.id.checkbox);
        vh.cb.setOnCheckedChangeListener(新OnCheckedChangeListener()
        {
    
            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked)
            {
                vh.isChecked =器isChecked;
                持有人[位置] = VH;
            }
        });
        vh.cb.setChecked(vh.isChecked);
    }
    
    其他
    {
        结果= inflater.inflate(R.layout.numeric_question_list_item,NULL);
        vh.et =(EditText上)result.findViewById(R.id.question_et);
        vh.et.addTextChangedListener(新TextWatcher()
        {
            @覆盖
            公共无效afterTextChanged(编辑S)
            {
                vh.tvValue = s.toString();
                持有人[位置] = VH;
                Log.i(TAG,输入afterTextChanged为+ question.getText());
            }
    
            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT)
            {
            }
    
            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数)
            {
            }
        });
        vh.et.setText(vh.tvValue);
    }
    vh.tv =(TextView中)result.findViewById(R.id.question_text);
    
    vh.tv.setText(question.getText());
    
    持有人[位置] = VH;
    result.setTag(VH);
    返回结果;
    }
     

解决方案

解决方案发出#3:

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

  2. 设置 windowSoftInputMode =adjustPan在清单

  3. 设置 listview.setItemsCanFocus(真)

I have a listview in my application that is basically a questionaire so there are some EditTexts that the user needs to fill. I've encountered the following issues:

  1. Some of the EditText require numeric input, so I set the inputType in the corresponding xml type as numeric, however, as when I click on the EditText the numeric keyboard is shown but almost immediately it disappears and the regular keyboard is shown.

  2. As you can imagine, I need to store the values that the user inputs in these EditTexts. The problem I have with this is that after I input some text in SOME EditTexts and I scroll down, the text is gone when I go back. You can see in my code that I made an attempt to prevent this and I should mention that it works perfectly fine with checkboxes.

  3. At the beginning I was having problems with losing the focus but I solved it looking at other questions (added descendantFocusablity="beforeDescendants" to the ListView and windowSoftInputMode="adjustPan"). Works fine except that when an EditText is below the half of the screen it loses focus as soon as I start typing into it. getView method for list adapter:

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

解决方案

Solutions to issue #3:

  1. Set descendantFocusablity="beforeDescendants" in the listview.

  2. Set windowSoftInputMode="adjustPan" in the manifest

  3. Set listview.setItemsCanFocus(true)

这篇关于安卓的EditText中的ListView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆