OnEditorActionListener不起作用

时间:2016-08-26 19:02:01

标签: android actionlistener

我只是想在用户编辑EditText时显示Toast消息,但我没有收到Toast消息!

我做错了什么?

public class OfferRide extends Activity implements TextView.OnEditorActionListener {
    private EditText et_msg;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        initializeViews();
    }

    private void initializeViews() {
        et_msg = (EditText) findViewById(R.id.et_msg);
        et_msg.setOnEditorActionListener(this);
    }
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Toast.makeText(getApplicationContext(), "Editing",Toast.LENGTH_LONG).show();
        return false;
    }
}

1 个答案:

答案 0 :(得分:2)

如果您在onEditorAction()中按Enter键,

EditText将有效。要在每次进行更改时触发操作,都可以使用TextWatcher()

et_msg.addTextChangedListener(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) {
            Toast.makeText(getApplicationContext(), charSequence ,Toast.LENGTH_LONG).show();
        }

        @Override
        public void afterTextChanged(Editable editable) {}
});