牛骨文教育服务平台(让学习变的简单)

(一).前言:

前面我们已经对于AndroidAnnotations框架的Injection标签做了讲解,今天我们开始具体学习一下事件绑定方法(Event Binding)。

FastDev4Android框架项目地址:https://github.com/jiangqqlmj/FastDev4Android 

已更新如下:

(二).@TextChange

该注解用户定义当TextView或者TextView的子类中的文本发生改变回调事件方法anroid.text.TextWatcher.onTextChanget(CharSequence s,int start,intbefore,int count)来进行处理。该注解的参数值为一个或者多个TextView或者该子类的R.id.引用。如果没有设置,那么方法名字将会作为R.id.属性名称。该方法中可以有多个参数。

  • android.widget.TextView注解知道那个view接收到了事件
  • Java.lang.CharSequence可以获取到发生改变的文本
  • Intstart表示改变文本的起始位置
  • Intbefore表示文本修改之前的长度
  • Intcount表示文本修改的数量

下面就是使用实例:

 @TextChange(R.id.helloTextView)
 void onTextChangesOnHelloTextView(CharSequencetext, TextView hello, int before, int start, int count) {
    // Something Here
 }
 
 @TextChange
 void helloTextViewTextChanged(TextView hello){
    // Something Here
 }
 
 @TextChange({R.id.editText,R.id.helloTextView})
 void onTextChangesOnSomeTextViews(TextView tv,CharSequence text) {
    // Something Here
 }
 
 @TextChange(R.id.helloTextView)
 void onTextChangesOnHelloTextView() {
    // Something Here
 }

(三).@BeforeTextChange

该用来注解回调TextView或者TextView子类文本发生变化之前的方法android.text.TextWathcher.beforeTextChanged(CharSequences,int start, int count,int after).

使用实例如下:

 @BeforeTextChange(R.id.helloTextView)
 void beforeTextChangedOnHelloTextView(TextViewhello, CharSequence text, int start, int count, int after) {
    // Something Here
 }
 
 @BeforeTextChange
 void helloTextViewBeforeTextChanged(TextViewhello) {
    // Something Here
 }
 
 @BeforeTextChange({R.id.editText,R.id.helloTextView})
 void beforeTextChangedOnSomeTextViews(TextViewtv, CharSequence text) {
    // Something Here
 }
 
 @BeforeTextChange(R.id.helloTextView)
 void beforeTextChangedOnHelloTextView() {
    // Something Here
 }

(四).@AfterTextChange

该用来注解回调TextView或者TextView子类文本发生变化之后的方法android.text.TextWathcher.afterTextChanged(Editable s)

使用实例如下:

 @AfterTextChange(R.id.helloTextView)
 void afterTextChangedOnHelloTextView(Editabletext, TextView hello) {
    // Something Here
 }
 
 @AfterTextChange
 void helloTextViewAfterTextChanged(TextViewhello) {
    // Something Here
 }
 
 @AfterTextChange({R.id.editText,R.id.helloTextView})
 void afterTextChangedOnSomeTextViews(TextViewtv, Editable text) {
    // Something Here
 }
 
 @AfterTextChange(R.id.helloTextView)
 void afterTextChangedOnHelloTextView() {
    // Something Here
 }

(五).@EditorAction

自AndroidAnnotations3.1起

该注解用来处理android.widget.TextView.OnEditorActionListener#onEditorAction(android.widget.TextView,int,android.View.KeyEvent)回调的事件.当接收处理编辑事件时候。

该注解的参数值为一个或者多个TextView或者该子类的R.id.引用。如果没有设置,那么方法名字将会作为R.id.属性名称。该方法中可以有多个参数。

  • Android.widget.TextView参数表示该接收到事件的view
  • Int参数表示事件动作的action
  • android.view.KeyEvent表示具体的事件

该方法返回的类型为void或者boolean。boolean返回true或者false表示当前事件是否被消费掉。如果返回void那么总会返回true给监听器,表示事件已经被消费掉了。

