具有Edittext可聚焦性的LIstview项目

时间:2013-09-14 08:48:29

标签: android

我有一个listview.in每个项目都有一个textview和一个edittext .. 我跟着this但它并没有帮助我很多。然后尝试了this但仍然没有帮助。我已经为listview项目声明了这样的编辑文本。

<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" />

然后在getview方法中我已经完成了这个

number.setOnTouchListener(new OnTouchListener() {

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


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

之后我甚至在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;
                }
            });  

现在很多人可能想知道为什么我在getview和listview中都编写了number.setontouchlistener以及onitemclick.even我没有找到任何正确的逻辑,但事实是它为我工作。但实际问题是当我点击任何edittext时(即我的号码)  它获得了焦点,但它不允许选择或取消选择自己的项目或列表视图中的任何其他项目。即使我通过编码清除了焦点。任何一个请一些好的解决方案请...

1 个答案:

答案 0 :(得分:0)

试试这个,

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

在布局中写这样的代码,

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

添加这样的代码,

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);
}