AndroidAnnotations学习笔记--事件(三)

[size=large][color=red][b]@TextChange[/b][/color][/size]
这个注解是用于接收 [i]android.text.TextWatcher.onTextChanged(CharSequence s, int start, int before, int count)[/i] Android定义的事件。

未使用@TextChange之前,我们的代码要这样写:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@ViewById
EditText edit_test;

@AfterViews
void afterView(){

edit_test.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
tvTest.setText(s);
}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}

});

}
}


使用这个注解之后我们的代码可以按下面的几种方式写:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@TextChange
void edit_test(CharSequence text, TextView test, int before, int start, int count)
{
tvTest.setText(text);
}

@TextChange(R.id.edit_test)
void test1(CharSequence text, TextView test)
{
tvTest.setText(text);
}

@TextChange(R.id.edit_test)
void test2(TextView text)
{
tvTest.setText(text.getText());
}

@TextChange(R.id.edit_test)
void test3()
{

}
}

支持多种的输入参数
[i]@TextChange({R.id.edit_test, R.id.helloTextView})[/i]
支持多个View绑定一个BeforeTextChange
[i]@TextChange void helloTextViewTextChanged(TextView edit_test)[/i]
可以在参数中指定View

[size=large][color=red][b]@BeforeTextChange[/b][/color][/size]
这个注解是用于接收 [i]android.text.TextWatcher.beforeTextChanged(CharSequence s, int start, int count, int after)[/i] Android定义的事件。
未使用@BeforeTextChange注解之前我们这样写代码

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@ViewById
EditText edit_test;

@AfterViews
void afterView(){

edit_test.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
tvTest.setText(s);
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}

});

}
}


使用这个注解之后,我们可以按以下的多种方法写:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@BeforeTextChange(R.id.edit_test)
void beforeTextChangedOnHelloTextView(TextView tv, CharSequence text, int start, int count, int after) {
// Something Here
tvTest.setText(text);
}

@BeforeTextChange(R.id.edit_test)
void helloTextViewBeforeTextChanged(TextView tv) {
// Something Here
tvTest.setText(tv.getText());
}

@BeforeTextChange({R.id.edit_test, R.id.helloTextView})
void beforeTextChangedOnSomeTextViews(TextView tv, CharSequence text) {
// Something Here
tvTest.setText(text);
}

@BeforeTextChange(R.id.edit_test)
void beforeTextChangedOnHelloTextView() {
// Something Here
}
}

支持多种的输入参数
[i]@BeforeTextChange({R.id.edit_test, R.id.helloTextView})[/i]
支持多个View绑定一个BeforeTextChange
[i]@BeforeTextChange void helloTextViewBeforeTextChanged(TextView edit_test)[/i]
可以在参数中指定View

[size=large][color=red][b]@AfterTextChange[/b][/color][/size]
这个注解是用于接收 [i]android.text.TextWatcher.afterTextChanged(Editable s)[/i] Android定义的事件。
没使用@AfterTextChange这个注解之前我们的代码是这样的

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@ViewById
EditText edit_test;

@AfterViews
void afterView(){

edit_test.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
tvTest.setText(s);
}

});

}
}


使用这个注解之后,我们可以有这样的写法:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@AfterTextChange(R.id.edit_test)
void afterTextChangedOnHelloTextView(Editable text, TextView hello) {
// Something Here
tvTest.setText(text);
}

@AfterTextChange
void helloTextViewAfterTextChanged(TextView edit_test) {
// Something Here
tvTest.setText(edit_test.getText());
}

@AfterTextChange({R.id.edit_test, R.id.helloTextView})
void afterTextChangedOnSomeTextViews(TextView tv, Editable text) {
// Something Here
tvTest.setText(text);
}

@AfterTextChange(R.id.edit_test)
void afterTextChangedOnHelloTextView() {
// Something Here
}
}

支持多种的输入参数
[i]@BeforeTextChange({R.id.edit_test, R.id.helloTextView})[/i]
支持多个View绑定一个BeforeTextChange
[i]@AfterTextChange void helloTextViewAfterTextChanged(TextView edit_test)[/i]
可以在参数中指定View

