2015-04-14 41 views
2

我需要在webview中查找特定的文本,但我無法做到。 findall在api級別16之後被棄用,我們需要使用findAllAsync。 我們也可以使用WebView.FindListener。 早些時候我使用findall,它工作正常,但它不工作kitkat和棒棒糖。我已經嘗試findAllAsync,但我無法找到任何特定文本的計數。 我的代碼是在這裏:在webview中查找特定文本的計數android

@Override 
public void onFindResultReceived(int activeMatchOrdinal, 
     int numberOfMatches, boolean isDoneCounting) { 
    if (isDoneCounting) { 
     updateMatchCount(activeMatchOrdinal, numberOfMatches, 
       numberOfMatches == 0); 

    } 
    Log.e("test", "data ::" + numberOfMatches); 

} 

public void findAll() { 
    if (webviewdevelopment == null) { 
     throw new AssertionError(
       "No WebView for FindActionModeCallback::findAll"); 
    } 
    CharSequence find = "Following"; 
    if (0 == find.length()) { 
     webviewdevelopment.clearMatches(); 
     mMatchesFound = false; 
     webviewdevelopment.findAll(null); 
    } else { 
     mMatchesFound = true; 
     mNumberOfMatches = 0; 
     webviewdevelopment.findAllAsync(find.toString()); 

    } 

    Log.e("test", "data ::" + mNumberOfMatches); 

} 

public void updateMatchCount(int matchIndex, int matchCount, 
     boolean isEmptyFind) { 
    if (!isEmptyFind) { 
     mNumberOfMatches = matchCount; 
     mActiveMatchIndex = matchIndex; 
     updateMatchesString(); 
    } else { 

     mNumberOfMatches = 0; 
    } 
} 

private void updateMatchesString() { 
    if (mNumberOfMatches == 0) { 

    } else { 

     Log.e("test", "data ::" + mNumberOfMatches + "," 
       + mActiveMatchIndex + 1 + "," + mNumberOfMatches); 

    } 

} 

private void findNext(boolean next) { 
    if (webviewdevelopment == null) { 
     throw new AssertionError(
       "No WebView for FindActionModeCallback::findNext"); 
    } 
    if (!mMatchesFound) { 
     findAll(); 
     return; 
    } 
    if (0 == mNumberOfMatches) { 
     // There are no matches, so moving to the next match will not do 
     // anything. 
     return; 
    } 
    webviewdevelopment.findNext(next); 
    updateMatchesString(); 
} 

@Override 
public void onFindResultReceived(int activeMatchOrdinal, 
     int numberOfMatches, boolean isDoneCounting) { 
    if (isDoneCounting) { 
     updateMatchCount(activeMatchOrdinal, numberOfMatches, 
       numberOfMatches == 0); 

    } 
    Log.e("test", "data ::" + numberOfMatches); 

} 

任何人有任何的演示,請提供給我,謝謝

回答

3

使用此代碼中的搜索所需的字符串。

webview.findAllAsync("Your String"); 

不是增加FindListener到的WebView

webview.setFindListener(new FindListener() { 

    @Override 
    public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) { 
     Toast.makeText(getApplicationContext(), "Matches: " + numberOfMatches, Toast.LENGTH_LONG).show(); 
    } 
}); 
相關問題