【问题标题】:LIstview items with Edittext focusability具有 Edittext 可聚焦性的 LIstview 项目
【发布时间】:2013-09-14 08:48:28
【问题描述】:

我有一个列表视图。在每个项目中都有一个文本视图和一个编辑文本.. 我关注了this,但它对我没有多大帮助。然后尝试使用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" />

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

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个,

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

    【讨论】:

    • 在哪里写这个方法?我的意思是在 getview 或 oncreate 或 itemclick 上的 listview 中?
    • 在 listview onitemclick 中试试
    • rply plz...waiting fr ur rply.. @shiju B
    • 但是当我把它放在我的代码中时,出现了一个括号问题..dnt 知道为什么..当我删除此代码时,每个错误点都得到了解决..
    • 在正确的轨道上....但它允许我选择项目,即使在编辑 bt 后也不允许取消选择该项目.. @shiju B
    猜你喜欢
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2016-11-11
    • 2018-07-26
    • 2016-08-27
    相关资源
    最近更新 更多