EditText.getText()。toString()有时返回“”

时间:2013-08-01 12:36:21

标签: android null android-edittext gettext

我使用onTextChanged() TextWatcher方法获取EditText的文本及其长度 当我键入以添加字符时它工作正常,但是即使文本不为空,从文本getText()中删除字符也会为空。这是随机发生的,而不是每次我删除字符。我观察到这种情况主要发生在文本中有3-4个字符时我按退格键。

奇怪的是,此问题仅发生在设备上,而不是模拟器上。

布局文件

<LinearLayout
    style="@style/create_trip_activity_components_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/from_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:ems="10"
        android:hint="@string/from_location_hint"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/use_current_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:onClick="useCurrentLocation"
        android:text="@string/use_current"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

代码:

fromLocation.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

        if (fromLocation == null) {
            Log.d(TAG, "fromLocation is null................");
        }

        Log.d(TAG, "fromLocation text : "
                    + fromLocation.getText().toString());
        Log.d(TAG, "fromLocation text length :  "
                    + fromLocation.getText().length());
        Log.d(TAG, "S : "
                    + s);
        Log.d(TAG, "S length :  "
                    + s.length());
    }
});

注意:我尝试使用afterTextChanged()beforeTextChanged()方法。但这并没有解决问题。

10 个答案:

答案 0 :(得分:1)

我没有看到任何问题。 from_location的文字应该改变,而且确实如此。

当您在EditText代码中时,为什么要“检查”from_locationTextWatcher)。我不认为EditText的价值在变化时必须是“稳定的”。

当您检查CharSequence中的from_location正在更新时,可能会发生什么事情,有时,您会在更改过程中捕获它,有时候会在更新后捕获它。

您是否检查(CharSequence s, int start, int before, int count)是否返回了预期值?

我看到了这种情况。如果您想对文本进行任何更改,您应该在String s方法的afterTextChanged参数上进行更改。

我编写此代码以检查更改EditText内容时发生的情况,也许是任何

    mEdtText.addTextChangedListener(new TextWatcher() {
        private static final String TAG = "TextWatcher";
        private static final boolean DEBUG = true;

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (DEBUG)
                Log.d(TAG,
                        "(onTextChanged) In \"" + s + "\" " + before
                                + " characters "
                                + " are being replaced at " + start
                                + " by " + count + " ("
                                + s.subSequence(start, start + count) + ")");
            if (DEBUG)
                Log.d(TAG,
                        "(onTextChanged) mEdtText: \"" + mEdtText.getText()
                                + "\"");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            if (DEBUG)
                Log.d(TAG,
                        "(beforeTextChanged) In \"" + s + "\" " + count
                                + " characters ("
                                + s.subSequence(start, start + count)
                                + ") are about to be replaced at " + start
                                + " by " + after);
            if (DEBUG)
                Log.d(TAG,
                        "(beforeTextChanged) mEdtText: \""
                                + mEdtText.getText() + "\"");

        }
    }

如果您查看EditText的源代码,则可以看到从getText继承TextView方法并返回CharSequence mText。您也可以播种另一个CharSequence mTransformed。在巨大的setText方法中,有mTextmTransformed取代的时刻。可能当您致电from_location的{​​{1}}时,您将获得getText,并且在mText开始后第二次执行该操作时,您会得到setText作为答案。

如果你想检查一下,只需更改

mTransformed

答案 1 :(得分:0)

如果您正在进行调试模式,请记住它有两个存储值的位置。在我的例子中,EditText有mText(Spannable String Builder),它的子字段(也是mText)是ID。 spannable字符串构建器将返回.toString()的值。另一个将返回.getText()的值。

答案 2 :(得分:0)

尝试使用onTextChange()中的CharSequence应该与fromLocation.getText()返回的相同。

答案 3 :(得分:0)

public void afterTextChanged(Editable s) {
           if(s.length()>=6)
           {

           }
           else{

           }
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }
          public void onTextChanged(CharSequence s, int start, int before, int count) {

          }

答案 4 :(得分:0)

