2013-09-10 49 views
0

布局:安卓:如果使用imeOptions方法被调用了两次

.... 
    <EditText 
     .... 
     android:hint="@string/email" 
     android:imeOptions="actionSend"/> 
    <Button 
     ... 
     android:onClick="sendMessage"  <<<- both must call it 
     android:text="@string/send" /> 

然后在代码绑定:

((EditText) findViewById(R.id.email)).setOnEditorActionListener(new OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) { 
       sendMessage(findViewById(android.R.id.content)); 
       return false; 
      } 
     }); 

其中的sendMessage是

public void sendMessage(View view) 
{ 
    .... 
    intent.putExtra("email", getEditContent(R.id.email)); 
    startActivityForResult(intent, 0); 
} 

当我按下按钮,一切都精细。当我在ime选项(键盘)中按“完成”时,两个活动同时启动。

我在做什么错?

+0

您是否尝试将返回值从'false'更改为'true'? –

+0

@HugoHidekiYamashita,哈!这样可行。请将它作为单独的答案发布。但对我来说这很奇怪,因为据我所知:这意味着“运行默认处理程序或不运行”(换句话说,“它在这里是否处理完了”)。但是如果我没有实现OnEditorActionListener - 根本没有任何反应! (我的意思是,默认情况下没有任何东西被调用,所以默认处理程序必须不做任何事不管怎样,谢谢你。 –

回答

3

onEditorAction方法的返回值从true更改为false

其实我觉得这个方法被称为两次因为KeyEvent。尝试记录arg2参数的类型以检查它。如果您确认这一点,而不是返回false,您可以添加一个if/else来检查正确的事件。

1

很可能是您的听众正在接收两个不同的事件。尝试并调试onEditorAction方法来检查KeyEvent arg2的值,在正确的事件中调用您的sendMessage方法。

1
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
    if (event.getAction() != KeyEvent.ACTION_DOWN) 
     return false; 

    // do your stuff 

    return true; 
} 
+1

请不要提供仅用于代码的答案,还要解释您的代码如何以及为什么解决了问题。 –

相关问题