-1

I have a listview.in each item there is a textview and an edittext.. i have followed this but it doesnt helped me a lot.then tried with this but still no help.I have declared an edittext for listview item like this.

<EditText
        android:id="@+id/dhsv1"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:editable="true" 
        android:maxLength="2"
         android:focusable="false"
         android:focusableInTouchMode="true"
          android:imeOptions="flagNoExtractUi|actionDone"
          android:inputType="textVisiblePassword|number" />

then in getview method i have done this

number.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {


                    number.setFocusableInTouchMode(true);
                    number.setFocusable(true);
                return false;
                }
            });

after that i have even done inside listviewonitemclick

number.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub

                    number.setFocusableInTouchMode(true);
                    number.setFocusable(true);

                    number.setOnEditorActionListener(new OnEditorActionListener() {        
                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            if(actionId==EditorInfo.IME_ACTION_DONE){

                                number.clearFocus();


                                }


                                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.hideSoftInputFromWindow(number.getWindowToken(), 0);
                            }
                        return false;
                        }
                    });

                    return false;
                }
            });  

now many may wonder why i have written number.setontouchlistener in both getview as well as listview onitemclick.even i dont find any correct logic but thing is that its working for me.NOw actual problem is that when i click on any edittext(i.e number in my case) it gains focus but it does not allow to select or deselect its own item or any other item in the listview.even i have cleared the focus through coding.Any one please with some good solution please..

Community
  • 1
  • 1

1 Answers1

0

Try this,

public void setItemsCanFocus(boolean itemsCanFocus) {
        mItemsCanFocus = itemsCanFocus;
        if (!itemsCanFocus) {
            setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        }
    }

In layout write code like this,

<ListView
    android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:descendantFocusability="beforeDescendants"
    />

add code like this,

public void onItemSelected(AdapterView<?> listView, View view, int position, long id)
{
    if (position == 1)
    {
        // listView.setItemsCanFocus(true);

        // Use afterDescendants, because I don't want the ListView to steal focus
        listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        myEditText.requestFocus();
    }
    else
    {
        if (!listView.isFocused())
        {
            // listView.setItemsCanFocus(false);

            // Use beforeDescendants so that the EditText doesn't re-take focus
            listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
            listView.requestFocus();
        }
    }
}

public void onNothingSelected(AdapterView<?> listView)
{
    // This happens when you start scrolling, so we need to prevent it from staying
    // in the afterDescendants mode if the EditText was focused 
    listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
shiju B
  • 1,690
  • 10
  • 23