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

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

AndroidAnnotations学习笔记--事件续(四)

 2015/1/28 13:43:25  hista  程序员俱乐部  我要评论(0)
  • 摘要:@OptionsMenu、@OptionsMenuItem、@OptionsItem@OptionsMenu:用来指定菜单资源@OptionsMenuItem:MenuItem的实例@OptionsItem:用来接收菜单的选中事件没有上面的注解,我们要这样写代码@EActivity(R.layout.activity_test)publicclassTestActivityextendsActivity{MenuItemitemhome
  • 标签:事件 笔记 android 学习 not Annotation 学习笔记
@OptionsMenu、@OptionsMenuItem、@OptionsItem

@OptionsMenu :用来指定菜单资源
@OptionsMenuItem :MenuItem的实例
@OptionsItem :用来接收菜单的选中事件

没有上面的注解,我们要这样写代码
class="java" name="code">
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
	
	MenuItem itemhome;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(com.hista.weiweilove.R.menu.mymenu, menu);
        itemhome = menu.findItem(R.id.menu_home);
        return super.onCreateOptionsMenu(menu);
    }
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
	       case R.id.menu_home:	       
	           Toast.makeText(TestActivity.this, ""+"home", Toast.LENGTH_SHORT).show();
	           itemhome.setTitle("主页君");
	           break;
	       case R.id.menu_search:
	           
	           Toast.makeText(TestActivity.this, ""+"search", Toast.LENGTH_SHORT).show();
	           break;
	       case R.id.menu_share:
	           
	           Toast.makeText(TestActivity.this, ""+"share", Toast.LENGTH_SHORT).show();
	           break;
	       default:
	    	   break;
	       }
	
		return super.onOptionsItemSelected(item);
	}
}


有了上面的注解我们可以这样写代码

@EActivity(R.layout.activity_test)
@OptionsMenu(R.menu.mymenu)
public class TestActivity extends Activity {
	
	@OptionsMenuItem
    MenuItem menu_home;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@OptionsItem(R.id.menu_share)
    void myMethod() {
		// You can specify the ID in the annotation, or use the naming convention
		Toast.makeText(TestActivity.this, ""+"share", Toast.LENGTH_SHORT).show();
    }

    @OptionsItem
    void menu_home() {
    	// home was selected in the action bar
    	// The "Selected" keyword is optional
    	Toast.makeText(TestActivity.this, ""+"home", Toast.LENGTH_SHORT).show();
    	menu_home.setTitle("主页君");
    }

    @OptionsItem
    boolean menu_search() {
    	Toast.makeText(TestActivity.this, ""+"search", Toast.LENGTH_SHORT).show();
    	// 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_share })
    void multipleMenuItems() {
    	// You can specify multiple menu item IDs in @OptionsItem
    	Toast.makeText(TestActivity.this, ""+"search or share", Toast.LENGTH_SHORT).show();
    }
}


@OptionsItem({ R.id.menu_search, R.id.menu_share }) 支持多个MenuItem


@SeekBarProgressChange

在未使用这个注解前,我们要写这样的代码
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
	
	@ViewById(R.id.tv_test)
	TextView tvTest;
	
	@ViewById
	SeekBar seekbar1;
	@ViewById
	SeekBar seekbar2;
	
	@UiThread
	void setObject(String s){
		tvTest.setText(s);
	}
	
	@AfterViews
	void afterView(){
		
		seekbar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {
        	public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser)
        	{
        		setObject(String.valueOf(progress));  
        	}
        	
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				Toast.makeText(TestActivity.this, "seekbar touch stare", 100).show();
			}
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				Toast.makeText(TestActivity.this, "seekbar touch end", 100).show();
			}
        });
    }
}


使用之后我们可以这样写代码:
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
	
	@ViewById(R.id.tv_test)
	TextView tvTest;
	
	@ViewById
	SeekBar seekbar1;
	@ViewById
	SeekBar seekbar2;
	
	@UiThread
	void setObject(String s){
		tvTest.setText(s);
	}
	
	@SeekBarProgressChange(R.id.seekbar1)
	 void onProgressChangeOnSeekBar(SeekBar seekBar, int progress, boolean fromUser) {
	    // Something Here
		setObject(String.valueOf(progress)); 
	 }

	 @SeekBarProgressChange(R.id.seekbar1)
	 void onProgressChangeOnSeekBar(SeekBar seekBar, int progress) {
	    // Something Here
		 setObject(String.valueOf(progress)); 
	 }

	 @SeekBarProgressChange({R.id.seekbar1, R.id.seekbar2})
	 void onProgressChangeOnSeekBar(SeekBar seekBar) {
	    // Something Here
		 setObject(String.valueOf(seekBar.getProgress())); 
	 }

	 @SeekBarProgressChange({R.id.seekbar1, R.id.seekbar2})
	 void onProgressChangeOnSeekBar() {
	    // Something Here
	 }
}

@SeekBarProgressChange({R.id.seekbar1, R.id.seekbar2}) :支持绑定多个View
同样,与前面的几个事件一样,也支持多种参数形式

@SeekBarTouchStart、@SeekBarTouchStop

原有代码书写方式见前面那个注解,这里就不重复了

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

@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
	
	@ViewById(R.id.tv_test)
	TextView tvTest;
	
	@ViewById
	SeekBar seekbar1;
	
	@UiThread
	void setObject(String s){
		tvTest.setText(s);
	}
	
	@SeekBarTouchStart(R.id.seekbar1)
	void seekbarStart(SeekBar seekBar)
	{
		Toast.makeText(TestActivity.this, "seekbar touch stare", 100).show();
	}
	
	@SeekBarTouchStop(R.id.seekbar1)
	void seekbarStop(SeekBar seekBar)
	{
		Toast.makeText(TestActivity.this, "seekbar touch stop", 100).show();
	}
}


至这里AndroidAnnotations的事件的注解就全部结束了
上一篇: 买买买!国行版Xbox One直降900元 下一篇: 没有下一篇了!
发表评论
用户名: 匿名