问题
I have displayed a gridview whose items are a textview and edittext.Now ,how to achieve function that I click the gridview item and the edittext get the focus?
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
how to override this function? I try that before:
((EditText)gridview.getChildAt(position).findViewById(R.id.grid_edittext)).requestFocus();
and also:
((ViewGroup)gridview.getChildAt(position)).getChildAt(1).requestFocus();
There adds a System.out.printlen("hello"); in onitemclick function,but it doesn't show up in logcat.
Is there any samples??
回答1:
Get EditText from AdapterView and set requestFocus() as below. This may be work.
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
EditText edittext = (EditText) view.findViewById(R.id.grid_edittext);
edt.setFocusable(true);
edittext.setFocusableInTouchMode(true);
if (edittext.requestFocus()) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
}
}
});
EDIT:
Set focusable and focusableInTouchMode to false for EditText as below:
android:focusable="false"
android:focusableInTouchMode="false"
回答2:
When Using gridView, TextView and EditText should not have the flag
android:singleLine="true"
unless you want a foreign focus behavior, you can use instead
android:lines="1",
don't ask me why! it's kind of android OS bug, Hope this will help,
来源:https://stackoverflow.com/questions/25519896/click-gridview-item-and-editext-gets-focus