如何将重点放在listview中动态添加的edittext上

时间:2015-01-29 16:04:11

标签: android listview

我正在尝试与此类似的内容Android ListView, with EditText ListItem move to Next最初会显示一个editext,当用户输入值时,我会在Editext.onTextChanged方法上创建一个项目。输入值后,会创建editext但焦点丢失,用户必须再次点击才能输入值。如何将焦点设置在listview中的下一个editext元素上?

任何人都可以向我解释我哪里出错了。任何建议都非常有帮助。

感谢

代码:

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.testeditext.R;

public class MainActivity extends Activity {

    ArrayList < String > list = new ArrayList < String > ();
    ListView lv;
    LvAdapter adpter;

    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.main);
        lv = (ListView) findViewById(R.id.listView1);
        lv.setItemsCanFocus(true);
        lv.setStackFromBottom(true);

        for (int i = 0; i < 1; i++) {
            list.add("1");
        }

        adpter = new LvAdapter();
        lv.setAdapter(adpter);
    }

    public class LvAdapter extends BaseAdapter {

        LayoutInflater mInflater;

        public LvAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

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

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

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

                convertView = mInflater.inflate(R.layout.ada, null); // this is your cell

                holder.caption = (EditText) convertView.findViewById(R.id.editText12);
                holder.caption.setTag(position);
                holder.caption.setText(list.get(position).toString());
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            int tag_position = (Integer) holder.caption.getTag();
            holder.caption.setId(tag_position);

            holder.caption.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                int count) {
                    final int position2 = holder.caption.getId();
                    final EditText Caption = (EditText) holder.caption;
                    if (Caption.getText().toString().length() > 0) {
                        //list.set(position2,Integer.parseInt(Caption.getText().toString()));
                        if (position == getCount() - 1) {
                            list.set(position2, Caption.getText().toString());
                            list.add(holder.caption.getText().toString());
                        } else {
                            list.set(position2, Caption.getText().toString());
                            // holder.radioBtn.setChecked(false);
                        }

                        notifyDataSetChanged();

                    } else {
                        list.remove(position2);
                        notifyDataSetChanged();
                        Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void afterTextChanged(Editable s) {
                }

            });

            return convertView;
        }
    }

    public class ViewHolder {
        EditText caption;
    }
}

1 个答案:

答案 0 :(得分:1)

到目前为止,我可以想到一种方法:

首先,在onTextChanged()中删除您刚刚用于创建新项目的项目 然后在getView()中检查所请求的视图是否是最后一个视图,并在那种情况下设置焦点。

你必须添加一个布尔字段,我们称之为setFocusToLast 创建新字段并从用于创建新字段的焦点中移除焦点后,您将setFocusToLast设置为true

您还需要知道用户明确请求何时关注其他元素,因此您需要实现onFocusChanged()(其中您将setFocusToLast重置为false)以防万一用户没有我不想继续添加字段,而是选择编辑现有字段 如果你不这样做,并且用户向下滚动以便最后一个项目变为可见,则焦点将移动到它。