实例如下:

  @EditorAction(R.id.helloTextView)
  void onEditorActionsOnHelloTextView(TextViewhello, int actionId, KeyEvent keyEvent) {
    // Something Here
  }
 
  @EditorAction
  void helloTextViewEditorAction(TextViewhello) {
    // Something Here
  }
 
  @EditorAction({R.id.editText,R.id.helloTextView})
  void onEditorActionsOnSomeTextViews(TextViewtv, int actionId) {
    // Something Here
  }
 
  @EditorAction(R.id.helloTextView)
  void onEditorActionsOnHelloTextView() {
    // Something Here
  }
 
 
  @EditorAction(R.id.helloTextView)
  boolean onEditorActionsOnHelloTextView() {
    // Something Here
    return false;
  }

(六).@FocusChange

该注解用来定义方法当view的焦点状态发生的改变的时候回调android.view.View.OnFoucChangeListener(View view,boolean hasFocus)的方法来处理事件。

  • android.viw.View参数表示接受事件的view
  • hasFocus boolean表示获取或者失去焦点

下面是使用@FocusChange注解的实例:

@FocusChange(R.id.helloTextView)
voidfocusChangedOnHelloTextView(View hello, boolean hasFocus) {
    // Something Here
}
 
@FocusChange
voidhelloTextViewFocusChanged(View hello) {
    // Something Here
}
 
@FocusChange({R.id.editText,R.id.helloTextView})
voidfocusChangedOnSomeTextViews(View hello, boolean hasFocus) {
    // Something Here
}
 
@FocusChange(R.id.helloTextView)
voidfocusChangedOnHelloTextView() {
    // Something Here
}

(七).@CheckedChange

该注解用来定义处理当compound button状态发生改变的时候回调android.widget.CompoundButton.OnCheckedChangeListener.onCheckedChanged(CompoundButtonbuttonView,boolean isChecked)方法来处理事件;

  • android.widget.CompoundButton该参数表示接受事件的compound button
  • isChecked参数表示view是否被选中

下面为@CheckedChange注解的使用实例

@CheckedChange(R.id.helloCheckBox)
voidcheckedChangeOnHelloCheckBox(CompoundButton hello, boolean isChecked) {
    // Something Here
}
 
@CheckedChange
voidhelloCheckBoxCheckedChanged(CompoundButton hello) {
    // Something Here
}
 
@CheckedChange({R.id.aCheckBox,R.id.helloCheckBox})
voidcheckedChangedOnSomeCheckBoxs(CompoundButton hello, boolean isChecked) {
    // Something Here
}
 
@CheckedChange(R.id.helloCheckBox)
voidcheckedChangedOnHelloCheckBox() {
    // Something Here
}

(八).@Click

该@Click注解的方法当相关的view被进行点击的时候会进行回调进行处理。被点击的view的id可以设置成注解的参数;例如:@Click(R.id.myButton)

如果同样的方法来处理多个view事件,多个view的ids可以如下进行设置

@Click({R.id.myButton,R.id.myOtherButton})

如果view的id没有设置,那么默认会使用方法的名字作为view的id;

该方法可能会有一个或者没有参数,如果存在参数那么参数只可能是一个view(该被点击的view)。该方法不能为私有方法,两个不同的方法不能处理同样的view。

使用实例如下:

@Click(R.id.myButton)
voidmyButtonWasClicked() {
    [...]
}
 
@Click
void anotherButton(){
    [...]
}
 
@Click
voidyetAnotherButton(View clickedView) {
    [...]
}
 
@Click({R.id.myButton,R.id.myOtherButton})
voidhandlesTwoButtons() {
    [...]
}

(九).@ItemClick,@ItemLongClick,@ItemSelect

你可以绑定方法来处理AdapterView中item的事件

  • Item Clicks使用@ItemClick
  • Long Item Clicks使用@ItemLongClick
  • Item selection使用@ItemSelect

