3

使用按钮很简单,

<Button 
    android:blablabla="blabla"
    ...
    android:onClick="doSomething" />

这将执行 doSomething(View) 函数。

我们如何使用 EditText 来模仿它?我已经读过这个,我读到大多数人使用一个 imeOptions (这似乎仍然是必要的),然后在那个 EditText 对象上实现一个 actionListener 。

这是我迷路了。有没有办法像使用 Button 一样实现从键盘到 onClick 函数的“完成”操作(或发送或...),或者我们需要显式实现侦听器?

问候 !

4

2 回答 2

5

当您按下Done软键盘中的键时,以下代码将执行一些操作。

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do your actions here that you like to perform when done is pressed
            //Its advised to check for empty edit text and other related 
            //conditions before preforming required actions
        }
    return false;
    }
});

希望能帮助到你 !!

于 2014-11-21T04:01:29.607 回答
1

I am assuming what you are wanting to do is run some code when the EditText is clicked?

If so, I have found a solution from another thread on the site:

    EditText myEditText = (EditText) findViewById(R.id.myEditText);
    myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {

    then do this code here

    }
}
});

via: A better way to OnClick for EditText fields?

于 2014-11-20T23:51:30.283 回答