Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载Android片段时,始终调用addTextChangedListener和ContextChanged_Android_Android Fragments - Fatal编程技术网

加载Android片段时,始终调用addTextChangedListener和ContextChanged

加载Android片段时,始终调用addTextChangedListener和ContextChanged,android,android-fragments,Android,Android Fragments,我有一个安卓片段中的EditText。我在onCreateView方法中将addTextChangedListener附加到它。在addTextChangedListener中,我有一个onTextChanged方法。通过日志记录,我可以看到每次加载片段,都会调用onTextChanged。为什么会这样?我只希望在用户实际更改EditText中的文本时调用它。这是我的密码: @Override public View onCreateView(final LayoutInflater infla

我有一个安卓
片段
中的
EditText
。我在
onCreateView
方法中将
addTextChangedListener
附加到它。在
addTextChangedListener
中,我有一个
onTextChanged
方法。通过日志记录,我可以看到每次加载
片段
,都会调用
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;
}
我找到了,我想知道是否应该将代码移到恢复时的
onResume
。但是,从上面的代码中可以看到,我需要访问传递给
onCreateView
LayoutInflater
ViewGroup
,以便访问
EditText
;但是,
onResume
通常无法访问这些项目。我应该在简历中使用
吗?如何使用
LayoutInflater
ViewGroup
处理该问题

-----------------进一步资料----------------------

我用Tyler的答案解决了如何在Resume上使用
的问题,但是当我第一次打开
片段时,onTextChanged仍然被调用。有人能解释一下原因吗?这是我修改过的代码:

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" />
私人编辑文本注释;
@凌驾
创建视图上的公共视图(最终布局充气机、最终视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.detailfragment,container,false);
notes=(EditText)view.findViewById(R.id.stitchnotes);
返回视图;
}
@凌驾
恢复时公开作废(){
super.onResume();
notes.addTextChangedListener(新的TextWatcher(){
@凌驾
公共无效后文本已更改(可编辑){
}
@凌驾
更改前的公共无效(字符序列、整数开始、,
整数计数,整数后){
}
@凌驾
public void onTextChanged(字符序列,int start,
前整数,整数计数){
编辑=真;
Log.w(“onTextChanged”,“Here”);
}
});
}

当我打开
片段时,
编辑的
被设置为true,并且LogCat被写入。我不要这个。我希望仅当用户在
EditText
中实际键入内容时,才将
Edited
设置为true。我做错了什么?

让你的
EditText
成为班上的一员。或者,使
视图成为类的成员,稍后调用
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!");
    }

    ...

公共类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

    }


}

}

我也遇到了同样的问题,但在适配器中。上面的解决方案对于视图的直接子级很好,但在列表视图适配器中我遇到了问题。 最后我找到了焦点改变的解决方案

 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
                }
            }
        }
    });

小而简单

基本上在这种情况下,当片段加载onTextChanged字符序列返回“”时,它不是空字符串,所以这将为您提供一些更好的想法


我在onViewCreated的Handler().postdayed()中添加它解决了这个问题。示例:(科特林)


谢谢,泰勒,这是显而易见的。我将试一试,看看它是否解决了我的问题,总是调用ContextChanged。
EditText
是否在XML布局中定义了任何默认文本?在
onTextChanged()
方法中实际设置的文本是什么?没有默认文本。有一个暗示。是吗?我已经为上面的EditText.BTW添加了XML——我接受了提示,但没有乐趣。它仍然以相同的方式运行。在调试器中(或通过打印到日志),您可以检查新文本值是什么(
s
变量)?它可能提供了一个线索,关于发生了什么。奇怪。当我在日志中输出“Here”时,它会在我打开片段时得到输出。然而,当我输出s.toString时,它只在我开始在EditText中写入时才得到输出。但是,无论我走哪条路,Edited都会在我打开片段的那一刻被设置为true,Edited的值就是我在这里想要的。这并不能说明用户可能会删除所有文本,因此s.toString()将为空。如果(!TextUtils.isEmpty(editable.toString()){}来处理空字符串或空字符串,我会使用它。
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) {

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