-1

我有一个listview。在每个项目中都有一个textview 和一个edittext .. 我已经遵循了这个,但它并没有帮助我很多。然后尝试了这个但仍然没有帮助。我已经为这样的listview 项目声明了一个edittext。

<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 onitemclick中都写了number.setontouchlistener。即使我没有找到任何正确的逻辑,但事情是它对我有用。现在实际的问题是当我点击任何edittext(即数字在我的情况下)它获得焦点,但它不允许选择或取消选择它自己的项目或列表视图中的任何其他项目。即使我已经通过编码清除了焦点。请任何人提供一些好的解决方案..

4

1 回答 1

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);
}
于 2013-09-14T08:58:51.337 回答