【问题标题】:Uncheck / deselect singleChoice ListView取消选中/取消选择 singleChoice ListView
【发布时间】:2012-07-06 11:03:46
【问题描述】:

组合 - 带有写入未列出值的选项的列表

<ListView
        android:id="@+id/category_choices"
        android:choiceMode="singleChoice"
        android:layout_width="400dp"
        android:layout_height="wrap_content">
</ListView>

<EditText
        android:id="@+id/other_please_specify"
        android:singleLine="true"    
        android:layout_height="wrap_content" android:layout_width="326dp"/>

数据绑定作品

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
        android.R.layout.simple_list_item_single_choice, categories);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
           selectedItem = (String) (listView.getItemAtPosition(myItemInt));
        }
});

在EditText中输入任何文本时如何清除单选按钮选择?

我尝试了以下方法,但它不起作用

editText.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (selectedItem != null) {
            int checkedItem = listView.getCheckedItemPosition();
            listView.setItemChecked(checkedItem, false);
            selectedItem = null;
        }
        return false;
    }
});

【问题讨论】:

  • 如果您在编辑框中输入任何文本,是否正在调用 KeyListener?

标签: java android user-interface android-listview user-interaction


【解决方案1】:

您必须使用 Text Watcher 编辑文本以侦听输入的文本:

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
                         // do your stuffs here
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.e("TextWatcherTest", "afterTextChanged:\t" + s.toString());
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    相关资源
    最近更新 更多