2012-05-09 56 views
2

我想添加一個鍵盤監聽器...Android的數字鍵盤

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的; import android.view.inputmethod.EditorInfo;

謝謝!

+0

請幫忙嗎?只是學習Java,如果這不明顯。只要按下軟鍵盤「完成」時即可執行操作。 – Jocala

回答

0

看起來你需要導入TextView.OnEditorActionListener

與此相關的,要注意KeyEvent參數。如果這個動作是由一個Enter鍵觸發的(這聽起來像你想要做的),它將會在這個參數中。你可以嘗試,而不是從int參數收集它。

+0

謝謝。聽衆在糾正後失敗2次錯誤。謝謝你的幫助。 – Jocala

+0

固定,謝謝! – Jocala

+0

import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; – Jocala

0

您需要在代碼中輸入android.widget.TextView.OnEditorActionListener

或者,改變從這個監聽器...

txta1cresult.setOnEditorActionListener(new OnEditorActionListener() { 

這個...

txta1cresult.setOnEditorActionListener(new TextView.OnEditorActionListener() { 

你得到編譯器錯誤,基本上說是不知道是什麼OnEditorActionListener是,所以你需要導入它。

+0

謝謝。聽衆在糾正後失敗2次錯誤。謝謝你的幫助。 – Jocala

+0

修正了,謝謝。 – Jocala