gpt4 book ai didi

android - addTextChangedListener 和 onTextChanged 总是在 Android Fragment 加载时被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:37 30 4
gpt4 key购买 nike

我在 Android Fragment 中有一个 EditText。我在 onCreateView 方法中将 addTextChangedListener 附加到它。在 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。但是,正如您从上面的代码中看到的,我需要访问传递给 onCreateViewLayoutInflaterViewGroup 才能访问 编辑文本;但是,onResume 通常无法访问这些项目。我应该使用 onResume 吗?如何处理 LayoutInflaterViewGroup 的问题?

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

我使用Tyler的答案解决了如何使用onResume的问题,但是当我第一次打开Fragment时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" />

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

最佳答案

我遇到了同样的问题,但在适配器中。上面的解决方案适用于 View 的直接子级,但在 ListView 适配器中我遇到了问题。最后我找到了焦点改变的解决方案。

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

关于android - addTextChangedListener 和 onTextChanged 总是在 Android Fragment 加载时被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713246/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com