包括使用Java的Andr​​oid [英] Include in android using java

查看:111
本文介绍了包括使用Java的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4个按钮的标题视图。当我点击每种4个按键就应该开始四个不同的活动。同时,我想在我的应用程序的每个视图这些按钮。

I have a header view which contains 4 buttons. When I click each of these 4 buttons it should start four different activities. And also I want these buttons in every view of my application.

我可以包括使用包括标记该头视图。但是,我怎么能包含Java code(按钮点击等),在每一个活动?

I can include this header view using include tag. But how can I include the java code (button click etc) in every activities?

感谢您

推荐答案

请一个单独的类,将得到的按钮作为参数,将创建并设置相应的onClick监听器。然后,只需调用,从那里你需要的类。像这样的:

Make a separate class which will get the buttons as parameters and which will create and set the appropriate onClick listeners. Then just call the class from where you need. Like this:

public class ButtonInitializer {

  private Button btn1, btn2, btn3, btn4;

  public ButtonInitializer(Button btn1, Button btn2/* and another 2 here*/) {
     this.btn1 = btn1;
     this.btn2 = btn2;
     this.btn3 = btn3;
     this.btn4 = btn4;
  }

  public void init() {
     btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick() {
           // your code
        }
     }
     // and for other buttons
   }

在您的活动:

new ButtonInitializer(btn1, btn2, btn3, btn4).init();

这篇关于包括使用Java的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