TextWatcher无法处理Fragment(无错误)

时间:2016-02-20 17:08:35

标签: android android-fragments textwatcher

我有一个带有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监视器上打印,以查看是否发生了没有结果的事情。

1 个答案:

答案 0 :(得分:2)

尝试将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;
}