2012-06-22 56 views
6

我正在使用Eclipse進行Android開發,並且我已經設置了自己的代碼格式化風格,但仍然有匿名方法,我無法弄清楚如何在Eclipse中進行格式化。這就是Eclipse如何格式化匿名方法:如何在Eclipse中爲Java設置代碼格式化器匿名方法

// The BroadcastReceiver that listens for discovered devices and 
    // changes the title when discovery is finished 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
                @Override 
                public void onReceive(Context context, Intent intent) { 
                 String action = intent.getAction(); 
                 Utils.Log.i("BLUETOOTH: " + action); 
                 if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
                  // Get the 
                  // BluetoothDevice 
                  // object from the 
                  // Intent 
                  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
                  // If it's already 
                  // paired, skip it, 
                  // because it's been 
                  // listed already 
                  if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
                   if (mNewDevicesArrayAdapter.getCount() == 0) { 
                    mNewDevicesArrayAdapter.add(device); 
                   } 
                   btDevicesUpdateList.add(device); 
                  } 
                 } 
                 else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
                  mNewDevicesArrayAdapter.setItems(btDevicesUpdateList); 
                  mNewDevicesArrayAdapter.notifyDataSetChanged(); 
                  btDevicesUpdateList.clear(); 
                  mBtAdapter.startDiscovery(); 
                 } 
                 else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { 
                  if (mBtAdapter.getState() == BluetoothAdapter.STATE_ON) { 
                   switchToView(viewBluetoothOn); 
                   firstTimeDiscover(); 
                  } 
                  else if (mBtAdapter.getState() == BluetoothAdapter.STATE_OFF) { 
                   switchToView(viewBluetoothOff); 
                  } 
                 } 
                } 
               }; 

請參閱?它非常糟糕。什麼是正確的設置來格式化匿名方法聲明留在左側,不要在=平等的性格?

+0

請發表你的內容的.settings/org.eclipse.jdt.core.prefs –

+3

我花了好5分鐘清理那個可怕的格式,才意識到問題是* *左右可怕的說格式化。 **** headdesk **** –

+0

@EugeneKuleshov這是項目的具體設置。 – papaiatis

回答

0

看起來好像你有某種自定義的格式化設置。轉至項目屬性/ Java代碼樣式/格式器/啓用項目特定設置,然後選擇「Java慣例」內置格式程序配置文件。

+0

是的,我確實有。正如我在我的問題中已經提到的,我已經設置了一些自定義格式設置,但我不知道如何處理Anonymus方法的格式... – papaiatis

3

我相信導致這種不良格式的設置是「在列中對齊字段」,如果關閉此功能,則類/接口實現應該從行的開頭縮進而不是等號。

我在eclipse上打開了一個bug來修復默認行爲或爲類/接口實現添加另一個設置。

https://bugs.eclipse.org/bugs/show_bug.cgi?id=385901

2

工作,一個全面的這個問題稍微改變您的編碼風格。 剪下並粘貼以下內容並運行您的格式器。風格#2將看起來不那麼蹩腳的 。

// **Style #1** - Formatter handles poorly 
JDialog jDialog1 = new JDialog(new JFrame()) { 
    public void setBackground(final Color c) { 
     super.setBackground(c); 
    } 
}; 

// **Style #2** - Formatter handles well. 
JDialog jDialog2; 
{ 
    jDialog2 = new JDialog(new JFrame()) { 
     public void setBackground(final Color c) { 
      super.setBackground(c); 
     } 
    }; 
} 
+1

Works! Eclipse應該按照@RedLenses的建議修復格式化程序,但現在這是一個很好的解決方法。 –