2016-06-14 137 views
1

应用程序需要知道在EditText被使用,填充期间何时按下新的按键。这是否是它的事件?在iOS中,对于UITextField,已使用shouldChangeCharactersInRange委托方法。EditText事件处理程序得到通知已按下按键

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:id="@+id/editText" 
     android:layout_alignParentBottom="false" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="18dp" 
     android:layout_marginLeft="8dp" 
     android:width="100dp" 
     android:on/> <----- some event handler need here but only onclick I see 

enter image description here

回答

1

您可以使用TextChangedListener为您EditText编辑文本字段的文本

yourEditText.addTextChangedListener(new TextWatcher() { 

    public void afterTextChanged(Editable s) { 

    } 

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

    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     Log.i("TAG","text = "+yourEditText.getText().toString()); 
    } 
}); 
+0

你知道什么时候接收事件为什么我不能看到'TextChangedListener'布局文件?添加了一个屏幕截图的问题。 –

+0

@János将它添加到您的Java代码中(在您的Activity的onCreate方法中) –

+0

我看到了,但是从布局xml中不可能? –