2

我有一个 EditText。我setOnEditorActionListener用来听用户输入的每个字母。但这似乎不起作用。我在函数中放了一个 println 语句来查看它是否已到达,但它从未被调用过。这是编辑文本。它缺少什么?

    <EditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:drawableRight="@drawable/my_img"
            android:hint="@string/email_hint"
            android:imeActionId="@+id/login"
            android:inputType="textNoSuggestions|textVisiblePassword"
            android:maxLines="1"
            android:singleLine="true"
            android:textColor="#000000" />
4

3 回答 3

6

如果您想收听用户输入的每个字母,TextWatcher您可以注册一个:EditText

editText.addTextChangedListener(new TextWatcher() {...} );
于 2013-08-29T21:04:22.547 回答
1

您正在寻找一个 TextWatcher。回调是不言自明的。根据您的需要,您可能希望将逻辑代码添加到 onTextChanged、beforeTextChanged 或 afterTextChanged。在 CharSequence 对象中,您可以获得 EditText 的文本:

EditText mEditText = (EditText) findViewById(R.id.email);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
            // Here you may access the text from the object s, like s.toString()

}

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

            // TODO Auto-generated method stub
        }

@Override
public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });
于 2013-08-29T21:13:41.410 回答
1

Try this in the activity that is using EditText object.

EditText emailText = (EditText) findViewById(R.id.email);

emailText.addTextChangedListener(new TextWatcher() {

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

    // put a debug statement to check

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    });
于 2013-08-29T21:12:27.297 回答