Reputation: 328
I am facing problem with enabling an edittext box at initialisation. I have disabled my editbox like so: text_edit.setFocusable(false);
Later, I want to enable the textbox via the click of a checkbox. The textbox is not being enabled, here is my click event code:
if(((CheckBox) v).isChecked()) {
Toast.makeText(User_name_search .this, "Selected", Toast.LENGTH_SHORT).show();
text_edit.setFocusable(false);
//checkbox.setChecked(false);
} else {
Toast.makeText(User_name_search .this, "Not Selected", Toast.LENGTH_SHORT).show();
text_edit.setFocusable(true);
}
What am I doing wrong?
Upvotes: 0
Views: 2986
Reputation: 1
In the click listener of the checkbox set the enabled property of the edittext to true. So if checkAbove is your checkbox and textAbove is your editText, the code will look like this..
checkAbove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox temp = (CheckBox)view;
if (temp.isChecked()){
textAbove.setEnabled(true);
}else {
textAbove.setEnabled(false);
}
}
});
Also put
android:editable = "false"
for the textAbove view in the xml.
Upvotes: 0
Reputation: 1932
You have to do the following
Checkbox c1;
EditText e1;
click on check box c1 if
if(c1.isChecked==true)
{
e1.setEnabled(true);
}
else
{
e1.setEnabled(false);
}
Upvotes: 0
Reputation: 5183
Since you want to enable/disable the EditText
, you should consider use the setEnabled(boolean x)
method as described here: http://developer.android.com/reference/android/widget/TextView.html#setEnabled%28boolean%29 (EditText
extends TextView
).
Hope this helps!
Upvotes: 1
Reputation: 3084
You should use setOnClickListener
or setOnCheckedChangeListener
for the checkbox, and when somebody clicks, you should change setfocusable
.
Upvotes: 0