如何在Eclipse中为Java匿名方法设置代码格式化程序
我正在使用Eclipse进行Android开发,我已经设置了我的代码格式样式,但仍然有一些匿名方法,我不知道如何在Eclipse中进行格式设置。Eclipse现在是这样格式化匿名方法的:如何在Eclipse中为Java匿名方法设置代码格式化程序,java,eclipse,coding-style,Java,Eclipse,Coding Style,我正在使用Eclipse进行Android开发,我已经设置了我的代码格式样式,但仍然有一些匿名方法,我不知道如何在Eclipse中进行格式设置。Eclipse现在是这样格式化匿名方法的: // The BroadcastReceiver that listens for discovered devices and // changes the title when discovery is finished private final BroadcastReceiver mRe
// 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);
}
}
}
};
看到了吗?很糟糕。将匿名方法声明格式化为保留在左侧且不在=相等字符下的正确设置是什么?您似乎有一些自定义格式化程序设置。转到“项目属性”/“Java代码样式”/“格式化程序”/“启用项目特定设置”,然后选择“Java约定内置格式化程序配置文件”。我认为导致格式错误的设置是“对齐列中的字段”,如果关闭此选项,类/接口实现应该从行首缩进,而不是从等号缩进 我在eclipse上打开了一个bug来修复默认行为或为类/接口实现添加另一个设置
解决这个问题的一个办法是稍微改变一下您的编码风格。 剪切并粘贴以下内容,然后运行格式化程序。样式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);
}
};
}
是的,我有。正如我在问题中已经提到的,我已经设置了一些自定义格式设置,但我不知道如何处理匿名方法的格式设置…请发布你的.settings/org.eclipse.jdt.core.prefsI的内容。我花了整整五分钟来清理这些糟糕的格式,只有意识到这个问题是关于说可怕的格式****总部****@EugeneKuleshov这是特定于项目的设置。不确定问题出在哪里?您没有特定于项目的格式化程序设置?然后从格式编辑器中导出全局格式。代码格式与项目设置无关。它是一个内置的Eclipse功能。问题是我找不到格式化匿名方法的设置。行!Eclipse应该按照@redlens的建议修复格式化程序,但现在这是一个很好的修复方法。