Android: Reset button Button Permanently disabled Other button

I currently have four buttons. The fourth button is the reset button, which I added to reset for the state when it was originally started. However, when I added the reset button, it disabled one of the buttons permanently, even if they had previously been turned on / off as intended. Here is the relevant code:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_name); // non relevant code okButton = (Button) findViewById(R.id.ok); okButton.setOnClickListener(this); changeButton = (Button) findViewById(R.id.change_button); changeButton.setOnClickListener(this); nextButton = (Button) findViewById(R.id.next_button); nextButton.setEnabled(false); nextButton.setOnClickListener(this); reset = (Button) findViewById(R.id.reset); reset.setEnabled(false); reset.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ok: changeButton.setEnabled(false); okButton.setEnabled(false); nextButton.setEnabled(true); break; case R.id.change_button: changeButton.setEnabled(false); okButton.setEnabled(false); nextButton.setEnabled(true); break; case R.id.next_button: nextButton.setEnabled(false); okButton.setEnabled(true); changeButton.setEnabled(true); break; case R.id.reset: Intent intent = getIntent(); finish(); startActivity(intent); break; default: break; } } 

Everything worked as it should have been until all parts were added using the reset button. Essentially, I want:

  • Initially: next and reset are disabled, they become available when you click on change or ok

  • When you click on any change or ok, they are both disabled (so as not to press more than once), and both reset and then enabled

  • When you click on the next or reset, they become disabled and change, and ok becomes enabled.

Changes, ok and the following button actions worked until the reset code was added. Then the next button was permanently disabled. What's wrong? How to fix it?

EDIT *:

Here is the xml code for the buttons:

 <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/change_button" android:layout_alignBottom="@+id/change_button" android:layout_toLeftOf="@+id/reset" android:text="@string/Ok" /> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/change_button" android:layout_alignBottom="@+id/change_button" android:layout_alignParentLeft="true" android:text="@string/next_button" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="@string/reset" /> <Button android:id="@+id/change_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="14dp" android:layout_toRightOf="@+id/next_button" android:text="@string/change" /> 
+2
source share
3 answers

Initially: next and reset are disabled, they turn on when either change or ok, click

 @Override public void onClick(View v) { switch (v.getId()) { case R.id.ok: changeButton.setEnabled(false); okButton.setEnabled(false); nextButton.setEnabled(true); reset.setEnabled(true); // add this break; case R.id.change_button: changeButton.setEnabled(false); okButton.setEnabled(false); nextButton.setEnabled(true); reset.setEnabled(true); // add this break; case R.id.next_button: nextButton.setEnabled(false); okButton.setEnabled(true); changeButton.setEnabled(true); reset.setEnabled(false); // add this break; case R.id.reset: Intent intent = getIntent(); finish(); startActivity(intent); break; default: break; } } 

This worked for me / add these lines.

+2
source

You can follow in a way that helps you with good programming practices.

 @Override public void onClick(View v) { switch (v.getId()) { case R.id.ok: setButtonStates(false,false,true,true); break; case R.id.change_button: setButtonStates(false,false,true,true); break; case R.id.next_button: setButtonStates(true,true,false,false); break; case R.id.reset: Intent intent = getIntent(); finish(); startActivity(intent); break; default: break; } } public void setButtonStates(boolean flag1, boolean flag2, boolean flag3, boolean flag4){ changeButton.setEnabled(flag1); okButton.setEnabled(flag2); nextButton.setEnabled(flag3); reset.setEnabled(flag4); } 

Thus, your code looks clear and easy to modify if necessary.

Hope this helps you.

Thanks.

+2
source

This is what you want, comment if any changes you want

 //Buttons Button okButton,changeButton,resetButton,nextButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // non relevant code okButton = (Button) findViewById(R.id.okButton); okButton.setOnClickListener(this); changeButton = (Button) findViewById(R.id.changeButton); changeButton.setOnClickListener(this); nextButton = (Button) findViewById(R.id.nextButton); nextButton.setEnabled(false); nextButton.setOnClickListener(this); resetButton = (Button) findViewById(R.id.resetButton); resetButton.setEnabled(false); resetButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.okButton: changeButton.setEnabled(false); okButton.setEnabled(false); nextButton.setEnabled(true); resetButton.setEnabled(true); break; case R.id.changeButton: changeButton.setEnabled(false); okButton.setEnabled(false); nextButton.setEnabled(true); resetButton.setEnabled(true); break; case R.id.nextButton: nextButton.setEnabled(false); resetButton.setEnabled(false); okButton.setEnabled(true); changeButton.setEnabled(true); break; case R.id.resetButton: resetButton.setEnabled(false); nextButton.setEnabled(false); okButton.setEnabled(true); changeButton.setEnabled(true); Intent intent = getIntent(); finish(); startActivity(intent); break; default: break; } } 
+1
source

All Articles