簡體   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