-2

私はアンドロイドでアプリを開発しており、ボックスに入力したテキストをキャプチャするメソッドを開発したいと考えていますが、ユーザーにボタンを押してほしくないのですが、テキストがボックスに書かれている瞬間に、数秒でテキストが取得され、それを他のテキストと比較します。

4

3 に答える 3

1

EditText.addTextChangedListener()メソッドを使用して、EditText テキストの変更をリッスンできます。

このサンプル実装を確認できます:

editText = (EditText) findViewById(R.id.edit_text);
editText.addTextChangedListener(new TextWatcher(){
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String text = s.toString();
    }

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

    @Override
    public void afterTextChanged(Editable s) {
    }
}
于 2015-02-17T18:53:16.997 に答える
0

EditText に onTextChangedListener を追加すると、次のようになります。

textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        String text = txtMessage.getText().toString();
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 
于 2015-02-17T18:54:34.723 に答える
0

を追加しTextWatcherEditText非同期タスクを作成し、必要な時間スリープ状態に送信してから、 からの入力テキストを使用して任意の操作を行うことができますEditText。タスクの実行中にテキストが変更された場合は、タスクをキャンセルして、次のように新しいタスクを開始します。

private UpdateFilterTask currentFilterTask = null;

private class UpdateFilterTask extends AsyncTask {

    private String mInputText;

    public UpdateFilterTask(String inputText){
        this.mInputText = inputText;
    }

    @Override
    protected Object doInBackground(Object[] params) {
        try {
            // Set the desired amount of time to wait in here, right now is 5 secs
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            return false;
        }

        if(this.isCancelled()){
            return false;
        }

        return true;
    }

    @Override
    protected void onPostExecute(Object o) {
        Boolean didFinish = (Boolean) o;
        if(didFinish){
            // Do whatever you like with the mInputText
        }
    }
}

private class SearchTextWatcher implements TextWatcher {
    @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    @Override public void afterTextChanged(Editable s) {}
    @Override
    public void onTextChanged(final CharSequence s, int start, int before, int count) {
        if(currentFilterTask != null){
            // If there was a running task previously, interrupt it
            currentFilterTask.cancel(true);
            currentFilterTask = null;
        }

        if(s.toString().trim().isEmpty()){
            // Return to the original state as the EditText is empty now
            return;
        }

        currentFilterTask = new UpdateFilterTask(s.toString());
        currentFilterTask.execute();
    }
}
于 2015-02-17T19:51:20.780 に答える