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

时间:2014-02-11 21:19:40

标签: android android-fragments

我在Android EditText中有一个Fragment。我在addTextChangedListener方法中将onCreateView附加到其中。在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;
}

我找到了this postthis post,我想知道是否应将代码移至onResume。但是,正如您从上面的代码中看到的那样,我需要访问传递给LayoutInflater的{​​{1}}和ViewGroup才能访问onCreateView;但是,EditText通常无权访问这些项目。我应该使用onResume吗?如何处理onResumeLayoutInflater

的问题

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

我使用Tyler的答案来解决如何使用ViewGroup的问题,但是当我第一次打开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" /> Fragment,的那一刻设置为true并写入LogCat。我不想要这个。我希望Edited仅在用户实际输入Edited中的内容时设置为true。我做错了什么?

5 个答案:

答案 0 :(得分: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
                }
            }
        }
    });

答案 1 :(得分: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!");
    }

    ...

答案 2 :(得分:1)

小而简单

  

基本上在这种情况下,片段加载onTextChanged字符序列返回&#34;&#34;这不是空字符串,所以这会给你一些更好的想法

myEditText.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) {



            if (!s.toString().isEmpty()) {

               //control come here when user enter some string to edit text

            } else {

                  //When fragment loads control comes here but below condition prevents execution because s.toString() give ""

                if (!s.toString().equals("")) {




                }

            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

答案 3 :(得分:0)

我在onViewCreated的Handler().postDelayed()中解决了它。示例:(科特林)

Handler(Looper.getMainLooper()).postDelayed({
            binding?.searchEdit?.doAfterTextChanged {
                
            }
        }, 100)

答案 4 :(得分:-1)

public class FeedbackFragment扩展了SherlockFragment {

private EditText mEtFeedback;
CustomTextWatcher obj;


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

view = inflater.inflate(R.layout.fragment_feedback_layout, container, false);
initUIComponents();
obj=new CustomTextWatcher();
mEtFeedback.addTextChangedListener(obj);

return view;
}



@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mEtFeedback.removeTextChangedListener(obj);

}

@Override
public void onClick(View v) {
switch (v.getId()) {

default:
    break;
}

}
class CustomTextWatcher implements TextWatcher {



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

    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

    }


}

}