在我的应用程序中,用户可以选择文本并使用图像上显示的选项设置文本的样式。在查看代码时,我想知道我所有switch语句的case语句是否应该,break
或者return true/false
是否有任何影响?我可以看到,通过使用Log.d();
该方法break
可以切换开关,而在开关中使用return
停留
因此,在这种情况下,发生的情况是否重要?
Switch情况下的方法:
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.textcolor:
f3 = ColorPickerDialogFrag2.newInstance(3, Color.WHITE);
f3.setStyle(android.support.v4.app.DialogFragment.STYLE_NORMAL, R.style.AppTheme);
f3.show(fragmentManager, "d");
f3.setListener(this);
break;
//--------------------BOLD----------------------------
case R.id.bold:
styleSpans = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);
for (int i = 0; i < styleSpans.length; i++) {
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
str.removeSpan(styleSpans[i]);
exists = true;
}
}
if (!exists) {
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
editText.setSelection(selectionStart, selectionEnd);
return true;
//--------------------ITALIC----------------------------
case R.id.italic:
styleSpans = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);
for (int i = 0; i < styleSpans.length; i++) {
if (styleSpans[i].getStyle() == android.graphics.Typeface.ITALIC) {
str.removeSpan(styleSpans[i]);
exists = true;
}
}
if (!exists) {
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), selectionStart, selectionEnd,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
editText.setSelection(selectionStart, selectionEnd);
Log.d(LOG_TAG, "italic");
break;
//--------------------UNDERLINE----------------------------
case R.id.underline:
UnderlineSpan[] underSpan = str.getSpans(selectionStart, selectionEnd, UnderlineSpan.class);
for (int i = 0; i < underSpan.length; i++) {
str.removeSpan(underSpan[i]);
exists = true;
}
if (!exists) {
str.setSpan(new UnderlineSpan(), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
editText.setSelection(selectionStart, selectionEnd);
Log.d(LOG_TAG, "underline");
return true;
//--------------------STROKE----------------------------
case R.id.stroke:
Log.d(LOG_TAG, "stroke");
android.text.style.StrikethroughSpan[] strokeSpan = str.getSpans(selectionStart, selectionEnd, android.text.style.StrikethroughSpan.class);
for (int i = 0; i < strokeSpan.length; i++) {
str.removeSpan(strokeSpan[i]);
exists = true;
}
if (!exists) {
str.setSpan(new android.text.style.StrikethroughSpan(), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
editText.setSelection(selectionStart, selectionEnd);
return false;
case R.id.increase:
Log.d(LOG_TAG, "increase");
str.setSpan(new RelativeSizeSpan(1.1f), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
editText.setSelection(selectionStart, selectionEnd);
break;
case R.id.decrease:
str.setSpan(new RelativeSizeSpan(0.9f), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
editText.setSelection(selectionStart, selectionEnd);
break;
case android.R.id.cut:
CharSequence charSequence = editText.getText().subSequence(selectionStart, selectionEnd);
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text", charSequence);
clipboard.setPrimaryClip(clip);
editText.getText().replace(selectionStart, selectionEnd, "");
Toast.makeText(context, R.string.toastCopy, Toast.LENGTH_SHORT).show();
break;
case android.R.id.copy:
charSequence = editText.getText().subSequence(selectionStart, selectionEnd);
clipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
clip = ClipData.newPlainText("simple text", charSequence);
clipboard.setPrimaryClip(clip);
Toast.makeText(context, R.string.toastCopy, Toast.LENGTH_SHORT).show();
break;
case R.id.textfont:
FontFragment fontFragment = new FontFragment(selectionStart, selectionEnd, editText);
fontFragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
fontFragment.show(fragmentManager, "fontfragment");
break;
}
}
Log.d(LOG_TAG, "out of switch");
return true;
}
使用return停留在开关中
这是不正确的。一return
言最肯定不会不留在switch
声明。Areturn
将立即从方法中返回。该方法中的其余代码将不会执行。因此,使用时,您看不到switch语句之后的log语句return
。
abreak
或areturn
将退出switch语句。任一个就足够了。关于在方法中使用多个return
语句的争论很多。
某些人希望return
方法末尾只有一个语句。这种做法的好处是毫不奇怪:在您维护软件并需要进行更改时,可以在切换后添加语句,这些语句将被执行。您不需要找到每个return
调整每个。
但是,有时可以很好地使用多个return
语句。例如,如果您return
立即测试错误情况,这可以使代码更短并避免不必要的深度缩进。有时,它需要多余的标志和复杂的逻辑,以避免额外的return语句。
最终,请考虑在未来数月和数年内阅读和理解代码的清晰性和便捷性。那就是区别很重要的时候。以最简单,最清晰的方式编写代码。我建议分开您的switch语句,并为每个块使用单独的方法。然后,该声明将变得更短,更清晰。您将能够在屏幕上看到每种情况,而不会滚动并迷失在噪声中,并且能够看到每种情况breaks
。或使用if-else语句,这样当您失败时不会感到惊讶,case
因为您忘记了break
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句