单击按钮,EditText获取选择索引

时间:2013-12-24 13:27:05

标签: android android-edittext

我想实现支持标记的纯文本编辑器。

为此,与stackoverflow输入面板一样,当用户选择文本并单击“粗体”按钮时,所选文本将替换为<b> selected text </b>** selected text **

要获取文本的开始和结束索引,请使用以下代码。

int startIndex =  _editTextContent.getSelectionStart();
int endIndex = _editTextContent.getSelectionEnd();

但是,当我单击按钮时,即使GUI仍然显示文本选择标记,也会给出相同的开始和结束值。

我发现Button工作正常但ListView项目点击事件没有给出正确的价值。 我使用HListView来适应各种分辨率。仅供参考,控件几乎与ListView控件相同。

项目点击事件如下。

public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> parent, View view, int position, long id) {
if (_keys.getList().get(position).getId().equals("bold")) {
        try {
            _editTextContent.requestFocus();
            int startIndex =  _editTextContent.getSelectionStart();
            int endIndex = _editTextContent.getSelectionEnd();
            if (startIndex != endIndex){
                _editTextContent.getText().replace(startIndex, endIndex, "**" + _editTextContent.getText().subSequence(startIndex, endIndex) + "**");
                _editTextContent.setSelection(endIndex + 4);

            }
        } catch (Exception ignore) {
        }
    }
}

可能是按钮单击将焦点更改为按钮,以便EditText控件从选择状态切换到正常状态。

要做的解决方法是什么?

请注意,我正在根据API 9开发,因此我无法使用textIsSelectable属性。

0 个答案:

没有答案
相关问题