加载Android Fragment时,始终会调用addTextChangedListener和onTextChanged

Mel*_*nie 10 android android-fragments

EditText在Android中有一个Fragment.我addTextChangedListeneronCreateView方法中附上它.在addTextChangedListener我有一个onTextChanged方法.通过记录,我可以看到每次Fragment加载时都会onTextChanged被调用.为什么会这样?我只希望在用户实际更改文本时调用它EditText.这是我的代码:

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.detailfragment, container, false);
    final EditText notes = (EditText)view.findViewById(R.id.stitchnotes);
notes.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start,
                int before, int count) {
            Edited = true;
        }
    });

    return view;
}
Run Code Online (Sandbox Code Playgroud)

我发现这篇文章这篇文章,我想知道我是否应该将代码移动到onResume.但是,正如您从上面的代码中可以看到的,我需要访问LayoutInflaterViewGroup传递给onCreateView才能访问EditText; 但是,onResume通常无法访问这些项目.我应该用onResume吗?我如何处理LayoutInflaterViewGroup?的问题?

- - - - - - - - -更多的信息 - - - - - - - - - - -

我用Tyler的答案解决了如何使用的问题onResume,但是当我第一次打开时,仍然会调用onTextChanged Fragment.有人可以解释原因吗?这是我修改过的代码:

private EditText notes;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.detailfragment, container, false);
    notes = (EditText)view.findViewById(R.id.stitchnotes);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    notes.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start,
                int before, int count) {
            Edited = true;
            Log.w("onTextChanged","Here");
        }
    });
}

<EditText
        android:id="@+id/stitchnotes"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="left"
        android:hint="@string/hint"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:inputType="text"
        android:visibility="invisible"
        android:textSize="30dip" />
Run Code Online (Sandbox Code Playgroud)

我打开的那一刻Fragment, Edited设置为true并写入LogCat.我不想要这个.我希望Edited只有当用户实际输入内容时才设置为true EditText.我究竟做错了什么?

Raj*_*ela 9

我有相同的问题,但在适配器.上面的解决方案适用于视图的直接子项,但在列表视图适配器中我有问题.最后,我找到了焦点改变的解决方案.

 editQty.setTag(false);
    editQty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            editQty.setTag(true);
        }
    });
    editQty.addTextChangedListener(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().equals("") && (Boolean) editQty.getTag()) {
               // Do your Logic here
                }
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)


Tyl*_*ell 5

让你EditText成为班上的一员.或者,成为View班级成员并findViewById稍后致电.

public class MyFragment extends Fragment {

    private EditText notes;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState) {
        View view = inflater.inflate(R.layout.detailfragment, container, false);
        notes = (EditText)view.findViewById(R.id.stitchnotes);
        // your other stuff
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        notes.setText("Now I can access my EditText!");
    }

    ...
Run Code Online (Sandbox Code Playgroud)