注释:此案例主要展示自定义底部菜单,一处封装处处调用。使用起来相当方便
一、初始的Activity
package com.example.myapi.buttommenu;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;import com.example.myapi.R;import com.example.myapi.buttommenu.MenuUtils.MeunOnClickListener;public class Menu1Activity extends Activity implements MeunOnClickListener,OnClickListener{ private MenuUtils menuUtils; private Button btn_two; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu1); menuUtils = (MenuUtils)findViewById(R.id.menu_utils); menuUtils.setMenuListener(this); btn_two = (Button)findViewById(R.id.btn_two); btn_two.setOnClickListener(this); } @Override public void menuOne() { Toast.makeText(this, "您点击了菜单一", Toast.LENGTH_LONG).show(); } @Override public void menuTwo() { Toast.makeText(this, "您点击了菜单二", Toast.LENGTH_LONG).show(); } @Override public void menuThree() { Toast.makeText(this, "您点击了菜单三", Toast.LENGTH_LONG).show(); } @Override public void menuFour() { Toast.makeText(this, "您点击了菜单四", Toast.LENGTH_LONG).show(); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_two: Intent intent = new Intent(); intent.setClass(this, Menu2Activity.class); startActivity(intent); break; } }}
第二个Activity
package com.example.myapi.buttommenu;import com.example.myapi.R;import com.example.myapi.buttommenu.MenuUtils.MeunOnClickListener;import android.app.Activity;import android.os.Bundle;import android.widget.Toast;public class Menu2Activity extends Activity implements MeunOnClickListener{ private MenuUtils menuUtils; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu2); menuUtils = (MenuUtils)findViewById(R.id.menu_utils); menuUtils.setMenuListener(this); } @Override public void menuOne() { Toast.makeText(this, "您点击了菜单一", Toast.LENGTH_LONG).show(); } @Override public void menuTwo() { Toast.makeText(this, "您点击了菜单二", Toast.LENGTH_LONG).show(); } @Override public void menuThree() { Toast.makeText(this, "您点击了菜单三", Toast.LENGTH_LONG).show(); } @Override public void menuFour() { Toast.makeText(this, "您点击了菜单四", Toast.LENGTH_LONG).show(); }}
package com.example.myapi.buttommenu;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;import com.example.myapi.R;/** * 菜单工具类 * @author * * */public class MenuUtils extends LinearLayout implements OnClickListener{ private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Context context; private MeunOnClickListener listener; public MenuUtils(Context context) { super(context); this.context = context; initView(context); } public MenuUtils(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } private void initView(Context context){ LinearLayout menuView = (LinearLayout)LayoutInflater.from(context).inflate(com.example.myapi.R.layout.menutuils, null); addView(menuView); btn_1 = (Button)menuView.findViewById(R.id.btn_1); btn_2 = (Button)menuView.findViewById(R.id.btn_2); btn_3 = (Button)menuView.findViewById(R.id.btn_3); btn_4 = (Button)menuView.findViewById(R.id.btn_4); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); } public void setMenuListener(MeunOnClickListener listener){ this.listener = listener; } public interface MeunOnClickListener{ public void menuOne(); public void menuTwo(); public void menuThree(); public void menuFour(); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_1: listener.menuOne(); break; case R.id.btn_2: listener.menuTwo(); break; case R.id.btn_3: listener.menuThree(); break; case R.id.btn_4: listener.menuFour(); break; } } }
一下是布局文件
完毕