How to change the focus of Edittext on a Listview?

Asked

Viewed 891 times

0

I have a ListView with a single EditText.

I used the following function to capture Enter, but how to use requestFocus() to select the next EditText?

 public class FriendslistAdapter extends BaseAdapter{

    private Context mContext;
    private int mQuantidade;
    private ViewHolder holder;
    public String[] attitude_values;

    public FriendslistAdapter(Context context, JsonArray array, int quantidade) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        this.mQuantidade = quantidade;
        attitude_values = new String[quantidade];
    }
    //Classe ViewHolder
    public final class ViewHolder {
        public EditText fullname;
        int position;

    }


    @Override
    //Número de campos a serem criados.
    public int getCount() {
        // TODO Auto-generated method stub
        return mQuantidade;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    //Id do item, refere-se a posição.
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         View vi=convertView;
         //Se  não houver nenhuma view, ela é criada.
            if(vi==null) 
            {
                vi = LayoutInflater.from(mContext).inflate(R.layout.row_friendlist, null);
            holder = new ViewHolder();
            holder.fullname = (EditText)vi.findViewById(R.id.etFullname);
            holder.fullname.setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                        InputMethodManager in = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

                        // NOTE: In the author's example, he uses an identifier
                        // called searchBar. If setting this code on your EditText
                        // then use v.getWindowToken() as a reference to your 
                        // EditText is passed into this callback as a TextView
                        Log.d("down", "down");
                        in.hideSoftInputFromWindow(holder.fullname
                                .getApplicationWindowToken(),
                                InputMethodManager.RESULT_HIDDEN);


                       //userValidateEntry();
                       // Must return true here to consume event
                       return true;

                    } 

                    return false;
                }
            });
            holder.fullname.addTextChangedListener(new TextWatcher()
            {
                public void afterTextChanged(Editable edt) 
                {
                    attitude_values[position] = edt.toString();
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                   // attitude_values[position] = holder.fullname.getText().toString();
                }
            });

            if (attitude_values[position] != null)
            {
                   holder.fullname.setText(attitude_values[position]);
           }
            else {
                  holder.fullname.setText("");
            }
            vi.setTag(holder);
            //Caso já haja uma view o objeto holder recebe o getTag da View vi já criada.
        } else {
            holder = (ViewHolder)vi.getTag();

        }
            holder.position = position;



        return vi;
    }


}

1 answer

1


Create an Edittext List, and store each object in that List. When pressed enter, you check the tight position, and call the next one inside the List.

private List<EditText> edits;
public View getView(final int position, View convertView, ViewGroup parent) {
  edits.add(convertView, position);
  ...
}

In the enter action, you call the list at position and do requestFocus().

edits.get(position).requestFocus();
  • Your idea is right, but with some wrong parameters, I will edit your answer, thank you.

  • I wrote it fast. I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.