2013-04-18 29 views
0

我使用以下代碼將TEXT從其他應用程序通過共享菜單發送到我的應用程序,並在EditText中顯示TEXT。如何格式化通過共享菜單發送的文本字符串

Intent receivedIntent = getIntent();  
    String receivedAction = receivedIntent.getAction();  
    String receivedType = receivedIntent.getType(); 
    TextView txtView = (EditText) findViewById(R.id.edWord); 
    //if(receivedAction.equals(Intent.ACTION_SEND)){ 
    if (Intent.ACTION_SEND.equals(receivedAction) && receivedType != null) { 
     if(receivedType.startsWith("text/")) {          
      String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT).toLowerCase(); 
      if (receivedText != null) 
      {     
       txtView.setText(receivedText); 
       txtView.requestFocus(); 
       ListView myList=(ListView) findViewById(R.id.lstWord); 
       myList.setFocusableInTouchMode(true); 
       myList.setSelection(0); 
      } 
      else 
       txtView.setText(""); 
     } 
    } 

一切運作良好,即發送文本顯示在我的EditText(在上面的代碼即edWord)。但問題是,通過共享發送的文本有時由無意義的元素或衍生物組成,例如:"word,word',word,looked,books,tomatoes

現在我想要的是格式化文本,使其僅包含真正的單詞或單詞的基本形式,然後纔將其添加到EditText。

我聽說過approximate string matchingfuzzy searching但我不知道如何將它應用於我的代碼。我想知道是否可以給我一些幫助來解決上述問題,至少格式化/剝離非單詞元素。

在此先感謝。

回答

0

我想我已經找到了我的問題的第一部分的答案,即從字符串中刪除非字元素(開始和/或結束字符串)。這裏是有點正則表達式的算法,我使用的代碼:

String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT); 
      if (receivedText != null)     
      {     
       receivedText = receivedText.toLowerCase(); 

       //Remove all non-word elements starting and/or ending a string 
       String strippedInput = receivedText.replaceAll("^\\W+|\\W+$", ""); 
       System.out.println("Stripped string: " + strippedInput); 

       txtView.setText(strippedInput); 
       txtView.requestFocus(); 
       ListView myList=(ListView) findViewById(R.id.lstWord); 
       myList.setFocusableInTouchMode(true); 
       myList.setSelection(0); 
      } 

對於我的問題,這是關於模糊搜索的第二部分,我想這或多或少涉及到了重新編碼怎麼我的應用程序搜索來自其SQLlite數據庫的結果。這仍然是我未回答的問題。

相關問題