繁体   English   中英

Android开发-创建带有3个imageButtons的菜单

[英]Android Development - Create a menu with 3 imageButtons

首先,英语不是我的母语,但我会尽力而为。

另外...我很确定我的头衔选择不是最好的,对此感到抱歉。

基本上我想做的是一个带有三个ImageButtons的菜单,但是有一个棘手的部分(至少对我来说很麻烦),因为每次我按下一个按钮时,相同的按钮都会更改图像(变为彩色版本而不是变灰的图像),另外两个也会从各自图像的彩色版本变为灰色,实际上,其他两个只有一个会更改,因为这样做的目的是一次只能激活一个,所以不可能同时激活其他两个。

请注意,这不是右上角的菜单,而只是活动或片段上的三个ImageButton的集合。

我已经尝试了很多方法来实现这一目标,但到目前为止还算不上什么运气,但我想我知道为什么虽然我实际上不是android dev的新手,但是却找不到解决方法。

我尝试过的是在任何这些按钮的setOnClickListener内部,例如:

eventsButton.setOnClickListener(
   new View.OnClickListener() {
      public void onClick(View view) {
         ImageButton eventsButton = (ImageButton) view.findViewById(R.id.eventsButton);
         eventsButton.setBackgroundResource(R.drawable.events_icon_active);
         eventsButton.setClickable(false);
      }
   }
);

我试图添加功能来更改其他imageButtons,例如:

eventsButton.setOnClickListener(
   new View.OnClickListener() {
      public void onClick(View view) {
         ImageButton eventsButton = (ImageButton) view.findViewById(R.id.eventsButton);
         eventsButton.setBackgroundResource(R.drawable.events_icon_inactive);
         eventsButton.setClickable(false);

         ImageButton contactsButton = (ImageButton) view.findViewById(R.id.contactsButton);
         contactsButton.setBackgroundResource(R.drawable.contacts_icon_inactive);
         contactsButton.setClickable(true);

         ImageButton interestsButton = (ImageButton) view.findViewById(R.id.interestsButton);
         interestsButton.setBackgroundResource(R.drawable.interests_icon_inactive);
         interestsButton.setClickable(true);
      }
   }
);

然后我重复了三遍,总是将其他按钮设置为可点击,并将其图像设置为非活动状态(变灰的一个),同时将我单击的按钮设置为不再可单击。

但是从我收集到的信息来看,我无法对eventsButton.setOnClickListener内的任何其他按钮(如按钮interestesButton或contactsButton)进行任何引用,只要我触摸以下三个错误消息中的任何三个按钮,就会使应用程序崩溃:

尝试在空对象引用上调用虚拟方法'void android.widget.ImageButton.setBackgroundResource(int)'

而且它始终指向第一行,在该行中我引用另一个按钮而不是用于启动setOnClickListener的按钮。

如果您能指出正确的方向,我将不胜感激。

祝一切顺利

eventsButton.setOnClickListener(
   new View.OnClickListener() {
      public void onClick(View view) {

         ImageButton contactsButton = (ImageButton) view.findViewById(R.id.contactsButton);
         contactsButton.setBackgroundResource(R.drawable.contacts_icon_inactive);
         contactsButton.setClickable(true);
      }
   }
);

您的问题出在view.findViewById(R.id.contactsButton)view这里是被单击的按钮(一个事件),通过调用view.findViewById(contactsButton)您隐式地说联系人按钮是view 的子级,事实并非如此。

只需使用findViewById() (来自Activity), getActivity().findViewById() (来自Fragments)或更好的container.findViewById() (如果您对包含三个按钮的布局进行引用)。

我并不是说您的菜单是处理菜单的最有效方法,只是指出您的错误。

您可以将ImageViews声明为在侦听器范围之外的final,并在调用onClickListener(View v)可以调用setBackground,因为它们是最终的,并且可以从侦听器内部引用它们。

像这样:

final ImageView view1 = (ImageView) findViewById(R.id.view1id);
final ImageView view2 = (ImageView) findViewById(R.id.view2id);

view1.setOnClickListener(
   new View.OnClickListener() {
      public void onClick(View view) {
          // do whatever you want to the ImageViews
          // view1.setBackground...
      }
   }
);

您可以首先使事情变得简单; 我建议:

  • 您在活动类中添加了3个arrayArraylist可能更好),一个用于按钮,一个用于活动资源,一个用于非活动资源。
  • onCreate方法中初始化这些数组;
  • 定义一个onClickListener对象,并将其用于所有按钮; onClick方法中使用循环,请参见下面的内容。

在代码方面,它看起来像这样:

ImageButton[] buttons;
int[] activeResources;
int[] inactiveResources;

protected void onCreate2(Bundle savedInstanceState) {

    View.OnClickListener onClickListener = new View.OnClickListener(){
        public void onClick(View view) {
            ImageButton clickedButton = (ImageButton) view;
            for(int i = 0; i<buttons.length; i++){
                ImageButton bt = buttons[i];
                if(clickedButton==bt){
                    bt.setBackgroundResource(inactiveResources[i]);
                    bt.setClickable(false);
                }else{
                    bt.setBackgroundResource(activeResources[i]);
                    bt.setClickable(true);
                }
            }
        }
    };

    buttons = new ImageButton[3];
    activeResources = new int[3];
    inactiveResources = new int[3];

    int idx = 0;
    buttons[idx] = (ImageButton) findViewById(R.id.eventsButton);
    inactiveResources[idx] = R.drawable.events_icon_inactive;
    activeResources[idx] = R.drawable.events_icon_active;

    idx = 1;
    buttons[idx] = (ImageButton) findViewById(R.id.contactsButton);
    inactiveResources[idx] = R.drawable.contacts_icon_inactive;
    activeResources[idx] = R.drawable.contacts_icon_active;

    idx = 3;
    buttons[idx] = (ImageButton) findViewById(R.id.interestsButton);
    inactiveResources[idx] = R.drawable.interests_icon_inactive;
    activeResources[idx] = R.drawable.interests_icon_active;

    for(int i =0; i<buttons.length; i++){
        buttons[i].setBackgroundResource(activeResources[i]);
        buttons[i].setOnClickListener(onClickListener);
    }
}

不要指望它能正确运行,我只给出想法,您必须查看并确定它是否适合您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM