TextWatcher 不处理 Fragment(无错误)

TextWatcher not working on Fragment (no Error)

我有一个带有 ListView 和 EditText 的片段,我在编辑文本中添加了一个 textWatcher 以在文本更改后执行某些操作,但它不起作用,没有任何反应。

我的代码:

 public class crafts extends Fragment {
   EditText iSearch;
   View V;
   TextWatcher textWatcher = new 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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d("Enter AfterTextChanges","WORKS!");
            String text = iSearch.getText().toString().toLowerCase(Locale.getDefault());
            filter(text);
        }
    };

 }

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

        V = inflater.inflate(R.layout.fragment_crafts, container, false);

        iSearch =(EditText)V.findViewById(R.id.iSearch);
        iSearch.addTextChangedListener(textWatcher);
        list=(ListView)V.findViewById(R.id.list);

        return inflater.inflate(R.layout.fragment_crafts, container, false);

 }

我尝试在文本更改时在 Android 监视器上打印,以查看是否发生了一些没有结果的事情。

尝试将您的 onCreateView 更改为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    V = inflater.inflate(R.layout.fragment_crafts, container, false);

    iSearch = (EditText) V.findViewById(R.id.iSearch);
    iSearch.addTextChangedListener(textWatcher);
    list = (ListView) V.findViewById(R.id.list);

    return V;
}