监视用户输入的 EditText 行的最有效方法是什么?

What is most efficient way to monitor an EditText line for user input?

我为用户输入设置了一个 EditText。一旦用户键入任何字符,我希望触发一个浮动标签上升到 EditText 行上方。当用户在 EditText 行中输入第一个字符时,查看的最佳方法是什么?文字观察者?其他人?

部分Activity文件:

...
private ListenerEditText cListenerEditText;

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

    cListenerEditText = (ListenerEditText) findViewById(R.id.CEditText);
    cTextInputLayout = (TextInputLayout) findViewById(R.id.ToDo_text_input_layout);

    cListenerEditText.requestFocus();
    cListenerEditText.setHint("To Do");

部分布局xml文件:

...
<android.support.design.widget.TextInputLayout
    android:id="@+id/ToDo_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  >

<com.example.jdw.fourthscreen.ListenerEditText
    android:id="@+id/CEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text|textCapSentences|textNoSuggestions"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF"
    android:textColorHighlight="#30D5C8"
    android:singleLine="true"
    android:maxLength="51"
    android:imeOptions="actionNext|flagNoExtractUi"
    android:layout_marginBottom="3dp"
    android:nextFocusDown="@+id/DEditText" >

</com.example.jdw.fourthscreen.ListenerEditText>
</android.support.design.widget.TextInputLayout>

监控and/or控制文本输入,我们通常使用TextWatcherInputFilter

对于你的情况,我认为 TextWatcher 是正确的做法。

要在 EditText 中监视用户键入事件,您可以使用 TextWatcher

您可以使用afterTextChanged()方法来检查每个键入的字符。

下面是将 TextWatcher 添加到 EditText 的代码。

cListenerEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
    }
});