我在 Android Studio 中的文本更改监听器方面遇到一些问题。我正在使用 Fragment,当我运行它时,Logcat 上没有显示任何内容。我确定我在 XML 文件中输入了文本编辑内容。我也检查了id,没有出现错误。
请告诉我为什么 Logcat 中没有弹出任何内容。
public class FirstFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
private EditText fiftythinput;
private TextView fiftythres;
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.v("tag_t","2");
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.v("tag_t","3");
}
@Override
public void afterTextChanged(Editable editable) {
Log.v("tag_t","1");
}
};
public static FirstFragment newInstance(int page){
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
FirstFragment blankFragment = new FirstFragment();
blankFragment.setArguments(args);
return blankFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
fiftythinput = (EditText)rootView.findViewById(R.id.fiftyinput);
fiftythres = (TextView)rootView.findViewById(R.id.fiftyres);
fiftythinput.addTextChangedListener(textWatcher);
return rootView;
}
}
________添加 XML________
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/fiftyth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="50000원"
android:textSize="15dp" />
<EditText
android:id="@+id/fiftythinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="수량"
android:inputType="number"
android:textSize="10dp" />
<TextView
android:id="@+id/fiftythres"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="- \"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
最佳答案
我认为错误在于您的 XML 代码将编辑文本标记为 fiftythres
。在您的代码中,您将其设置为:
fiftythinput = rootView.findViewById(R.id.fiftyinput);
您需要做的是:
fiftythinput = (EditText)rootView.findViewById(R.id.fiftythinput);
希望对您有帮助!如果没有,请通知我。
关于java - 当我实现它时如何修复文本更改监听器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57290963/