Skip to content

Commit f83c999

Browse files
committed
Fixed keyboard focus bug
1 parent 48e2f15 commit f83c999

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

sys/android/src/com/tbd/UnNetHack/TilesetPreference.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import android.text.Editable;
1717
import android.text.TextWatcher;
1818
import android.util.AttributeSet;
19+
import android.view.KeyEvent;
1920
import android.view.View;
2021
import android.view.ViewGroup;
22+
import android.view.inputmethod.EditorInfo;
2123
import android.widget.*;
2224

2325
public class TilesetPreference extends Preference implements PreferenceManager.OnActivityResultListener
@@ -36,6 +38,8 @@ public class TilesetPreference extends Preference implements PreferenceManager.O
3638
private Bitmap mCustomTileset;
3739
private ImageButton mBrowse;
3840
private Bitmap mCustomTile;
41+
private boolean mTileWFocus;
42+
private boolean mTileHFocus;
3943

4044
public TilesetPreference(Context context, AttributeSet attrs)
4145
{
@@ -74,6 +78,48 @@ public void onClick(View v)
7478
});
7579
mTilesetPath = (TextView)mRoot.findViewById(R.id.image_path);
7680

81+
///////////////////////////////////////////////////////////////////////////////////////////////////////
82+
// Workaround for weird focus problem
83+
// When the keyboard is opened because an input field receive focus the entire view is recreated,
84+
// which makes the field lose focus again
85+
mTileW.setSelectAllOnFocus(true);
86+
mTileH.setSelectAllOnFocus(true);
87+
88+
TextView.OnEditorActionListener onEditorActionListener = new TextView.OnEditorActionListener()
89+
{
90+
@Override
91+
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
92+
{
93+
if(actionId == EditorInfo.IME_ACTION_DONE)
94+
{
95+
mTileWFocus = false;
96+
mTileHFocus = false;
97+
}
98+
return false;
99+
}
100+
};
101+
mTileW.setOnEditorActionListener(onEditorActionListener);
102+
mTileH.setOnEditorActionListener(onEditorActionListener);
103+
104+
mTileW.setOnFocusChangeListener(new View.OnFocusChangeListener()
105+
{
106+
@Override
107+
public void onFocusChange(View v, boolean hasFocus)
108+
{
109+
mTileWFocus = hasFocus;
110+
}
111+
});
112+
113+
mTileH.setOnFocusChangeListener(new View.OnFocusChangeListener()
114+
{
115+
@Override
116+
public void onFocusChange(View v, boolean hasFocus)
117+
{
118+
mTileHFocus = hasFocus;
119+
}
120+
});
121+
///////////////////////////////////////////////////////////////////////////////////////////////////////
122+
77123
return mRoot;
78124
}
79125

@@ -102,6 +148,14 @@ protected void onBindView(View view)
102148
mTileW.addTextChangedListener(updateCustom);
103149
mTileH.addTextChangedListener(updateCustom);
104150
mTilesetPath.addTextChangedListener(updateCustom);
151+
152+
if(mTileWFocus)
153+
mTileW.requestFocus();
154+
else if(mTileHFocus)
155+
mTileH.requestFocus();
156+
157+
mTileWFocus = false;
158+
mTileHFocus = false;
105159
}
106160

107161
private void choseCustomTilesetImage()
@@ -148,6 +202,14 @@ private void setCustomUIEnabled(boolean enabled)
148202
persistCustom();
149203
setTreeEnabled(mTilesetUI, enabled);
150204
mTilesetPath.setEnabled(false);
205+
206+
if(!enabled)
207+
{
208+
mTileW.clearFocus();
209+
mTileH.clearFocus();
210+
mTileWFocus = false;
211+
mTileHFocus = false;
212+
}
151213
}
152214

153215
private void setTreeEnabled(View view, boolean enabled)

0 commit comments

Comments
 (0)