简体   繁体   English

Android数字键盘

[英]Android numeric keyboard

I am trying to add a keyboard listener... 我正在尝试添加键盘监听器...

txta1cresult.setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView v,int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            calculate();
        }
    return false;
    }
});  

However, I am getting the following compiler error... 但是,我收到以下编译器错误...

/home/jocala/hba1c/src/com/android/hba1c.java:82: cannot find symbol
symbol  : class OnEditorActionListener
location: class com.jocala.hba1c.hba1c
txta1cresult.setOnEditorActionListener(new OnEditorActionListener() {        

This is my EditText ... 这是我的EditText ...

<EditText
    android:id="@+id/txta1cresult"
    android:inputType="numberDecimal"
    android:layout_width="80px"
    android:maxLength="5"
    android:layout_height="40px"
    android:textSize="18sp"
    android:layout_x="200px"
    android:layout_y="32px"
    >
</EditText>

Do I need to import something other than EditText and TextView ? 我是否需要导入EditTextTextView以外的其他内容? Is there something else wrong here? 这里还有其他问题吗?

 [javac] Compiling 3 source files to /home/jeff/hba1c/bin/classes
  [javac] /home/jeff/hba1c/src/com/android/hba1c.java:83: cannot find symbol
  [javac] symbol: class KeyEvent
  [javac]     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
     [javac]                                                       ^
[javac] /home/jeff/hba1c/src/com/android/hba1c.java:84: cannot find symbol
[javac] symbol: variable EditorInfo
[javac]         if(actionId==EditorInfo.IME_ACTION_DONE){
[javac]                      ^
[javac] 2 errors

2 errors remaining after fixing the import: 修复导入后还剩下2个错误:

[javac] Compiling 2 source files to /home/jeff/hba1c/bin/classes
[javac] /home/jeff/hba1c/src/com/android/hba1c.java:161: cannot find symbol
[javac] symbol: class KeyEvent
[javac]     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
[javac]                                                             ^
[javac] /home/jeff/hba1c/src/com/android/hba1c.java:162: cannot find symbol
[javac] symbol: variable EditorInfo
[javac]         if(actionId==EditorInfo.IME_ACTION_DONE){
[javac]                      ^
[javac] 2 errors

It appears to choke on this code: 似乎使以下代码窒息:

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if(actionId==EditorInfo.IME_ACTION_DONE){
        calculate();
    }

Finally fixed with: 最后修复:

import android.view.KeyEvent; 导入android.view.KeyEvent; import android.view.inputmethod.EditorInfo; 导入android.view.inputmethod.EditorInfo;

Thanks! 谢谢!

It looks like you need to import TextView.OnEditorActionListener 看来您需要导入TextView.OnEditorActionListener

Relatedly, pay attention to the KeyEvent parameter. 相关地,请注意KeyEvent参数。 If the action was triggered by an Enter key (which sounds like what you want to do), it will be in that parameter. 如果该操作是由Enter键触发的(这听起来像您要执行的操作),它将在该参数中。 You might try that instead of gleaning it from the int parameter. 您可以尝试这样做,而不是从int参数中收集它。

You need to import android.widget.TextView.OnEditorActionListener in your code. 您需要在代码中导入android.widget.TextView.OnEditorActionListener

Or, change your listener from this... 或者,从此更改您的听众...

txta1cresult.setOnEditorActionListener(new OnEditorActionListener() {

to this... 为此...

txta1cresult.setOnEditorActionListener(new TextView.OnEditorActionListener() {

The compiler error you're getting is basically saying that is doesn't know what OnEditorActionListener is, so you need to import it. 您得到的编译器错误基本上是在说什么不知道OnEditorActionListener是什么,因此您需要导入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM