简体   繁体   中英

editText.getText().toString returns empty

I'm new to Android and with the help of this site I've put together this code :

java:

public class Activity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    String input1 = ((EditText) findViewById(R.id.editText)).getText().toString().trim();
    TextView TextView = (TextView) findViewById(R.id.textView);

    if (input1.length() == 0) {
        TextView.setText("empty");
    } 
      else {
        TextView.setText(input1);
    }

}


}

xml:

 <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:hint="text" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="28dp"
    android:layout_marginStart="28dp"
    android:layout_marginTop="20dp"
    android:id="@+id/textView" />

But the textView shows empty when I type , tried a lot of things but nothing worked .

What I'm trying to do is get the value of editText and store it in a variable so I can work with it . I used a textView to see if it works but returns empty . I want the value to be updated real time , just like Android default calculator app .

EDIT

For my purpose I had to put a textWatcher.

public class Activity extends AppCompatActivity {


TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    EditText editText = (EditText) findViewById(R.id.editText);
    textView = (TextView) findViewById(R.id.textView);

    editText.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) {
            textView.setText(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

}

}

You didnt cast the java file properly...

TextView TextView = (TextView) findViewById(R.id.textView);

try replacing with

 TextView textView = (TextView) findViewById(R.id.textView);

Change your code to the following :

    public class Activity extends AppCompatActivity {
EditText ed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    ed= (EditText) findViewById(R.id.editText);
    TextView TextView = (TextView) findViewById(R.id.textView);

    String input1 = ed.getText().toString().trim();

    if (input1.length() == 0) {
        TextView.setText("empty");
    } 
      else {
        TextView.setText(input1);
    }

}


}

Hope it works well :)

As pointed out by @Selvin, a Listener for text changes in the EditText would help.

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 0) {
                    TextView.setText("empty");
                }
                else {
                    TextView.setText(charSequence);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };
        EditText editText = (EditText) findViewById(R.id.editText);
        editText.addTextChangedListener(textWatcher);

But please remove the Listener ( textView.removeTextChangedListener(textWatcher); ) when you're done, as there's a potential for a memory leak here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM