来自所有片段的EditText事件调用

时间:2014-10-20 13:46:31

标签: java android android-fragments android-button butterknife

我想知道我是否可以保存我在应用程序的所有片段中编写的代码块。

片段Nro 1中的代码是:

public class Fragment1 extends Fragment {

@InjectView(R.id.txtRP) EditText txtRP;

public static Fragment1 newInstance() {
    Fragment1 fragment = new Fragment1();
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
    ButterKnife.inject(this, rootView);
    return rootView;
}

@OnEditorAction(R.id.txtRP)
boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_ACTION_NEXT){
        return true;
    }
    if ((event == null && (actionId == EditorInfo.IME_ACTION_DONE)) || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)){
        if (event == null || event.getAction() == KeyEvent.ACTION_DOWN){
            buscaRP();
            return true;
        }
    }
    return true;
}

private void buscaRP(){
    Toast.makeText(getActivity(), "Button in fragment 1.", Toast.LENGTH_SHORT).show();
}

@Override 
public void onDestroyView() {
    super.onDestroyView();
    ButterKnife.reset(this);
}

}

如您所见,EditText名为“ txtRP ”,在事件onEditorAction中调用函数“ buscaRP()”。

在其他3个片段中是相同的,所以......

如何保存该代码块而不必在所有片段中声明事件onEditorAction?我可以在单独的类中创建事件onEditorAction并从那里调用它吗?

提前致谢!!!

0 个答案:

没有答案