AndroidAnnotations学习笔记--事件(三)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > AndroidAnnotations学习笔记--事件(三)

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

 2015/1/28 9:43:50  hista  程序员俱乐部  我要评论(0)
  • 摘要:@TextChange这个注解是用于接收android.text.TextWatcher.onTextChanged(CharSequences,intstart,intbefore,intcount)Android定义的事件。未使用@TextChange之前,我们的代码要这样写:@EActivity(R.layout.activity_test)publicclassTestActivityextendsActivity{@ViewById(R.id.tv_test
  • 标签:事件 笔记 android 学习 not Annotation 学习笔记
@TextChange
这个注解是用于接收 android.text.TextWatcher.onTextChanged(CharSequence s, int start, int before, int count) Android定义的事件。

未使用@TextChange之前,我们的代码要这样写:
class="java" name="code">
@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()
	{
		
	}
}

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

@BeforeTextChange
这个注解是用于接收 android.text.TextWatcher.beforeTextChanged(CharSequence s, int start, int count, int after) 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
	}
}

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

@AfterTextChange
这个注解是用于接收 android.text.TextWatcher.afterTextChanged(Editable s) 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
	}
}

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

@EditorAction
这个注解是用于接收 android.widget.TextView.OnEditorActionListener#onEditorAction(android.widget.TextView, int, android.view.KeyEvent) 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;
	}
}

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

@FocusChange
这个注解是用于接收 android.view.View.OnFocusChangeListener.onFocusChange(View view, boolean hasFocus) 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
	}
}

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

@CheckedChange

这个注解是用于接收 android.widget.CompoundButton.OnCheckedChangeListener.onCheckedChanged(CompoundButton buttonView, boolean isChecked) 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
	}
}

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

@Click
未使用@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");
		}
	}
}


@LongClick
@Touch


@ItemClick,@ItemLongClick,@ItemSelect
未使用注解时,我们的代码是这个样子地:
@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));
    }
}

void listviewTest(String clickedItem)
这里的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));
    }
}
发表评论
用户名: 匿名