Java 来自所有片段的EditText事件调用
我想知道我是否可以保存我在应用程序的所有片段中编写的代码块 片段Nro 1中的代码如下: } 如您所见,EditText调用txtRP,在事件onEditorAction中调用函数buscaRP 在其他3个片段中是相同的,所以 如何保存该代码块而不必在所有片段中声明事件onEditorAction?。我可以在单独的类中创建event onEditorAction并从那里调用它吗Java 来自所有片段的EditText事件调用,java,android,android-fragments,android-button,butterknife,Java,Android,Android Fragments,Android Button,Butterknife,我想知道我是否可以保存我在应用程序的所有片段中编写的代码块 片段Nro 1中的代码如下: } 如您所见,EditText调用txtRP,在事件onEditorAction中调用函数buscaRP 在其他3个片段中是相同的,所以 如何保存该代码块而不必在所有片段中声明事件onEditorAction?。我可以在单独的类中创建event onEditorAction并从那里调用它吗 提前感谢 换句话说,你想扩展Fragment1而不是Fragment1我不想在所有的Fragments中重复事件的代码
提前感谢 换句话说,你想扩展Fragment1而不是Fragment1我不想在所有的Fragments中重复事件的代码你试试看,也就是说,你是否尝试在单独的类中创建事件?如果你只是扩展Fragment1,那么你就不必重复代码了。
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);
}