Android开发:MainMenu编辑按钮无法跳转至MainActivity的问题咨询
问题描述
在MainActivity录入身高、体重、年龄并选择性别后,应用会跳转至MainMenu页面。现在想在MainMenu中添加「编辑」按钮,点击后返回MainActivity修改已录入的信息,但点击按钮后跳转无效果。已创建id为btnEdit的按钮,设置onClick为btnEditClick,并编写了跳转至MainActivity的Intent逻辑,但没有作用。
MainActivity 代码
public class MainActivity extends AppCompatActivity { EditText enterHeight, enterWeight, enterAge; private double height; private double weight; private double age; String selectedRadiobut; Button button; RadioButton radioMale, radioFemale; RadioGroup radioGroup; SharedPreferences sp; private boolean heightIsValid() { if (enterHeight.getText().toString().length() == 0) { enterHeight.setError("Cant be empty"); return false; } double height = Double.valueOf(enterHeight.getText().toString()); if (height <= 0) { enterHeight.setError("Cant be zero"); return false; } if (height <= 40 || height > 250) { enterHeight.setError("Cant be less than 40 cm or more than 250 cm"); return false; } return true; } private boolean weightIsValid() { if (enterWeight.getText().toString().length() == 0) { enterWeight.setError("Cant be empty"); return false; } double weight = Double.valueOf(enterWeight.getText().toString()); if (weight <= 0) { enterWeight.setError("Cant be zero"); return false; } if (weight > 600) { enterWeight.setError("Cant be more than 600 kg"); return false; } return true; } private boolean ageIsValid() { if (enterAge.getText().toString().length() == 0) { enterAge.setError("Cant be empty"); return false; } double age = Double.valueOf(enterAge.getText().toString()); if (age <= 0) { enterAge.setError("Cant be zero"); return false; } if (age > 116) { enterAge.setError("Cant be older than 116 years"); return false; } return true; } private boolean genderIsSelected() { if (radioGroup.getCheckedRadioButtonId() == -1) { //checking if the user has selected a option using id Toast toast = Toast.makeText(this, "Please select gender", Toast.LENGTH_SHORT); toast.show(); return false; } if (radioMale.isChecked()) { selectedRadiobut = radioMale.getText().toString(); } else if (radioFemale.isChecked()) { selectedRadiobut = radioFemale.getText().toString(); } return true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getSupportActionBar().hide(); setContentView(R.layout.activity_main); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); sp = getSharedPreferences("MyUserPrefs", Context.MODE_PRIVATE); if (sp.contains("height") && sp.contains("weight") && sp.contains("age") && sp.contains("gender")) { // Values are stored, navigate to MainMenu directly Intent intent = new Intent(MainActivity.this, MainMenu.class); startActivity(intent); } else { radioMale = (RadioButton) findViewById(R.id.radioMale); radioFemale = (RadioButton) findViewById(R.id.radioFemale); button = (Button) findViewById(R.id.button); enterHeight = (EditText) findViewById(R.id.enterHeight); enterWeight = (EditText) findViewById(R.id.enterWeight); enterAge = (EditText) findViewById(R.id.enterAge); radioGroup = (RadioGroup) findViewById(R.id.radioSex); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (heightIsValid() & weightIsValid() & ageIsValid() & genderIsSelected()) { height = Double.valueOf(enterHeight.getText().toString()); weight = Double.valueOf(enterWeight.getText().toString()); age = Double.valueOf(enterAge.getText().toString()); SharedPreferences.Editor editor = sp.edit(); editor.putString("height", String.valueOf(Integer.valueOf((int) height))); editor.putString("weight", String.valueOf(Integer.valueOf((int) weight))); editor.putString("age", String.valueOf(Integer.valueOf((int) age))); editor.putString("gender", String.valueOf(selectedRadiobut)); editor.commit(); InputMethodManager imm = (InputMethodManager) getSystemService(getApplicationContext().INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); Intent intent = new Intent(MainActivity.this, MainMenu.class); startActivity(intent); } } }); } } @Override protected void onResume() { super.onResume(); if (enterHeight != null) { enterHeight.setText(""); } if (enterWeight != null) { enterWeight.setText(""); } if (enterAge != null) { enterAge.setText(""); } if (radioGroup != null) { radioGroup.clearCheck(); } } }
MainMenu 代码
public class MainMenu extends AppCompatActivity { Button btnMC , btnFN , btnRec , btnGuides, btnEdit; public void btnStepClick(View view) { Intent intent = new Intent(this, StepCounter.class); startActivity(intent); } public void btnCalClick(View view) { Intent intent = new Intent(this, MacrosCalculator.class); startActivity(intent); } public void btnRecClick(View view){ Intent intent = new Intent(this, Recipes.class); startActivity(intent); } public void btnEditClick(View view){ Log.d("MainMenu", "Edit button clicked"); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getSupportActionBar().hide(); setContentView(R.layout.activity_main_menu); TextView age1 , height1 , weight1 , gender1; age1 = (TextView) findViewById(R.id.age1); height1 = (TextView) findViewById(R.id.height1); weight1 = (TextView) findViewById(R.id.weight1); gender1 = (TextView) findViewById(R.id.gender1); btnMC = (Button) findViewById(R.id.btnMC); btnFN = (Button) findViewById(R.id.btnFN); btnRec = (Button) findViewById(R.id.btnRec); btnGuides = (Button) findViewById(R.id.btnGuides); btnEdit = (Button) findViewById(R.id.btnEdit); SharedPreferences sp = getApplicationContext().getSharedPreferences("MyUserPrefs", Context.MODE_PRIVATE); String height = sp.getString("height" , ""); String weight = sp.getString("weight" , ""); String age = sp.getString("age" , ""); String gender= sp.getString("gender", ""); gender1.setText("Gender: " + gender); age1.setText("Age: " + age); height1.setText("Height(cm): " + height); weight1.setText("Weight(kg): " + weight); } }
问题原因与解决方法
问题根源
点击编辑按钮后,虽然Intent触发了跳转,但MainActivity的onCreate方法会检查SharedPreferences中是否存在用户数据,由于之前已经录入过信息,这一条件成立,代码会立即再次跳回MainMenu,导致用户看不到MainActivity的界面。
解决步骤
- 修改MainMenu的跳转逻辑:在跳转到MainActivity时,添加编辑模式标记:
public void btnEditClick(View view){ Log.d("MainMenu", "Edit button clicked"); Intent intent = new Intent(this, MainActivity.class); intent.putExtra("IS_EDIT_MODE", true); // 添加编辑模式标记 startActivity(intent); }
- 修改MainActivity的onCreate逻辑:读取标记,编辑模式下跳过自动跳转,同时填充已有数据:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getSupportActionBar().hide(); setContentView(R.layout.activity_main); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); sp = getSharedPreferences("MyUserPrefs", Context.MODE_PRIVATE); // 读取编辑模式标记 boolean isEditMode = getIntent().getBooleanExtra("IS_EDIT_MODE", false); if (!isEditMode && sp.contains("height") && sp.contains("weight") && sp.contains("age") && sp.contains("gender")) { // 非编辑模式且有数据时,才跳转到MainMenu Intent intent = new Intent(MainActivity.this, MainMenu.class); startActivity(intent); finish(); // 结束当前Activity,避免返回栈问题 } else { // 初始化控件 radioMale = (RadioButton) findViewById(R.id.radioMale); radioFemale = (RadioButton) findViewById(R.id.radioFemale); button = (Button) findViewById(R.id.button); enterHeight = (EditText) findViewById(R.id.enterHeight); enterWeight = (EditText) findViewById(R.id.enterWeight); enterAge = (EditText) findViewById(R.id.enterAge); radioGroup = (RadioGroup) findViewById(R.id.radioSex); // 编辑模式下填充已有数据 if (isEditMode) { String height = sp.getString("height", ""); String weight = sp.getString("weight", ""); String age = sp.getString("age", ""); String gender = sp.getString("gender", ""); enterHeight.setText(height); enterWeight.setText(weight); enterAge.setText(age); if (gender.equals(radioMale.getText().toString())) { radioMale.setChecked(true); } else if (gender.equals(radioFemale.getText().toString())) { radioFemale.setChecked(true); } } button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (heightIsValid() & weightIsValid() & ageIsValid() & genderIsSelected()) { height = Double.valueOf(enterHeight.getText().toString()); weight = Double.valueOf(enterWeight.getText().toString()); age = Double.valueOf(enterAge.getText().toString()); SharedPreferences.Editor editor = sp.edit(); editor.putString("height", String.valueOf(Integer.valueOf((int) height))); editor.putString("weight", String.valueOf(Integer.valueOf((int) weight))); editor.putString("age", String.valueOf(Integer.valueOf((int) age))); editor.putString("gender", String.valueOf(selectedRadiobut)); editor.commit(); InputMethodManager imm = (InputMethodManager) getSystemService(getApplicationContext().INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); Intent intent = new Intent(MainActivity.this, MainMenu.class); startActivity(intent); finish(); // 结束当前Activity } } }); } }
- 修改MainActivity的onResume方法:编辑模式下不清空输入框:
@Override protected void onResume() { super.onResume(); boolean isEditMode = getIntent().getBooleanExtra("IS_EDIT_MODE", false); if (!isEditMode) { if (enterHeight != null) { enterHeight.setText(""); } if (enterWeight != null) { enterWeight.setText(""); } if (enterAge != null) { enterAge.setText(""); } if (radioGroup != null) { radioGroup.clearCheck(); } } }
内容的提问来源于stack exchange,提问作者VGoychev
相关产品推荐
相关产品推荐

