2013-10-09 15 views
0

在我的字符串中,我需要找到鏈接和電話號碼,然後需要通過下劃線來更改,如需要的鏈接。Android查找電話號碼或鏈接在一個字符串中

在這方面,我需要找到數,如果有號碼,然後它需要打開撥號器。 如果有鏈接,它需要在瀏覽器中打開。

如: -

String myString ="Please check the link www.google.com sadsd asdasd asd. Call us xxx-xxx-xxxx asd asdbsd sdasd" 

在這種www.google.com應在瀏覽器和電話號碼開號開。

+0

現在看到我的答案。我只做了調用按鈕,現在你必須使webview調用按鈕 – Rohit

回答

0

我找到了解決辦法。

我們需要使用spannalbe。

String [] parts = textViewText.split(" "); 
StringBuilder strtextViewBuiler = new StringBuilder(); 
for(String items:parts){ 
if(items.startsWith("http://") || (items.startsWith("https://")) || (items.startsWith("www."))) { 
     if(items.contains("<")){ 
      items = items.substring(0, items.indexOf("<")); 
     } 
     strtextViewBuiler .append(items+" "); 
    } 
    else if(items.contains("<http://")){ 
     int startIndex = items.indexOf("<http://"); 
     int endIndex = items.indexOf(">"); 
     String replacement = ""; 
     String toBeReplaced = items.substring(startIndex, endIndex+1); 

     items = items.replace(toBeReplaced, replacement); 
     strtextViewBuiler .append(items+" "); 
    } 
    else if(items.contains("<https://")){ 
     int startIndex = items.indexOf("<https://"); 
     int endIndex = items.indexOf(">"); 
     String replacement = ""; 
     String toBeReplaced = items.substring(startIndex, endIndex+1);       
     items = items.replace(toBeReplaced, replacement); 
     strtextViewBuiler .append(items+" "); 
    } 
    else{ 
     strtextViewBuiler .append(items+" "); 
    }        
} 
mtextView.setMovementMethod(LinkMovementMethod.getInstance()); 
mtextView.setText(setSpannablePropertyForDescription(strtextViewBuiler .toString()), BufferType.SPANNABLE); 
1

找到電話號碼Look for a phone number in a string

對於下劃線android: how to add Underline in the String

爲dailer

加入menifest

<uses-permission android:name="android.permission.CALL_PHONE" /> 

呼叫按鈕

Button callButton = (Button)findViewById(R.id.btnCall); 
txtPhn = (EditText)findViewById(R.id.txtPhnNumber); 
callButton.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     try { 
       Intent callIntent = new Intent(Intent.ACTION_CALL); 
       callIntent.setData(Uri.parse("tel:"+txtPhn.getText().toString())); 
       startActivity(callIntent); 
      } catch (ActivityNotFoundException activityException) { 
       Log.e("Calling a Phone Number", "Call failed", activityException); 
      } 
    } 
}); 

試試這個

<Button 
android:id="@+id/btnview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@null" 

/> 

Button btn = (button)findViewbyid(R.id.btnview); 
btn.setText("YOUR Phone number"); 
btn.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
Intent callIntent = new Intent(Intent.ACTION_CALL); 
       callIntent.setData(Uri.parse("tel:"+btn.getText().toString())); 
       startActivity(callIntent); 
      } catch (ActivityNotFoundException activityException) { 
       Log.e("Calling a Phone Number", "Call failed", activityException); 
      } 

       } 
      }); 
+0

我的字符串將像「打電話給我們xxx-xxx-xxxx」 – Navaneethan

+0

是的,拿一個按鈕和設置文本我們的電話號碼和設置背景=「@ null」 – Rohit

+0

就像那個設置文本的URL和背景的第二個按鈕null和onclick執行你的操作 – Rohit