我似乎不明白为什么此活动中的后退按钮不起作用。请问该如何解决此问题?我尝试了其他一些解决方案,但大多数似乎不起作用?我希望用户返回按下按钮后,进入MainActivity。
我已经添加了家长活动的名称。你们有什么建议吗?
public class WaterSelectionActivity extends AppCompatActivity {
//Firebase Auth for Userlocation
private FirebaseAuth mAuth;
String currentUser;
DatabaseReference mgetUsername;
final ArrayList<String> selection = new ArrayList<>();
final ArrayList<String> pickedNumber = new ArrayList<>();
Switch simpleSwitch;
ActionBar actionBar;
ScrollableNumberPicker numberPicker1;
CheckBox checkbox1;
CheckBox checkbox2;
ImageView mimgop;
ImageView mimageViewop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water_selection);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser().getUid();
mgetUsername = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUser);
simpleSwitch = (Switch) findViewById(R.id.switch2);
mimageViewop = (ImageView) findViewById(R.id.imageViewop);
mimageViewop.animate().alpha(1f).setDuration(2000);
mimgop = (ImageView) findViewById(R.id.imgop);
mimageViewop.animate().alpha(1f).setDuration(3000);
checkbox1 = (CheckBox) findViewById(R.id.checkBox);
checkbox2 = (CheckBox) findViewById(R.id.secondCheckbox);
numberPicker1 = (ScrollableNumberPicker) findViewById(R.id.number_picker_horizontal);
numberPicker1.setMaxValue(10);
numberPicker1.setMinValue(3);
numberPicker1.setListener(new ScrollableNumberPickerListener() {
@Override
public void onNumberPicked(int value) {
// Do some magic
pickedNumber.add(String.valueOf(value));
}
});
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF8C00")));
actionBar.setTitle("Water Specifics");
//set the current state of a Switch
simpleSwitch.setBackgroundColor(Color.parseColor("#FF8C00"));
simpleSwitch.setChecked(false);
}
public void selectItem(View view) {
boolean checked = ((CheckBox) view).isChecked();
if (view.getId() == R.id.checkBox) {
if (checked) {
selection.add("15ltr");
checkbox2.setVisibility(View.INVISIBLE);
} else {
selection.remove("15ltr");
checkbox2.setVisibility(View.VISIBLE);
}
} else {
if (checked) {
selection.add("2000ltr");
checkbox1.setVisibility(View.INVISIBLE);
} else {
selection.remove("2000ltr");
checkbox1.setVisibility(View.VISIBLE);
}
}
}
public void submitWater(View view) {
//Getting the switch value
String statusSwitch1 = "";
if (simpleSwitch.isChecked()) {
statusSwitch1 = statusSwitch1 + "Fresh Water";
} else {
statusSwitch1 = statusSwitch1 + "Regular Salty Water";
}
Toast.makeText(this, statusSwitch1, Toast.LENGTH_SHORT).show();
String finalSelection = "";
for (String Selections : selection) {
finalSelection = finalSelection + selection + "\n";
}
Toast.makeText(this, finalSelection, Toast.LENGTH_SHORT).show();
final String finalStatusSwitch = statusSwitch1;
final String finalSelection1 = finalSelection;
numberPicker1.setListener(new ScrollableNumberPickerListener() {
@Override
public void onNumberPicked(int value) {
// Do some magic
pickedNumber.add(String.valueOf(value));
}
});
Toast.makeText(WaterSelectionActivity.this, pickedNumber.toString(), Toast.LENGTH_SHORT).show();
final String finalStatusSwitch1 = statusSwitch1;
final String finalSelection2 = finalSelection;
mgetUsername.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Values
String name = dataSnapshot.child("name").getValue().toString();
String waterType = finalStatusSwitch1;
String container = finalSelection2;
String quantity = pickedNumber.toString();
//Put these values in an intent and send them to the next page
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return onKey(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return onKey(keyCode, event);
}
private boolean onKey(int keyCode, KeyEvent event) {
if (numberPicker1.isFocused()) {
return numberPicker1.handleKeyEvent(keyCode, event);
}
return false;
}
}