[size=large][color=red][b]@EditorAction[/b][/color][/size]
这个注解是用于接收 [i]android.widget.TextView.OnEditorActionListener#onEditorAction(android.widget.TextView, int, android.view.KeyEvent)[/i] Android定义的事件。
在未使用@EditorAction 这个注解之前,我们要这么写代码:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@ViewById
EditText edit_test;

@AfterViews
void afterView(){

edit_test.setOnEditorActionListener(new OnEditorActionListener() {

@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// TODO Auto-generated method stub
tvTest.setText(actionId);
return false;
}
});

}
}


有了这个注解我们就可以这样写代码了:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@EditorAction(R.id.edit_test)
void onEditorActionsOnHelloTextView(TextView hello, int actionId, KeyEvent keyEvent) {
// Something Here
tvTest.setText(actionId);
}

@EditorAction
void helloTextViewEditorAction(TextView edit_test) {
// Something Here
tvTest.setText(edit_test.getId());
}

@EditorAction({R.id.edit_test, R.id.helloTextView})
void onEditorActionsOnSomeTextViews(TextView tv, int actionId) {
// Something Here
tvTest.setText(actionId);
}

@EditorAction(R.id.edit_test)
void onEditorActionsOnHelloTextViewText() {
// Something Here
}


@EditorAction(R.id.edit_test)
boolean onEditorActionsOnHelloTextView() {
// Something Here
return false;
}
}

支持多种的输入参数
[i]@EditorAction({R.id.edit_test, R.id.helloTextView})[/i]
支持多个View绑定一个EditorAction
[i]@EditorAction void helloTextViewEditorAction(TextView edit_test)[/i]
可以在参数中指定View

[size=large][color=red][b]@FocusChange[/b][/color][/size]
这个注解是用于接收 [i]android.view.View.OnFocusChangeListener.onFocusChange(View view, boolean hasFocus)[/i] Android定义的事件。

在没有@FocusChange这个注解之前我们的代码要这样写:


@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@ViewById
EditText edit_test;

@AfterViews
void afterView(){

edit_test.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View view, boolean arg1) {
// TODO Auto-generated method stub
setObject(String.valueOf(view.getId()));
}
});
}

@UiThread
void setObject(String s){
tvTest.setText(s);
}
}


有了这个注解之后,我们的代码可以这样写:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@UiThread
void setObject(String s){
tvTest.setText(s);
}

@FocusChange(R.id.edit_test)
void focusChangedOnHelloTextView(View hello, boolean hasFocus) {
// Something Here
setObject(String.valueOf(hello.getId()));
}

@FocusChange
void helloTextViewFocusChanged(View edit_test) {
// Something Here
setObject(String.valueOf(edit_test.getId()));
}

@FocusChange({R.id.edit_test, R.id.helloTextView})
void focusChangedOnSomeTextViews(View hello, boolean hasFocus) {
// Something Here
setObject(String.valueOf(hello.getId()));
}

@FocusChange(R.id.edit_test)
void focusChangedOnHelloTextView() {
// Something Here
}
}

支持多种的输入参数
[i]@FocusChange({R.id.edit_test, R.id.helloTextView})[/i]
支持多个View绑定一个EditorAction
[i]@FocusChange void helloTextViewFocusChanged(View edit_test)[/i]
可以在参数中指定View

[size=large][color=red][b]@CheckedChange[/b][/color][/size]

这个注解是用于接收 [i]android.widget.CompoundButton.OnCheckedChangeListener.onCheckedChanged(CompoundButton buttonView, boolean isChecked)[/i] Android定义的事件

在没有@CheckedChange 这个注解这前,我们的代码是这样的:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@ViewById
CheckBox checkboxText;

@AfterViews
void afterView(){

checkboxText.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
tvTest.setText(String.valueOf(isChecked));
}
});
}
}


在使用了这个注解之后,我们的代码就可以这样写了:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@CheckedChange(R.id.checkboxText)
void checkedChangeOnHelloCheckBox(CompoundButton hello, boolean isChecked) {
// Something Here
tvTest.setText(String.valueOf(isChecked));
}

