简体   繁体   English

editText.getText()。toString返回空

[英]editText.getText().toString returns empty

I'm new to Android and with the help of this site I've put together this code : 我是Android的新手,在此网站的帮助下,我将以下代码组合在一起:

java: 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: 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 . 但是当我输入文本时,textView显示为空,尝试了很多事情,但没有任何作用。

What I'm trying to do is get the value of editText and store it in a variable so I can work with it . 我想做的是获取editText的值并将其存储在变量中,以便我可以使用它。 I used a textView to see if it works but returns empty . 我使用了textView来查看它是否有效,但返回空值。 I want the value to be updated real time , just like Android default calculator app . 我希望该值能够实时更新,就像Android默认计算器应用程序一样。

EDIT 编辑

For my purpose I had to put a textWatcher. 为了我的目的,我不得不放置一个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... 您没有正确投射Java文件...

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. 正如@Selvin指出的那样,EditText中的文本更改侦听器将有所帮助。

        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. 但请在完成后删除侦听器( textView.removeTextChangedListener(textWatcher); ),因为此处可能会发生内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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