Android如何将动态行为添加到edittexts的列表视图中

时间:2015-11-14 04:50:59

标签: android android-layout android-listview android-edittext

我正在尝试使用Edittexts实现特定行为。

我希望实现的目标

将向用户显示一个EditText。当焦点放在这个EditText中时,我希望在不打断用户输入的情况下创建另一个EditText。在转到下一个EditText时,将创建另一个EditText并将其放置在其下方。这将持续到用户填写他们希望填写的所有内容。 如果任何EditText(最后一个除外)为空,则应删除该EditText并从视图中删除。

到目前为止我遇到的问题

我尝试使用诸如TextWatchers,setOnEditorActionListener和onFocusChangeListeners之类的监听器。但是,每次我使用一个时,我都会遇到列表中数据表示不正确的问题。

问题

  • 在这种情况下,哪种听众最适合EditTexts?
  • 添加,删除或通知适配器更改的正确方法是什么?

适配器类

public class TwitterUserNameAdapter extends BaseAdapter{

    private List<String> twitterUserNames;

    public TwitterUserNameAdapter(){
        this.twitterUserNames = new ArrayList<>();
        twitterUserNames.add("");
    }

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

    @Override
    public Object getItem(int position) {
        return twitterUserNames.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.twitter_user_input_field, parent, false);
        }

        final EditText usernameEditText = (EditText) convertView.findViewById(R.id.etTwitterUserName);
        usernameEditText.setText(twitterUserNames.get(position));


        TextWatcher lastEditTextBehavior = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().isEmpty()){
                    twitterUserNames.set(position, s.toString());
                    twitterUserNames.add("");
                    notifyDataSetChanged();
                }
            }
        };

        TextWatcher standardEditTextBehavior = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().isEmpty()){
                    twitterUserNames.set(position, s.toString());
                    removeItem(position);
                    notifyDataSetChanged();
                }
            }
        };

        if (position == twitterUserNames.size() - 1) {
            usernameEditText.addTextChangedListener(lastEditTextBehavior);
        } else {
            usernameEditText.addTextChangedListener(standardEditTextBehavior);
        }

        String debugString = "";
        for (String username : twitterUserNames) {
            debugString += "\"" + username + "\" ";
        }
        Log.d("TAG", debugString);

        return convertView;
    }

    public void addNewItem() {
        twitterUserNames.add("");
        notifyDataSetChanged();
    }

    public void setItem(int index, String value) {
        twitterUserNames.set(index, value);
        notifyDataSetChanged();
    }

    public void removeItem(int index) {
        twitterUserNames.remove(index);
        notifyDataSetChanged();
    }
}

列表视图布局项XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tilTwitterUserName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColorHint="@color/black"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="\@"
            android:textSize="18dp"
            android:textColor="@color/primary_text" />

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/etTwitterUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:inputType="text"
            android:focusable="true"
            android:imeOptions="actionNext"
            android:hint="Twitter User Name"
            android:paddingTop="16dp"/>
    </LinearLayout>
</android.support.design.widget.TextInputLayout>

活动XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context="com.github.jackkell.mimicryproject.mainactivities.ImpersonatorCreationActivity"
    android:id="@+id/ImpersonatorCreation" >

    <android.support.design.widget.TextInputLayout
        android:id="@+id/tilImpersonatorName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/black"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp" >

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/etImpersonatorName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:inputType="text"
            android:hint="Impersonator Name" />

    </android.support.design.widget.TextInputLayout>

    <ListView
        android:id="@+id/lvTwitterUsernames"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_below="@id/tilImpersonatorName">
    </ListView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabCreateImpersonator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/lvTwitterUsernames"
        android:layout_alignRight="@+id/lvTwitterUsernames"
        android:layout_alignEnd="@+id/lvTwitterUsernames">
    </android.support.design.widget.FloatingActionButton>

</RelativeLayout>

0 个答案:

没有答案