如何自动将 textview 的值分配给字符串

我想自动将editext值分配给字符串


当我单击它分配但不是自动分配的按钮时


private void OverValidation()

    {


        if (!noOfOvers.equals("0"))

        {

            noOfOvers=chooseOverEdttext.getText().toString();

        }

        else

        {

            Toast.makeText(getActivity(), "Please choose no of overs", Toast.LENGTH_SHORT).show();

        }

    }

我想当我在编辑文本中键入文本时,它同时为字符串赋值


30秒到达战场
浏览 98回答 4
4回答

交互式爱情

您可以尝试使用数据绑定库。它具有连接视图和数据源的功能。例如,您可以在 Activity 中有一个 ObservableString:ObservableField<String> noOfOvers = new ObservableField<String>()然后,在你的 xml 中,你应该添加一个绑定到这个数据:<layout&nbsp;&nbsp; xmlns:android=”http://schemas.android.com/apk/res/android"&nbsp; xmlns:tools=”http://schemas.android.com/tools">&nbsp; <data>&nbsp; &nbsp; <import type="android.databinding.ObservableField"/>&nbsp; &nbsp; <variable name="noOfOvers" type="ObservableField<String>"&nbsp; </data>&nbsp; <LinearLayout&nbsp; &nbsp; android:layout_width=”match_parent”&nbsp; &nbsp; android:layout_height=”match_parent”&nbsp; &nbsp; android:orientation=”vertical”>&nbsp; &nbsp; <EditText&nbsp; &nbsp; &nbsp; android:layout_width=”match_parent”&nbsp; &nbsp; &nbsp; android:layout_height=”wrap_content”&nbsp; &nbsp; &nbsp; android:text=”@{noOfOvers}”/>&nbsp; </LinearLayout></layout>现在,一旦您在 EditText 中进行了更改,它的值将自动分配给 noOfOvers 变量。

精慕HU

您可以将更改侦听器添加到您的 edittext 然后将字符串设置为您的 edittext 或其他youredittext.addTextChangedListener(new TextWatcher() {&nbsp;@Override&nbsp;public void afterTextChanged(Editable s) {}&nbsp;@Override&nbsp; &nbsp;&nbsp;&nbsp;public void beforeTextChanged(CharSequence s, int start,&nbsp;int count, int after) {&nbsp;}&nbsp;@Override&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void onTextChanged(CharSequence s, int start,&nbsp; &nbsp;int before, int count) {&nbsp; &nbsp; if(s.length() != 0)&nbsp; &nbsp; field2.setText("");&nbsp;}&nbsp;});

慕尼黑5688855

您可能想要使用TextView#addTextChangedListener(文档)。这很容易。chooseOverEdttext.addTextChangedListener(&nbsp; &nbsp;new TextWatcher() {&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onTextChanged(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final CharSequence text,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int start,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int before,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int count) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;final String noOfOvers = text.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Process your String, or use CharSequence directly&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void beforeTextChanged(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final CharSequence text,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int start,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int count,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int after) { }&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void afterTextChanged(final Editable editable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Editable represents the changed text. If you need to process&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the inserted text and modify it, you can apply these modifications&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// here, directly to the Editable instance. Be carefult as it might be called recursively.&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;});如果您经常使用TextWatcher(s),您可能还想创建一个Adapter,这是一个将这三种方法实现为无操作的基类。然后你将能够只覆盖你感兴趣的那个。更干净!

温温酱

您可以通过将 textchangeListner 添加到 editText 来实现。这是您的代码的正确实现:&nbsp;chooseOverEdttext.addTextChangedListener(new TextWatcher() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setString();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void afterTextChanged(Editable editable) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;});&nbsp; private void setString(){&nbsp; &nbsp; String overs=chooseOverEdttext.getText().toString();&nbsp;&nbsp; &nbsp; if (!(TextUtils.isEmpty(overs)||overs.equals("0")))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; noOfOvers=overs;&nbsp; &nbsp; }&nbsp; }只需复制并粘贴此代码代替您当前的代码
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java