简体   繁体   English

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

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

First of all english is not my first language but i will try my best. 首先,英语不是我的母语,但我会尽力而为。

Also... i am pretty sure my title choice was not the best so sorry for that. 另外...我很确定我的头衔选择不是最好的,对此感到抱歉。

Basically what i wanted to do is a menu with three ImageButtons but there is a tricky part (tricky for me at least) since every time i press one button that same button changes image (to a colored version instead of a grayed out image) and the other two change as well from colored version of their respective images to grayed out ones, actually only one of the other two will change since the purpose of this is to be able to activate only one at a time so it would not be possible to have the other two active at the same time. 基本上我想做的是一个带有三个ImageButtons的菜单,但是有一个棘手的部分(至少对我来说很麻烦),因为每次我按下一个按钮时,相同的按钮都会更改图像(变为彩色版本而不是变灰的图像),另外两个也会从各自图像的彩色版本变为灰色,实际上,其他两个只有一个会更改,因为这样做的目的是一次只能激活一个,所以不可能同时激活其他两个。

Notice that this is not a menu on the top right corner but just a set of three ImageButtons on a activity or Fragment. 请注意,这不是右上角的菜单,而只是活动或片段上的三个ImageButton的集合。

I already tried a lot of stuff to make that happen but so far no luck but i think i know why though i can't find a workaround for this since i am actually new in android dev. 我已经尝试了很多方法来实现这一目标,但到目前为止还算不上什么运气,但我想我知道为什么虽然我实际上不是android dev的新手,但是却找不到解决方法。

what i tried was inside the setOnClickListener of any of those buttons such as: 我尝试过的是在任何这些按钮的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);
      }
   }
);

i tried to add the functions to change the other imageButtons as well like: 我试图添加功能来更改其他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);
      }
   }
);

and i repeated that three time, always setting the other buttons clickable and setting their images to the inactive one (the grayed out one), also setting the button i click as no longer clickable. 然后我重复了三遍,总是将其他按钮设置为可点击,并将其图像设置为非活动状态(变灰的一个),同时将我单击的按钮设置为不再可单击。

But from what i gather i cant do any references to any other buttons inside the eventsButton.setOnClickListener like the buttons interestsButton or contactsButton, it will crash the app as soon as i touch any of those three buttons with the following error message: 但是从我收集到的信息来看,我无法对eventsButton.setOnClickListener内的任何其他按钮(如按钮interestesButton或contactsButton)进行任何引用,只要我触摸以下三个错误消息中的任何三个按钮,就会使应用程序崩溃:

Attempt to invoke virtual method 'void android.widget.ImageButton.setBackgroundResource(int)' on a null object reference 尝试在空对象引用上调用虚拟方法'void android.widget.ImageButton.setBackgroundResource(int)'

And it always point to the first line where i make a reference to another button other then the one used to start the setOnClickListener. 而且它始终指向第一行,在该行中我引用另一个按钮而不是用于启动setOnClickListener的按钮。

If you can just point me in the right direction i would be tremendously grateful. 如果您能指出正确的方向,我将不胜感激。

All the best 祝一切顺利

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);
      }
   }
);

Your problem is in view.findViewById(R.id.contactsButton) : view here is the button being clicked (the events one), and by calling view.findViewById(contactsButton) you are implicitly saying that the contact button is a child of view , which is not. 您的问题出在view.findViewById(R.id.contactsButton)view这里是被单击的按钮(一个事件),通过调用view.findViewById(contactsButton)您隐式地说联系人按钮是view 的子级,事实并非如此。

Just use findViewById() (from Activity), getActivity().findViewById() (from Fragments), or better container.findViewById() (if you have a reference to the layout containing the three buttons). 只需使用findViewById() (来自Activity), getActivity().findViewById() (来自Fragments)或更好的container.findViewById() (如果您对包含三个按钮的布局进行引用)。

I'm not saying that yours is the most efficient way to deal with a menu, just pointing out your error. 我并不是说您的菜单是处理菜单的最有效方法,只是指出您的错误。

You can declare your ImageViews as final outside the scope of the listener and when the onClickListener(View v) is called you can then just call setBackground because they are final and you can reference them from inside the listener. 您可以将ImageViews声明为在侦听器范围之外的final,并在调用onClickListener(View v)可以调用setBackground,因为它们是最终的,并且可以从侦听器内部引用它们。

Something like this: 像这样:

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...
      }
   }
);

You can first make things simple; 您可以首先使事情变得简单; I suggest: 我建议:

  • you add 3 array ( Arraylist might be better) fields in your activity class, one for the buttons, one for the active resources and one for the inactive resources 您在活动类中添加了3个arrayArraylist可能更好),一个用于按钮,一个用于活动资源,一个用于非活动资源。
  • initialize those arrays in the onCreate method; onCreate方法中初始化这些数组;
  • define a single onClickListener object and use it for all the buttons; 定义一个onClickListener对象,并将其用于所有按钮; Use a loop in the onClick method, see bellow. onClick方法中使用循环,请参见下面的内容。

In terms of code, it looks like this: 在代码方面,它看起来像这样:

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);
    }
}

Do not expect it to run right the way, I am giving only ideas, you have to look and see if it fit for you are looking for. 不要指望它能正确运行,我只给出想法,您必须查看并确定它是否适合您的需求。

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

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