在edittext

时间:2017-07-17 14:45:53

标签: android android-edittext selection textwatcher

这可能看起来像一个简单的问题,但我不明白它为什么会发生。我希望在用户更改文本之前在我的edittext中获取选择的开始和结束索引,所以在我的TextWatcher中我做了如下操作: -

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


                    selectionStart = edittext.getSelectionStart();
                    selectionEnd = edittext.getSelectionEnd();


            }

然而,selectionStart和selectionEnd都返回选择的结束索引。例如,我选择一个单词" hello "两者都返回 5 。我尝试在处理程序中插入它们以在任何键盘输入之前获得选择无效。

2 个答案:

答案 0 :(得分:0)

我终于找到了解决这个问题的方法,它有点难看,但最佳地解决了问题。问题是在textchanged之前文本选择已经消失所以我不得不使用处理程序等待选择文本和跨度观察者之后立即设置选择: -

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>

<input type="button" onclick="MyFunction(this)" value="Click Me!!" class="my-class"/>

当然这只有在选择文本时才能用于常规打字,因为它会在文本更改后立即自动更新,所以在文本观察者中我添加了这段代码: -

final SpanWatcher watcher = new SpanWatcher() {
@Override
public void onSpanAdded(final Spannable text, final Object what,final int start, final int end) {

final Handler handler = new Handler();//handler to get selection after 
certain time
handler.postDelayed(new Runnable() {
@Override
public void run() {

if(noteview.hasSelection()) {

higlightdetect=1;//integer to detect that text was selected

selectionStart = noteview.getSelectionStart();
selectionEnd = noteview.getSelectionEnd();

                    }else
higlightdetect=0;
                }
            }, 600);//after 600ms seemed to be the optimal time after selection occurs
        }
}

@Override
public void onSpanRemoved(final Spannable text, final Object what,
                                  final int start, final int end) {
            // Nothing here.
        }

@Override
public void onSpanChanged(final Spannable text, final Object 
what,final int ostart, final int oend, final int nstart, final int nend) 
{
//The same

final Handler handler = new Handler();//handler to get selection after 
certain time
handler.postDelayed(new Runnable() {
@Override
public void run() {

if(noteview.hasSelection()) {

higlightdetect=1;//integer to detect that text was selected

selectionStart = noteview.getSelectionStart();
selectionEnd = noteview.getSelectionEnd();

                    }else
higlightdetect=0;
                }
            }, 600);//after 600ms seemed to be the optimal time after selection occurs
        }
};


edittext.getText().setSpan(watcher, 0, edittext.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);//set spanwatcher to edittext

答案 1 :(得分:0)

为什么不简单地在beforeTextChanged方法中使用var https = require('https'); var options = { host: 'https://api.applymagicsauce.com', port: 443, path: '/auth', method: 'POST' }; var req = https.request(options, function(res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); // write data to request body req.write('data\n'); req.write('data\n'); req.end(); 参数?