这可能就是您所需要的。这是我在项目中用于搜索字符串的一些代码。我有一个名为mySearchMethod的方法,不用担心。但是在文本更改后我在方法上得到了我的编辑文本字符串..

 public void search(){
    // Notice I used a final edittext
    final EditText editText = (EditText) findViewById(R.id.search);


    // create the TextWatcher
    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) {

        }

        @Override
        public void afterTextChanged(Editable editable) {


              String searchQuery = editText.getText().toString();

              // This method is just something I did with the string I got
              MySearchMethod(searchQuery);




        }
    };

    //we must add the textWatcher to our EditText
    editText.addTextChangedListener(textWatcher);

}

调用on create中的方法search()来初始化它。

答案 5 :(得分:0)

什么是fromLocation? 您应该使用CharSequence,如下所示。

试试这个......

@Override
 public void onTextChanged(CharSequence s, int start, int before,
            int count) {

// if(fromLocation == null) {
//         Log.e(TAG, "fromLocation is null.");
//  }

Log.e(TAG, "fromLocation text : " + s.toString());
Log.e(TAG, "fromLocation text length :  " + s.toString().length()); 

// Log.e(TAG, "fromLocation id : " + fromLocation.getId());
// Log.e(TAG, "fromLocation text : " + fromLocation.getText().toString());
// Log.e(TAG, "fromLocation text length :  " +    //fromLocation.getText().toString().length());            
}

答案 6 :(得分:0)

试试这个会工作......

final EditText from_location = (EditText) findViewById(R.id.from_location);

    from_location.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub              
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub              
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            if(from_location == null) {
                Log.v("", "fromLocation is null.");
        }

        Log.v("", "fromLocation id : " + from_location.getId());
        Log.v("", "fromLocation text : " + from_location.getText().toString());
        Log.v("", "fromLocation text length :  " + from_location.getText().toString().length());            

        }
    });

答案 7 :(得分:0)

我正在根据First EditText上的用户输入更新其他Second EditText值,反之亦然。

因此,对于First EditText,我已经添加了类似于下面的TextWatche,它永远不会崩溃并且永远不会陷入任何错误或异常。你可以试试这样的事情。

final EditText from_location = (EditText) findViewById(R.id.from_location);

 from_location.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(!(from_location.getText().toString().equals(""))) {
                Log.e(TAG, "fromLocation id : " + fromLocation.getId());
                Log.e(TAG, "fromLocation text : " + fromLocation.getText().toString());
                Log.e(TAG, "fromLocation text length :  " + fromLocation.getText().toString().length());
            }else{
                 Log.e(TAG, "fromLocation is null.");
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {}
        @Override
        public void afterTextChanged(Editable s) {

        }
    });

希望这会对你有所帮助。如果尼特没有帮助你,请告诉我。

享受编码.... :)

答案 8 :(得分:0)

好的这是解决方案,而不是使用getText获取文本,只需转换传递给

的charSequence
onTextChanged(CharSequence s, int start, int before,
                    int count)

使用s.toString()

进行回调

我的示例类看起来像这样

package com.peshal.texttest;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText  fromLocation = (EditText) findViewById(R.id.editText1);
    fromLocation.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        Log.d("Current String is :" ,s.toString());

        }

      });
  }


 }

答案 9 :(得分:0)

我确实遇到了同样的问题。我的代码在大多数设备上都运行正常,但在使用Android 4.1.2的Motorola Razr i上,它随机返回一个空字符串。

我做了以下解决方法:

// TextWatcher Methods
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
    fromLocation.post(new Runnable()
    {

        @Override
        public void run()
        {
            Editable text = fromLocation.getText();
            //... now the string is not empty anymore
        }
    });

}

@Override
public void afterTextChanged(Editable editable)
{
}

虽然我不知道为什么会出现问题 - 将其发布在Runnable(可能是延迟的)中修复它。并没有影响其他设备的行为。

在你的情况下,你可能需要做一些额外的!=对fromLocation的null检查 - 在我的情况下它永远不会为null。

相关问题