该注解的参数可以为一个或者多个R.id.,如果没有设置方法的名字会默认作为R.id.

  • 如果采用@ItemClick或者@ItemLongClick注解的方法必须要有一个参数,该参数为当调用adapter.getItem(positon)返回的对象的类型。 
  • 如果采用@ItemSelect注解的方法可能有一个或者两个参数,第一个参数必须要为boolean类型,第二个参数为adapter中选中位置的对象

使用实例如下:

@EActivity(R.layout.my_list)
public classMyListActivity extends Activity {
 
    // ...
 
    @ItemClick
    public void myListItemClicked(MyItemclickedItem) {
 
    }
 
    @ItemLongClick
    public void myListItemLongClicked(MyItemclickedItem) {
 
    }
 
    @ItemSelect
    public void myListItemSelected(booleanselected, MyItem selectedItem) {
 
    }
 
}

自Androidannotations2.4器

[注]对于@ItemClick,@ItemLongClick和@ItemSelect注解的方法,如果参数的类型为int,那么该代表选中索引position而不是代表adpter中选中的对象,实例如下:

@EActivity(R.layout.my_list)
public classMyListActivity extends Activity {
 
    // ...
 
    @ItemClick
    public void myListItemClicked(int position){
 
    }
 
    @ItemLongClick
    public void myListItemLongClicked(intposition) {
 
    }
 
    @ItemSelect
    public void myListItemSelected(booleanselected, int position) {
 
    }
 
}

(十).@OptionsItem

10.1.处理可选菜单

自AndroidAnnotations2.2起

通过@OptionsMenu和@OptionsItem注解你可以在Activity中很快添加菜单功能。

  • @OptionsMenu表示菜单资源
  • @OptionsItem标示选中某个菜单

下面是一个简单的例子:

@EActivity
@OptionsMenu(R.menu.my_menu)
public classMyActivity extends Activity {
 
    @OptionsMenuItem
    MenuItem menuSearch;
 
    @OptionsItem(R.id.menuShare)
    void myMethod() {
      // You can specify the ID in theannotation, or use the naming convention
    }
 
    @OptionsItem
    void homeSelected() {
      // home was selected in the action bar
      // The "Selected" keyword isoptional
    }
 
    @OptionsItem
    boolean menuSearch() {
      menuSearch.setVisible(false);
      // menuSearch was selected
      // the return type may be void or boolean(false to allow normal menu processing to proceed, true to consume it here)
      return true;
    }
 
    @OptionsItem({ R.id.menu_search,R.id.menu_delete })
    void multipleMenuItems() {
      // You can specify multiple menu item IDsin @OptionsItem
    }
 
    @OptionsItem
    void menu_add(MenuItem item) {
      // You can add a MenuItem parameter toaccess it
    }
}

10.2.注入菜单项

自AndroidAnnotations3.0起

@OptionsMenuItem可以在属性中进行注入一个MenuItem

【注】由于Activity生命周期的原因,注入的menuitem是不能在@AfterInject或者@AfterView注解的方法中进行使用。

10.3.注入菜单

自AndroidAnnotations4.0开始

@OptionsMenuItem可以在在Fragment或者Activity中注入Menu对象

【注】该menu和@OptionsMenuItem一样,不能在@AfterInject或者@AfterView注解的方法中进行使用。

使用实例如下:

@EActivity
public classMyActivity extends Activity {
 
    @InjectMenu
    Menu menu;
 
}

10.4.多个可选菜单

自AndroidAnnotations2.7起,可以使用@OptionsMenu来进行联合多个xmlmenus注入多个可选菜单

@EActivity
@OptionsMenu({R.menu.my_menu1,R.menu.my_menu2})
public classMyActivity extends Activity {
 
}

10.5.Fragment支持

自AndroidAnnotations2.7起,你可以在Fragment中使用@OptionsMenu和@OptionsItem

@EFragment
@OptionsMenu(R.menu.my_fragment_menu)
public classMyFragment extends Fragment {
 
    @OptionsItem
    void menuRefreshSelected() {
    }
 
}