@CheckedChange
void helloCheckBoxCheckedChanged(CompoundButton hello) {
// Something Here
tvTest.setText(String.valueOf(hello.isChecked()));
}

@CheckedChange({R.id.checkboxText, R.id.helloCheckBox})
void checkedChangedOnSomeCheckBoxs(CompoundButton hello, boolean isChecked) {
// Something Here
tvTest.setText(String.valueOf(isChecked));
}

@CheckedChange(R.id.checkboxText)
void checkedChangedOnHelloCheckBox() {
// Something Here
}
}

支持多种的输入参数
[i]@CheckedChange({R.id.checkboxText, R.id.helloCheckBox})[/i]
支持多个View绑定一个EditorAction
[i]@CheckedChange void helloCheckBoxCheckedChanged(CompoundButton hello)[/i]
可以在参数中指定View

[size=large][color=red][b]@Click[/b][/color][/size]
未使用@Click注解之前我们这样写代码


@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;
@ViewById
Button bt_one;
@ViewById
Button bt_two;

@AfterViews
void afterView(){

bt_one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tvTest.setText("111111111111");
}
});
bt_two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tvTest.setText("22222222222");
}
});

}

}



使用后我们可以这样写

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@Click
void bt_one(){
tvTest.setText("111111111111");
}

@Click(R.id.bt_two)
void bttwoOnclick(){
tvTest.setText("222222222222");
}

}


同样的方法也可以处理多个View,多View的如下格式:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

@Click({R.id.bt_one, R.id.bt_two})
void clickbt(View v){
if(v.getId() == R.id.bt_one){
tvTest.setText("111111111111");
}
else if(v.getId() == R.id.bt_two){
tvTest.setText("222222222222");
}
}
}


[size=large][color=red][b]@LongClick[/b][/color][/size]
[size=large][color=red][b]@Touch[/b][/color][/size]


[size=large][color=red][b]@ItemClick,@ItemLongClick,@ItemSelect[/b][/color][/size]
未使用注解时,我们的代码是这个样子地:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

private String[] mListStr = {"11111111","22222222","33333333","44444444","55555555"};
@ViewById
ListView listviewTest;
@ViewById
Spinner spinnerTest;

@AfterViews
void afterView(){

spinnerTest.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mListStr));

listviewTest.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mListStr));
listviewTest.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tvTest.setText("点击第"+position+"个项目");
}
});

listviewTest.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
tvTest.setText("长点击第"+position+"个项目");
return false;
}
});

spinnerTest.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
tvTest.setText("选中了第"+position+"个项目");
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
});
}
}


如果使用了了上面的注解后,代码可以这样写:

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

private String[] mListStr = {"11111111","22222222","33333333","44444444","55555555"};
@ViewById
ListView listviewTest;
@ViewById
Spinner spinnerTest;

@ItemClick
void listviewTest(String clickedItem){
tvTest.setText("点击个项目" + clickedItem);
}
@ItemLongClick(R.id.listviewTest)
public void myListItemLongClicked(String clickedItem) {
tvTest.setText("长点击个项目" + clickedItem);
}

@ItemSelect
public void spinnerTest(boolean selected, String selectedItem) {
tvTest.setText("选中了项目"+selectedItem);
}

@AfterViews
void afterView(){

spinnerTest.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mListStr));

listviewTest.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mListStr));
}
}

[i]void listviewTest(String clickedItem)[/i]
这里的String是Item的类型,这里根据你的类型写。

也可以这样写

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {

@ViewById(R.id.tv_test)
TextView tvTest;

private String[] mListStr = {"11111111","22222222","33333333","44444444","55555555"};
@ViewById
ListView listviewTest;
@ViewById
Spinner spinnerTest;

@ItemClick
public void listviewTest(int position) {
tvTest.setText("点击第"+position+"个项目");
}

@ItemLongClick(R.id.listviewTest)
public void myListItemLongClicked(int position) {
tvTest.setText("长点击第"+position+"个项目");
}

@ItemSelect
public void spinnerTest(boolean selected, int position) {
tvTest.setText("选中了第"+position+"个项目");
}

@AfterViews
void afterView(){

spinnerTest.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mListStr));

listviewTest.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mListStr));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值