2015-06-05 26 views
3

我想以編程方式啓用和禁用4個UI按鈕。我正在使用Unity3D,但似乎無法使其工作。我錯過了什麼?我現在的嘗試是這樣的:如何啓用和禁用Android中的按鈕?

LinearLayout XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/overlay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="right" 
    android:orientation="vertical" > 

    <com.BoostAR.Generic.TintedImageButton 
     android:id="@+id/helpButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/overlayButtonMargin" 
     android:src="@drawable/help" 
     android:visibility="visible" /> 

    <com.BoostAR.Generic.TintedImageButton 
     android:id="@+id/refreshButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/overlayButtonMargin" 
     android:src="@drawable/refresh" /> 

    <com.BoostAR.Generic.TintedImageButton 
     android:id="@+id/screenshotButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/overlayButtonMargin" 
     android:src="@drawable/photo" /> 

    <com.BoostAR.Generic.LockButton 
     android:id="@+id/lockButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/overlayButtonMargin" 
     android:src="@drawable/unlocked" />  
</LinearLayout> 

我的代碼做了什麼:

private static final int[] AUGMENTED_UI_IDS = { 
     R.id.refreshButton, R.id.screenshotButton, R.id.lockButton 
}; 


private void updateAugmentedUiVisibility() 
{ 
    final int visibility = 
       (mShouldShowAugmentedUI ? View.VISIBLE : View.INVISIBLE); 

    runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       for (int id : AUGMENTED_UI_IDS) { 
        final View view = findViewById(id); 
        if (view == null) { 
         Log.e(LOG_TAG, "Failed to find view with ID: " + id); 
        } else { 
         Log.e(LOG_TAG, "Visibility: " + visibility); 
         view.setVisibility(visibility); 
        } 
       } 
      } 
     }); 
    } 
} 

觀察:

聲明

Log.e(LOG_TAG, "Failed to find view with ID: " + id); 

被調用。當我確實交叉引用了似乎很好的ID號碼。

+0

你應該在這裏粘貼相關的代碼。 「打開和關閉這4個UI按鈕」是什麼意思?你想禁用/啓用它們? – codeMagic

+0

我確實增加了這個問題。謝謝,是的,我想禁用並啓用它們。這就是我想要做的。 – karsnen

+0

編輯嘗試和改進您的格式和您的語法。我還刪除了無關的介紹材料和*謝謝*。另外,我從pastebin導入了xml(請不要將您的部分問題鏈接到第三方網站)。 –

回答

2

可能會增加一些訂單的事情,當你設置通過代碼屬性的簡單說明,這是很好的記住這些:

view.setVisibility(View.INVISIBLE); // the opposite is obvious 

會讓視圖不可見,但仍會佔用的空間(您剛纔不會看到它)

view.setVisibility(View.GONE); 

會崩潰的觀點,使其既看不見,將重新排列的方式,它周圍的景色,這將佔用的空間,就好像它從來沒有在那裏。

view.setEnabled(false); // the opposite is again obvious 

將無響應,但在視覺上理解的方式,例如,假設你使用一個開關,切換後,你希望它成爲unchangable,那麼這將是一個例子的觀點:

Switch MySwitch = (Switch) someParentView.findViewById(R.id.my_switch); 

    MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 
      MySwitch.setEnabled(false); 
      } 
     } 
    } 

這種方式與佈局以及(在某種程度上)也是相關的。

希望這有助於。