2

我正在尝试添加键盘侦听器...

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

但是,我收到以下编译器错误...

/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() {        

这是我的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>

我需要导入和以外的东西EditTextTextView?这里还有什么问题吗?

 [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 个错误:

[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

它似乎扼杀了这段代码:

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

最后修复:

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

谢谢!

4

2 回答 2

0

看起来你需要导入TextView.OnEditorActionListener

相关,注意KeyEvent参数。如果操作是由 Enter 键触发的(听起来像您想要做的),它将在该参数中。您可以尝试这样做,而不是从int参数中收集它。

于 2012-05-10T14:03:26.470 回答
0

您需要android.widget.TextView.OnEditorActionListener在代码中导入。

或者,从这个改变你的听众......

txta1cresult.setOnEditorActionListener(new OnEditorActionListener() {

对这个...

txta1cresult.setOnEditorActionListener(new TextView.OnEditorActionListener() {

你得到的编译器错误基本上是说不知道是什么OnEditorActionListener,所以你需要导入它。

于 2012-05-10T14:04:41.163 回答