I have a CheckBox and an EditText field. I want to make sure that when CheckBox active (when they ticked), EditText immediately appear. How can this be implemented?
|
1 answer
From the point of view of the user experience, it is more logical not to completely hide the widget, but to make it inactive, so that the user knows that there is an input field that he can unlock with the checkbox. The code that controls the activity of the input field, depending on the state of the checkbox (the checkbox is checked - the field is active, not checked - not active):
public class MainActivity extends Activity { private EditText editText; private CheckBox checkBox; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); checkBox = (CheckBox) findViewById(R.id.checkBox); editText.setEnabled(checkBox.isChecked()); //editText.setVisibility(checkBox.isChecked()? View.VISIBLE: View.INVISIBLE); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editText.setEnabled(isChecked); //editText.setVisibility(isChecked? View.VISIBLE: View.INVISIBLE); } }); } } If you need to make it invisible, then instead of the lines with editText.setEnabled() (delete them) uncomment the lines with editText.setVisibility() immediately after them (sets visibility depending on the state of the checkbox)
|