Java Switch语句只返回最后一个case
开关固定装置: switch语句仅返回最后一种情况,即情况4“#0r0df0ff”。如何解决此问题,使文本视图显示对话框中单击的文本? 我是一个完全的新手,所以我非常感谢你的帮助Java Switch语句只返回最后一个case,java,android,textview,switch-statement,Java,Android,Textview,Switch Statement,开关固定装置: switch语句仅返回最后一种情况,即情况4“#0r0df0ff”。如何解决此问题,使文本视图显示对话框中单击的文本? 我是一个完全的新手,所以我非常感谢你的帮助 public class NoteEdit extends Activity { public EditText mTitleText; public EditText mBodyText; public EditText mColor; private NotesDbAdapter mDbHelper; privat
public class NoteEdit extends Activity {
public EditText mTitleText;
public EditText mBodyText;
public EditText mColor;
private NotesDbAdapter mDbHelper;
private static final int DIALOG_ALERT = 10;
Long mRowId;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.note_edit);
setTitle(R.string.done);
mTitleText = (EditText) findViewById(R.id.editTitle);
mBodyText = (EditText) findViewById(R.id.editNote);
mColor = (EditText) findViewById(R.id.editColor);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
}
populateFields();
setupActionBar();
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
setResult(RESULT_OK);
finish();
}
return super.onOptionsItemSelected(item);
}
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
mBodyText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
mColor.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_COLOR)));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateFields();
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
String color = mColor.getText().toString();
if (mRowId == null) {
long id = mDbHelper.createNote(title, body, color);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title, body, color);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.add:
showDialog(DIALOG_ALERT);
return true;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
// Create out AlterDialog
android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] colors = {"Blue", "Green", "Yellow", "Red", "Purple"};
builder.setTitle(R.string.body);
builder.setItems(colors, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which){
case 0:
mColor.setText("#000000");
case 1:
mColor.setText("#0000FF");
case 2:
mColor.setText("#0R00FF");
case 3:
mColor.setText("#0R00dsdFF");
case 4:
mColor.setText("#0R0dfdf0FF");
default:
break;
}
} });
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
}您缺少
中断代码>在交换机分支的末尾。您缺少中断代码>位于交换机分支的末尾。失败。
您必须添加中断
case 0:
mColor.setText("#000000");
break;
break语句是必需的,因为如果没有它们,开关块中的语句就会失效:匹配的case标签之后的所有语句都会按顺序执行,而不管后续case标签的表达式如何,直到遇到break语句为止
失败。
您必须添加中断
case 0:
mColor.setText("#000000");
break;
break语句是必需的,因为如果没有它们,开关块中的语句就会失效:匹配的case标签之后的所有语句都会按顺序执行,而不管后续case标签的表达式如何,直到遇到break语句为止
当您没有返回时,您需要中断
,否则会导致故障当您没有返回时,您需要中断
,否则会导致故障您需要中断;除了最后一个之外,所有的案例都会一一解决
switch (which){
case 0:
mColor.setText("#000000");
break;
case 1:
mColor.setText("#0000FF");
break;
case 2:
mColor.setText("#0R00FF");
break;
case 3:
mColor.setText("#0R00dsdFF");
break;
case 4:
mColor.setText("#0R0dfdf0FF");
default:
break;
}
你需要休息;除了最后一个之外,所有的案例都会一一解决
switch (which){
case 0:
mColor.setText("#000000");
break;
case 1:
mColor.setText("#0000FF");
break;
case 2:
mColor.setText("#0R00FF");
break;
case 3:
mColor.setText("#0R00dsdFF");
break;
case 4:
mColor.setText("#0R0dfdf0FF");
default:
break;
}
我甚至没有看到密码就猜到了答案。您在每个案例之后都缺少break
。是的,但我会得到回答该问题的代表分数:P(当然,它肯定会被删除..@MightyPork。不,不会被删除。这是一个很合理的问题。你发布了太多的代码。我甚至没有看到代码就猜到了答案。您在每个案例之后都缺少break
。是的,但我会得到回答该问题的代表分数:P(当然,它肯定会被删除..@MightyPork。不,不会被删除。这是一个很合理的问题。你发布的代码太多了