[英]How to set Code Formatter for Java Anonymous Methods in Eclipse
我正在使用Eclipse进行Android开发,我已经设置了我的代码格式化样式,但仍然有匿名方法,我无法弄清楚如何在Eclipse中进行格式化。 这就是Eclipse现在如何格式化匿名方法:
// 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);
}
}
}
};
看到? 它非常糟糕。 格式化匿名方法声明以保持在左侧并且不在=
相等字符下的正确设置是什么?
我认为导致这种错误格式化的设置是“对齐列中的字段”,如果关闭它,则类/接口实现应该从行的开头而不是等于缩进。
我在eclipse上打开了一个错误,要么修复默认行为,要么为类/接口实现添加另一个设置。
这个问题的工作是略微改变你的编码风格。 剪切并粘贴以下内容并运行格式化程序。 风格#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);
}
};
}
看起来你有某种自定义格式化设置。 转到项目属性/ Java代码样式/格式化程序/启用项目特定设置,然后选择“Java约定”内置格式化程序配置文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.