2010-11-03 69 views
24

在我测试的一款手机上(HTC Incredible,Android 2.2,软件3.21.605.1),我遇到以下行为。OnEditorActionListener在SenseUI键盘上调用两次具有相同的eventTime

当按下Sense UI键盘上的Enter键时,会调用onEditorAction事件处理程序两次(立即)。

的KeyEvent.getEventTime()是两个时代的事件被称为相同,导致我这个变通:

protected void onCreate(Bundle savedInstanceState) { 
    [...] 

    EditText text = (EditText)findViewById(R.id.txtBox); 
    text.setOnEditorActionListener(new OnEditorActionListener() { 
     private long lastCalled = -1; 

     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if (event.getEventTime() == lastCalled) { 
       return false; 
      } else { 
       lastCalled = event.getEventTime(); 
       handleNextButton(v); 
       return true; 
      } 
     } 
    }); 

    [...] 
} 

的EditText上被定义为:

<EditText 
    android:layout_width="150sp" 
    android:layout_height="wrap_content" 
    android:id="@+id/txtBox" 
    android:imeOptions="actionNext" 
    android:capitalize="characters" 
    android:singleLine="true" 
    android:inputType="textVisiblePassword|textCapCharacters|textNoSuggestions" 
    android:autoText="false" 
    android:editable="true" 
    android:maxLength="6" 
/> 

在我测试过的所有其他设备,动作按钮被正确标记为“下一步”,并且该事件仅在按下该按钮时被称为单次。

这是Sense UI键盘中的错误,还是我做错了什么?

感谢您的协助。


更新 - 由于给出的答案,我已经解决了以下作为我的支票。这工作正常,两个我都可用手机测试(Sense界面和CyanogenMod的CM7)

  if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) { 
       return false; 
      } 

      if (actionId != EditorInfo.IME_ACTION_NEXT && actionId != EditorInfo.IME_NULL) { 
       return false; 
      } 
+2

对你没有答案我很害怕,我只是想说我今天受到这个问题的打击。我从未在今天看过HTC Sense键盘..呃!他们怎么逃避忽略textNoSuggestions标志?使我的UI有点烂,腐烂它们。 – 2010-11-11 16:54:31

+0

我注意到很多应用程序工作不正常,因为他们的键盘没有实现默认按钮(它总是“输入”)。这包括Google提供的应用程序(包括我认为的初始注册过程)。对于HTC来说,这似乎是一件非常重要的事情。 真遗憾,因为我比起Motoblur更喜欢SenseUI。 感谢您的确认。 – mbafford 2010-11-15 13:24:28

回答

26

米奇说,你要检查事件操作:

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

    // do your stuff 

    return true; 
} 

这段代码工作的Sense界面和模拟器中。

+2

这似乎几乎是答案,虽然在我的其他手机上(运行CyanogenMod CM7),但当我按下键盘上的“下一步”时,“事件”为空,所以我必须检查这一点。 – mbafford 2011-02-16 16:32:12

+1

我还添加了一个 “if(actionId == EditorInfo.IME_ACTION_UNSPECIFIED) 返回false;”该方法使用 。 – AlexVogel 2011-07-04 14:39:12

0

,而不是发送的IME动作和null作为一个事件,它发送的触摸事件,所以如果你没有得到正确的动作,你可以检查事件action_up或ACTION_DOWN

+1

我不明白你在这里说什么。在HTC的Sense用户界面和其他正确实施默认操作的键盘上是否可以正常工作? – mbafford 2011-01-12 01:28:13

3
(EditText) passwordView = (EditText) findViewById(R.id.password); 
passwordView.setImeOptions(EditorInfo.IME_ACTION_DONE); 
    passwordView.setOnEditorActionListener(new OnEditorActionListener() 
     { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
      { 
       String input; 
       if(actionId == EditorInfo.IME_ACTION_DONE) 
       { 
        input= v.getText().toString(); 
        Toast toast= Toast.makeText(LogIn.this,input, 
          Toast.LENGTH_LONG); 
        toast.setGravity(Gravity.CENTER, 0, 0); 
        toast.show(); 
        return true; 
       } 
       return false; 
      } 
     }); 
0

返回true; 后处理乌尔情况下,它不会被再次

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 

       if (actionId == EditorInfo.IME_NULL) 
       { 
        Log.i("here","Add product "); 
        return true; //this does the trick 
       } 
       return false; 
      }  
1

我认为,如果你返回true调用,它意味着你有兴趣,直到它到达IME_ACTION_DONE事件的其余部分。因此,如果您返回错误,这意味着您对这些事件不感兴趣,其他意见将有机会处理。既然你只有1个视图,我建议你忽略actionId检查,每次只返回false。

etMovieName = (EditText) view.findViewById(R.id.et_movie_name); 
etMovieName.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView tv, int actionId, KeyEvent event) { 
     System.out.println("actionId= "+ actionId); 
     performSearch(); 
     return false; 
    } 
}); 

其他情况是,如果您有重叠视图或兄弟视图,则可以使用actionId传递信息。在这种情况下,返回true将允许您将信息传递给另一个视图。如果你真的有兴趣在事件/ actionId(例如,如果您有其他兄弟姐妹视图),那么你可以这样做:

etMovieName = (EditText) view.findViewById(R.id.et_movie_name); 

etMovieName.setImeOptions(EditorInfo.IME_ACTION_DONE); 
etMovieName.setSingleLine(); 

etMovieName.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView tv, int actionId, KeyEvent event) { 
     System.out.println("actionId= "+ actionId); 
     return performSearch();  } 
}); 

通知的actionId值已更改为6,一旦我说:

etMovieName.setImeOptions(EditorInfo.IME_ACTION_DONE); 
etMovieName.setSingleLine(); 
+1

非常奇怪的行为,但看起来你是完全正确的。 – 2016-05-02 08:31:24

相关问题