编辑文本不能正常工作android

时间:2015-10-11 00:05:45

标签: java android android-edittext

您好我正在尝试使用编辑文本进行列表视图,其中用户可以输入他想要的数字但是我添加第一个字符之后我的代码在编辑框之间跳转是否有办法阻止它?我尝试删除get focus并删除addTextWatcher。但是当我删除addtextwatcher时,会记录非文本。如果您需要更多信息,请告诉我,因为我对这个机器人生活有点新鲜。这是我的适配器代码: public CreateWKOAdapter(Context context,ArrayList items,ListView listview)     {         inflater = LayoutInflater.from(context);         this.mContext = context;         this.items = items;         this.listview = listview;     }

@Override
public int getCount()
{
    return items.size();
}

@Override
public CreateWKOItem getItem(int position)
{
    return items.get(position);
}

@Override
public long getItemId(int position)
{
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder = null;
    if (convertView == null)
    {
        holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.create_wko_layout_item, null);
        holder.name_text_view = (TextView) convertView.findViewById(R.id.text_name);
        holder.header = convertView.findViewById(R.id.relativeLayout1);
        holder.input = convertView.findViewById(R.id.oclick_layout);
        holder.image_view = (ImageView) convertView.findViewById(R.id.foto_createwko);
        holder.sets_edit_text = (CustomEditText) convertView.findViewById(R.id.sets_edit_text);
        holder.reps_edit_text = (CustomEditText) convertView.findViewById(R.id.rep_edit_text);
        holder.checkbox = (ImageView) convertView.findViewById(R.id.image_chk);

        holder.header.setTag(holder.input);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();

    }


    if (visibleindex == position)
    {
        holder.input.setVisibility(View.VISIBLE);
        if(holder.sets_edit_text != null)
        {
            final EditText clone = holder.sets_edit_text;
            holder.sets_edit_text.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    clone.requestFocus();
                    Log.i("su", "Request focus");
                }
            }, 300);
        }
    }
    else
    {
        holder.input.setVisibility(View.GONE);
    }

    CreateWKOItem item = getItem(position);

    if(item.isShouldShowCheckBox())
    {
        holder.checkbox.setVisibility(View.VISIBLE);
    }
    else
    {
        holder.checkbox.setVisibility(View.GONE);
    }
    String image_drawable_str = item.getName().toLowerCase().replace(" ", "_");
    Drawable drawable = getImageFromDrawableByString(image_drawable_str);
    if (drawable != null)
    {
        holder.image_view.setImageDrawable(drawable);
    }
    else
    {
        holder.image_view.setImageResource(R.drawable.bg_count);
    }

    holder.sets_edit_text.removeTextWatcher();
    holder.reps_edit_text.removeTextWatcher();

    holder.name_text_view.setText(item.getName());
    holder.sets_edit_text.setHint("" + item.getSets());
    holder.reps_edit_text.setHint("" + item.getReps());

    holder.header.setTag(holder.input);
    holder.header.setTag(R.id.tag_id, position);
    holder.header.setTag(R.id.view_id, holder.reps_edit_text);

    holder.sets_edit_text.setTag(item);
    holder.reps_edit_text.setTag(item);

    holder.sets_edit_text.addTextWatcher();
    holder.reps_edit_text.addTextWatcher();

    holder.header.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            final EditText editText = (EditText) v.getTag(R.id.view_id);

            final int a = (int) v.getTag(R.id.tag_id);
            visibleindex = a;

            View input = (View) v.getTag();
            if (input.getVisibility() == View.VISIBLE)
            {
                input.setVisibility(View.GONE);
                visibleindex = -1;
            }
            else
            {
                //input.setVisibility(View.VISIBLE);

            }
            notifyDataSetChanged();

        }
    });

    holder.sets_edit_text.setiOnTextChanged(new CustomEditText.IOnTextChanged()
    {
        @Override
        public void onTextChanged(View v, String newText)
        {
            CreateWKOItem it = (CreateWKOItem) v.getTag();
            int value = 0;
            try
            {
                String str = newText.toString();
                Log.i("su", "Sets text change to: " + str);
                if (str.length() > 0)
                    value = Integer.parseInt(str);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            if (it != null)
            {
                it.setSets(value);
                Log.i("su", "Current item set sets to: " + value);
            }
            notifyDataSetChanged();
        }
    });

    holder.reps_edit_text.setiOnTextChanged(new CustomEditText.IOnTextChanged()
    {
        @Override
        public void onTextChanged(View v, String newText)
        {
            CreateWKOItem it = (CreateWKOItem) v.getTag();
            int value = 0;
            try
            {
                String str = newText.toString();
                Log.i("su", "Reps text change to: " + str);
                if (str.length() > 0)
                    value = Integer.parseInt(str);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            if (it != null)
            {
                it.setReps(value);
                Log.i("su", "Current item set reps to: " + value);
            }
            notifyDataSetChanged();
        }
    });
    return convertView;

}

由于

2 个答案:

答案 0 :(得分:0)

简单的解决方案:保留TextBox监听器,并将括号留空。也许是Android?你可能正在看一个错误。

答案 1 :(得分:0)

我要做的是注释掉notifyDataSetChanged();但仅限于设定的代表。