如何在ActionBarActivity的片段中处理android:imeOptions =“actionNext”,“actionDone”?

时间:2014-09-03 14:40:49

标签: android android-edittext

我想处理" actionNext"和" actionDone"在ActionBarActivity中做片断的事情,但它无法奏效。 当我调用setActionListeners()时出现错误,但我不知道如何解决它。

我的代码:

public void setActionListeners() {
    fir_ET.setOnEditorActionListener((OnEditorActionListener) this);
    sec_ET.setOnEditorActionListener((OnEditorActionListener) this);
}

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
    switch(actionId){          
    case EditorInfo.IME_ACTION_NEXT:
        Toast.makeText(getActivity(), "next:" + v.getText(), Toast.LENGTH_SHORT).show();
        break; 
    case EditorInfo.IME_ACTION_DONE:  
        Toast.makeText(getActivity(), "done:" + v.getText(), Toast.LENGTH_SHORT).show();
        break;  
    }         
    return true;  
}  

logcat的:

09-04 12:12:49.150: E/Trace(1778): error opening trace file: No such file or directory (2)
09-04 12:14:58.180: E/AndroidRuntime(1778): FATAL EXCEPTION: main
09-04 12:14:58.180: E/AndroidRuntime(1778): java.lang.ClassCastException: com.example.rtev_t3_2.WriteData cannot be cast to android.widget.TextView$OnEditorActionListener
09-04 12:14:58.180: E/AndroidRuntime(1778):     at com.example.rtev_t3_2.WriteData.setActionListeners(WriteData.java:132)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at com.example.rtev_t3_2.WriteData.initView(WriteData.java:76)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at com.example.rtev_t3_2.WriteData.onCreateView(WriteData.java:61)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.app.Fragment.performCreateView(Fragment.java:1695)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.os.Handler.handleCallback(Handler.java:725)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.os.Looper.loop(Looper.java:137)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at android.app.ActivityThread.main(ActivityThread.java:5039)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at java.lang.reflect.Method.invoke(Method.java:511)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-04 12:14:58.180: E/AndroidRuntime(1778):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

从您的logcat看起来您实际上还没有实现OnEditorActionListener接口。简单地添加onEditorAction()方法并不能使您的班级成为OnEditorActionListener;你仍然需要实现界面。

你的课应该是这样的:

public class MyClass implements OnEditorActionListener {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        // Do stuff
    }
}

这也消除了将课程强制转换为OnEditorActionListener的需要,而不是

fir_ET.setOnEditorActionListener((OnEditorActionListener) this);

您只需致电

即可
fir_ET.setOnEditorActionListener(this);

答案 1 :(得分:0)

您的类WriteData必须实现OnEditorActionListener以此方式设置侦听器

fir_ET.setOnEditorActionListener((OnEditorActionListener) this);
sec_ET.setOnEditorActionListener((OnEditorActionListener) this);