【问题标题】:editText.getText().toString returns emptyeditText.getText().toString 返回空
【发布时间】:2017-06-07 08:00:22
【问题描述】:

我是 Android 新手,在这个网站的帮助下,我整理了这段代码:

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" />

但是当我输入时 textView 显示为空,尝试了很多东西但没有任何效果。

我要做的是获取 editText 的值并将其存储在一个变量中,以便我可以使用它。我使用 textView 来查看它是否有效但返回 empty 。我希望实时更新值,就像 Android 默认计算器应用程序一样。

编辑

出于我的目的,我不得不放置一个 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) {

        }
    });

}

}

【问题讨论】:

  • 当你得到字符串的时候是空的......显然你需要在它改变时倾听......
  • TextView TextView 您已将变量命名为与类名相同。我认为这技术上很好,但绝对不要这样做。至少在骆驼情况下声明它:TextView textView 但您可能应该称它为更具描述性的东西。
  • 更具体到您的实际问题,您的函数调用出现在onCreate。创建活动时,默认情况下文本框为空。您需要将代码移动到其他地方。也许是按下按钮,或者是文本框上的更改监听器。

标签: java android textview gettext editview


【解决方案1】:

正如@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);

但是请移除监听器 (textView.removeTextChangedListener(textWatcher);) 完成后,这里可能会发生内存泄漏。

【讨论】:

  • 是的,这就是解决方案,但不幸的是,您的代码无法正常工作,因为它会使应用程序崩溃,但我已经让它工作了,我会发布工作代码。谢谢。
  • 很高兴听到您成功了。我想知道,当应用程序使用上面的代码崩溃时记录的异常是什么?我想知道是不是因为你的局部变量“TextView”没有设置,它试图静态使用 TextView.setText()。我想这就是为什么您最好不要将变量命名为与类相同的原因;)顺便说一句,您添加到原始答案的解决方案不会在使用后删除 TextWatcher,这可能会阻止 EditText 实例被垃圾收集和从而浪费内存。
【解决方案2】:

您没有正确转换 java 文件...

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

尝试替换为

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

【讨论】:

    【解决方案3】:

    将您的代码更改为以下内容:

        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);
        }
    
    }
    
    
    }
    

    希望它运作良好:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2017-04-27
      • 2015-11-12
      • 1970-01-01
      相关资源
      最近更新 更多