我在 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);
希望能帮助到你!如果没有,请通知我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句