2016-08-31 9 views
0

Ich möchte den Dialog anzeigen, wenn Benutzer auf TextView Inhalt mit Telefonnummer, E-Mail und URL klicken. Nach Benutzerauswahl müssen wir die Standardaktion Spannable Text ausführen oder verweigern Führen Sie eine Standardaktion aus.TextView durchsuchbarer Text Klicken Sie beim Klicken auf E-Mail, Telefon und Internet auf

<TextView 
android:id="@+id/text_view" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:autoLink="all" 
android:text="Here is link. https://www.google.com, Phone number 1-80012-12122 and email [email protected]" /> 
+0

Sie möchten einen separaten Dialog foreach Teil des spannbaren Textes? –

+0

Ich brauche den gleichen Dialog, muss aber Standardaktion durchführen, z. Wenn Telefon, dann gehe zur Wahlnummer – Abhishek

Antwort

0

Sie benötigen Spannable Zeichenfolge mit Linkify zu kombinieren, die ein Filter ist, die Telefonnummern konvertiert, E-Mails, URLs etc.

TextView textView = (TextView)findViewById(R.id.TextView); 
    Spannable spannable = (Spannable)textView.getText(); 
    StyleSpan boldSpan = new StyleSpan(Typeface.BOLD); 
    spannable.setSpan(boldSpan, 41, 52, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    TextView textView2 = (TextView)findViewById(R.id.TextView2); 
    SpannableStringBuilder ssb = new SpannableStringBuilder("Here's a smiley, and here's a link http://blablabla.com"); 
    Bitmap smiley = BitmapFactory.decodeResource(getResources(), R.drawable.emoticon); 
    ssb.setSpan(new ImageSpan(smiley), 16, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    textView2.setText(ssb, BufferType.SPANNABLE); 
    Linkify.addLinks(textView2, Linkify.WEB_URLS); 
0

HINWEIS: Diese Lösung funktioniert für mich.

Lass es in SpannableStringBuilder

tvPost.setText(data.getPost()); 

    CharSequence charSequence = tvPost.getText(); 
    SpannableStringBuilder sp = new SpannableStringBuilder(charSequence); 

    ClickableSpan[] spans = sp.getSpans(0, charSequence.length(), ClickableSpan.class); 
    for (ClickableSpan span : spans) { 
     int start = sp.getSpanStart(span); 
     int end = sp.getSpanEnd(span); 
     AutoSpan mySpan = new AutoSpan(String.valueOf(charSequence.subSequence(start, end))); 
     sp.setSpan(mySpan, sp.getSpanStart(span), 
       sp.getSpanEnd(span), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
    } 

    tvPost.setText(sp); 
    tvPost.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (isAutoLink) { 
       DialogHelper.showExternalLinkAlert(context, null, "My Message", spanText.toString()); 
      } 
      isAutoLink = false; 
     } 
    }); 

erstellen SubClass in Ihrer Aktivität/Fragment/Adapter Datensatz in Textview als CharSequence und konvertieren, wo immer Sie

private boolean isAutoLink = false; 
private static String spanText = ""; 

private class AutoSpan extends ClickableSpan { 

    private String autoLinkData; 

    public AutoSpan(String autoLinkData) { 

     super(); 
     this.autoLinkData = autoLinkData; 
    } 

    @Override 
    public void onClick(View widget) { 

     isAutoLink = true; 
     spanText = autoLinkData; 
    } 

} 

Dialog Implementieren anzuzeigen anwenden müssen Ihre Informationen

public static void showAutoLinkAlert(final Context context, String title, String message, final String autoLink) { 
    AlertDialog mExitDialog; 
    AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogStyle); 
    if (title != null) 
     builder.setTitle(title); 
    builder.setMessage(message); 
    builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent; 
      if (android.util.Patterns.EMAIL_ADDRESS.matcher(autoLink).matches()) { 
       intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", autoLink, null)); 
       context.startActivity(intent); 
      } else if (Patterns.PHONE.matcher(autoLink).matches()) { 
       intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + autoLink)); 
       context.startActivity(intent); 
      } else if (Patterns.WEB_URL.matcher(autoLink).matches()) { 
       intent = new Intent(Intent.ACTION_VIEW, Uri.parse(autoLink)); 
       context.startActivity(intent); 
      } 
     } 
    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    mExitDialog = builder.create(); 
    mExitDialog.show(); 
} 
Verwandte Themen