(十一).@SeekBarProgressChange

我们可以绑定方法来处理来自SeekBarView的方法。使用@SeekBarProgressChange注解的方法等同于当SeekBar的进度发生改变的时候回调SeekBar.onSeekBarChangeListener.onProgressChanged(SeekBar,int,boolean)方法来处理事件。参数说明

  • Android.widget.SeekBar参数表示接受的事件的view
  • int参数表示SeekBar的进度
  • boolean参数表示是否由用户触发

以上所有的这些参数都可以可选的,使用实例如下:

@SeekBarProgressChange(R.id.seekBar)
 void onProgressChangeOnSeekBar(SeekBarseekBar, int progress, boolean fromUser) {
    // Something Here
 }
 
 @SeekBarProgressChange(R.id.seekBar)
 void onProgressChangeOnSeekBar(SeekBarseekBar, int progress) {
    // Something Here
 }
 
 @SeekBarProgressChange({R.id.seekBar1,R.id.seekBar2})
 void onProgressChangeOnSeekBar(SeekBarseekBar) {
    // Something Here
 }
 
 @SeekBarProgressChange({R.id.seekBar1,R.id.seekBar2})
 void onProgressChangeOnSeekBar() {
    // Something Here
 }

(十二).@SeekBarTouchStart,@SeekBarTouchStop

这两个注解方法等同于当用户开始或者完成对SeekBar的拖动回调SeekBar.onSeekBarChangeListener.onStartTrackingTouch(SeekBarseekBar)和SeekBar.OnSeekBarChangeListener.onStopTrackingTouch(SeekBar seekBar)方法来处理事件。

(十三).@KeyDown,@KeyUp,@KeyLongPress,@KeyMultiple

13.1.事件介绍

自AndroidAnnotations4.0开始。你可以很方便的处理KeyEvent.Callback接口中的四个事件。更多信息可以查看KeyEevent.Callback文档。四个事件的注入如下:

  • @KeyDown
  • @KeyUp
  • @KeyLongPress
  • @KeyMultiple

这四个注解可以在参数中设置keycode或者keycodes。通过KeyEvent.KEYCODE_*常量也可以设置。例如:KeyDown(KeyEevent.KEYCODE_ENTER)或者KeyDown({})

如果这个key code没有被设置,那么方法的名字默认用于key code。如果回车事件被调用,那么可能回调用的事件有enter,onEnter,enterPressed,onEnterPressed。所有的被注解的方法都可以返回void,boolean或者Boolean,如果返回void,那么将永远返回true(该代表已经处理事件)

13.2.@KeyDown,@KeyUp,@KeyLongPress

该被注解的方法考可以为一个或者零个参数。如果没有参数也必须为KeyEvent。该方法不能为私有,两个不同的方法在一个类中不能处理同一个事件,使用实例:

@EActivity
public classMyActivity extends Activity {
 
    @KeyDown
    void enterPressed() {
        //...
    }
 
    @KeyUp(KeyEvent.KEYCODE_ESCAPE)
    boolean handleEscapeActionUpEvent() {
        //...
        return false;
    }
 
    @KeyLongPress({ KeyEvent.KEYCODE_F,KeyEvent.KEYCODE_G })
    void fOrGKeyLongPress(KeyEvent keyEvent) {
        //...
    }
}

13.3.@KeyMultiple

该被注解的方法可以没有参数,一个或者两个参数。

使用实例如下:

@EActivity
public classMyActivity extends Activity {
 
    @KeyMultiple
    void onEnterPressed() {
        //...
    }
 
    @KeyMultiple(KeyEvent.KEYCODE_ESCAPE)
    boolean handleEscapeActionMultipleEvent(intcount) {
        //...
        return false;
    }
 
    @KeyMultiple({ KeyEvent.KEYCODE_F,KeyEvent.KEYCODE_G })
    void fOrGKeyWasMultiplePressed(int count,KeyEvent keyEvent) {
        //...
    }
}