Reputation: 1451
I have a series of dynamically generated EditTexts in an Android Activity. They are generated based on the below XML:
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:textCursorDrawable="@null"
/>
In some circumstances I want to disable them but let them remain visible. I am doing this by setting their focusable property to false (I have reasons for using focusable instead of enabled):
if (view2 is EditText)
{
((EditText)view2).Focusable = false;
}
When I want to reenable the EditText I set the focusable property to true:
foreach (EditText disEditText in m_disabledEditTexts)
{
disEditText.Focusable= true;
}
However, this does not make the EditText respond to the user touching it. I have spent all morning fiddling with this and I haven't been able to solve it! How can I make the EditText responsive again after setting focusable to false.
If the EditText does not have focusable set to false it works fine.
This is written in C# / Xamarin
.
Upvotes: 1
Views: 1320
Reputation: 13176
You need to also add the FocusableInTouchMode
property so it can be focused again in touch mode. Thus said, you should probably set FocusableInTouchMode
to true after you've set Focusable
to false.
The framework will handle routine focus movement in response to user input. This includes changing the focus as views are removed or hidden, or as new views become available. Views indicate their willingness to take focus through the isFocusable() method. To change whether a view can take focus, call setFocusable(boolean). When in touch mode (see notes below) views indicate whether they still would like focus via isFocusableInTouchMode() and can change this via setFocusableInTouchMode(boolean).
Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides by using these XML attributes in the layout file:
nextFocusDown nextFocusLeft nextFocusRight nextFocusUp
To get a particular view to take focus, call requestFocus().
Reference: http://developer.android.com/reference/android/view/View.htm
Upvotes: 1