How to enable an edittext component in the same view when clicking on the "other" checkbox component? As in the image:
Try it like this:
CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
//Habilitar EditText
editText.setEnabled(true);
}
else
//Desabilitar EditText
editText.setEnabled(false);
}
});
After performing some searches, follow the answer:
XML
<CheckBox
android:id="@+id/cb_outros"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:button="@drawable/selector_chk"
android:text="@string/cb_outros"
android:textSize="17dp" />
<EditText
android:id="@+id/edt_outroMaterial"
android:layout_width="240dp"
android:visibility="gone"
android:layout_height="wrap_content"
android:hint="Ex: baterias, pilhas..."
android:inputType="text"
android:layout_marginRight="17dp" />
JAVA
private void opcaoCheckOutro(){
cbOutros.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
EditText campoOutroMaterial = view.findViewById(R.id.edt_outroMaterial);
if (cbOutros.isChecked()) {
campoOutroMaterial.setVisibility(View.VISIBLE);
} else {
linearLayoutCheckOutro.setVisibility(View.GONE);
campoOutroMaterial.setText("");
}
}
});
}