android SDK开发 — TitleBar封装(二)

最后更新于:2022-04-01 10:05:59

android SDK开发 -- TitleBar封装 (一) [点击打开链接](http://blog.csdn.net/id19870510/article/details/50386067) 这一篇讲述一下TitleBar怎么增加交互 BaseActivity 中的逻辑实现 ~~~ private ViewSwitcher[] titleSwitcher; /** * 初始化View */ protected void findViewById() { titleSwitcher = new ViewSwitcher[3]; titleSwitcher[0] = (ViewSwitcher) findViewById(R.id.app_title_left_switcher); titleSwitcher[1] = (ViewSwitcher) findViewById(R.id.app_title_middle_switcher); titleSwitcher[2] = (ViewSwitcher) findViewById(R.id.app_title_right_switcher); } protected void setTitle(TitleRes leftTitle, TitleRes middleTitle, TitleRes rightTitle) { TitleRes[] reses = new TitleRes[] { leftTitle, middleTitle, rightTitle }; for (int i = 0; i < reses.length; i++) { TitleRes res = reses[i]; ViewSwitcher switcher = titleSwitcher[i]; if (res == null) { switcher.setVisibility(View.INVISIBLE); } else { switcher.setVisibility(View.VISIBLE); // 触发单击事件的View View clickView = null; // 文字 if (res.getType() == 0) { switcher.setDisplayedChild(0);// 显示TextView // Object resource = res.getResource(); String title = resource instanceof Integer ? getResources().getString((Integer) resource) : String.valueOf(resource); ((TextView) switcher.getChildAt(0)).setText(title); clickView = switcher.getChildAt(0); } else { switcher.setDisplayedChild(1);// 显示ImageView // int imageResource = (Integer) res.getResource(); ((ImageView) switcher.getChildAt(1)).setImageResource(imageResource); clickView = switcher.getChildAt(1); } // 设置title的单击事件 clickView.setOnClickListener(res.getClickListener()); } } } ~~~ 自雷TitleSampleActivity 调用如下 ~~~ @Override protected void findViewById() { setContentView(R.layout.title_demo); super.findViewById(); // setTitle("返回主页", "这是一个Title", "下一个界面"); TitleRes left = new TitleRes("left", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(TitleDemoActivity.this, "click left", Toast.LENGTH_SHORT).show(); //showToast("click left"); } }); TitleRes middle = new TitleRes("middle", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(TitleDemoActivity.this, "click middle", Toast.LENGTH_SHORT).show(); //showToast("click middle"); } }); TitleRes right = new TitleRes("right", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(TitleDemoActivity.this, "click right", Toast.LENGTH_SHORT).show(); //showToast("click right"); } }); // setTitle(left, middle, right); } ~~~ 这个时候title三个控件都具备了交互的能力   Toast.makeText(TitleDemoActivity.this,"click right",Toast.* LENGTH_SHORT * ).show(); 程序中会频繁的用到 context  Toast等对象 采用Class.this的方式在代码移植、维护、重构等方面很不友好 另外频繁调用Toast每次都那么写的话 麻烦死 所以BaseActivity中新增俩方法 作用于暂且定义为protected  以后估计还得改 ~~~ protected Context getContext() {     returnthis; } ~~~ ~~~ protectedvoid showToast(String message) {     Toast.*makeText*(getContext(), message,Toast.*LENGTH_SHORT*).show(); } ~~~ 这个时候子类调用父类的代码如下 ~~~ TitleRes left =newTitleRes("left",newView.OnClickListener() {     @Override     publicvoid onClick(View v) {         showToast("click left");//     } }); 。。。。中、右略 // setTitle(left,middle, right); ~~~
';