0

I have Gridview in my activity. Data adapter for my GridView:

private class EfficientAdapter extends BaseAdapter {
        ...
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item3, null);
                holder = new ViewHolder();
                holder.Attitude_Value = (EditText) convertView.findViewById(R.id.editText1);
                holder.Attitude_Value.addTextChangedListener(new TextWatcher()
                    {
                        public void afterTextChanged(Editable edt) 
                        {attitude_values.set(holder.ref, edt.toString());}

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3){}
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}});
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.ref=position;
            holder.Attitude_Value.setText(attitude_values.get(position));
            return convertView;
        }
   ...

How can I clear editbox in grid view, when I focus in it, before start typing?

Ivan
  • 191
  • 1
  • 2
  • 11

2 Answers2

0

Did you try like this way i am not so sure change your XML Edittext

android:hint="Something"

try this link example

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        et.setCompoundDrawables(null, null, et.getText().toString().equals("") ? null : x, null);
    }

    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
});
Community
  • 1
  • 1
Janmejoy
  • 2,721
  • 1
  • 20
  • 38
0
String value = "anyValue";

final EditText et = (EditText) findViewById(R.id.edtText);
et.setText(value);
et.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_UP) {
            et.setText(et.getText().toString.trim());
            return false;
        }

        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            value = et.getText().toString.trim();
            et.setText("");
            return true;
        }
        return false;
    }
});

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
});
  • I try, but actually I can't understand how to do it. We can't see holder from TextWatcher()? – Ivan Jan 25 '13 at 06:56
  • @Ivan Onishchenko Now try this code. In `OnTouchListener` when you get event action `ACTION_DOWN` get the current value and clear the `EditText` and if user can not edit the current value and just loose the focus just set that value back otherwise set the new text. –  Jan 25 '13 at 07:23
  • thank you! It work when I touch, but does't when I push enter, end move by table. Could you advice method for that? And I add et.RequestFocus(), be cause cells hasn't focus – Ivan Jan 25 '13 at 07:39
  • Ivan If you are talking about to use enter key from softkeybaord, you need to bind listener for that. Like `editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_ENTER) { //Do whatever you want return true; } return false; } });` –  Jan 25 '13 at 07:57
  • Little problem, when I push enter, clear cels, where was focus before, and then move to next cells – Ivan Jan 25 '13 at 08:28
  • In Adapter you need to manage thing on `OnTouch` of `EditText`. When you touch get the old value store it and enter the new one or set the old value on `EnterKey` press. On Next Cell same thing will be done. –  Jan 25 '13 at 08:37
  • Actually need to use onFocusChange, be cause, when we push enter, OnEditorActionListener event work for last cell, not for new. Thank you! – Ivan Jan 25 '13 at 09:52