2013-04-17 3 views
3

J'ai ce code qui fonctionne pour ma méthode qui appelle un EditText, j'ai essayé d'utiliser le même code pour un TextView mais cela ne fonctionne pas. Le texte ne se transforme pas en un lien hypertexte comme dans EditText, est-ce que quelqu'un sait pourquoi?Est-ce que Linkify fonctionne pour TextView sous Android?

public class MainActivity extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TextView tv = (TextView) findViewById(R.id.link_view); 
    // make sure that setText call comes BEFORE Linkify.addLinks call 
    tv.setText(tv.getText().toString()); 
    Linkify.addLinks(tv, Linkify.WEB_URLS); 
}} 

Voici la mise en page:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TableRow> 

     <TextView 
      android:id="@+id/link_lbl" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:paddingRight="10dip" 
      android:text="Link" /> 

     <TextView 
      android:id="@+id/link_view" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="google.com" /> 
    </TableRow> 
</TableLayout> 

Cela fonctionne bien dans EditText, j'ai juste besoin aider à faire la même chose dans TextView

+0

http://developer.android.com/reference/android/text/util/Linkify.html – Raghunandan

Répondre

2

Avoir une portée cliquable et définir le texte avec la durée clikable. Vous pouvez avoir une couleur personnalisée pour l'intervalle clickabke. Lorsque vous cliquez sur le texte en textview, il affiche un toast.

String title="hello"; 
SpannableString ss1= new SpannableString(title); 
    ss1.setSpan(new MyClickableSpan(title), 0, ss1.length(), 0); 
    tv = (TextView) findViewById(R.id.textview); 
    tv.setText(ss1); 
    tv.setMovementMethod(LinkMovementMethod.getInstance()); 

MyClickableSpan

class MyClickableSpan extends ClickableSpan{  
    String clicked; 
    public MyClickableSpan(String string) 
    { 
    super(); 
    clicked =string; 
    } 
    public void onClick(View tv) 
    { 
    // onclick of text in textview do something 
Toast.makeText(MainActivity.this,clicked ,Toast.LENGTH_SHORT).show(); 
    //display a toast 
    } 
    public void updateDrawState(TextPaint ds) 
    { 
    ds.setColor(Color.BLUE);//set text color 
    ds.setUnderlineText(true); // set to false to remove underline 
    } 
    } 

résultant Snap Shot

enter image description here

EDIT:

Ouvrez un navigateur avec l'URL click sur le texte dans textview. Vous pouvez également passer l'URL à une activité. Récupérez l'URL et chargez l'URL dans Webview.

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

public void onClick(View tv) { 
//do something 

    Toast.makeText(MainActivity.this,clicked , 
     Toast.LENGTH_SHORT).show(); 
    String url = "http://www.example.com"; 
    Intent i = new Intent(Intent.ACTION_VIEW); 
    i.setData(Uri.parse(url)); 
    startActivity(i); 
} 


       OR 

En onClick()

Intent t= new Intent(MainActivity.this,SecondActivity.class); 
    t.putExtra("key","http://www.google.com"); 
    startActivity(t); 

second.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <WebView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/wv"></WebView> 
    </LinearLayout> 

Puis, en SecondActivty

SecondActivity public class étend Activité {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 
    WebView wv= (WebView) findViewById(R.id.wv); 
    Bundle extras= getIntent().getExtras(); 
    if(extras!=null) 
    { 
     wv.loadUrl(extras.getString("key")); 
    } 
} 
} 
+0

si je mets un site web le charge-t-il? – Lucas

+0

@lucas vous devez gérer cela dans onClick(). Avoir un webview et charger l'url dans onClick() ou commencer une nouvelle activité qui charge une webview avec l'URL – Raghunandan

+0

@Lucas vérifier l'édition – Raghunandan

2

Essayez juste en dessous de code. Ça fonctionne bien pour moi.

TextView tv = .... 
tv.setMovementMethod(LinkMovementMethod.getInstance()); 

String content = tv.getText().toString(); 
List<String> links = new ArrayList<String>(); 

Pattern p = Patterns.WEB_URL; 
Matcher m = p.matcher(content); 
while (m.find()) { 
    String urlStr = m.group(); 
    links.add(urlStr); 
} 

SpannableString f = new SpannableString(content); 

for (int i = 0; i < links.size(); i++) { 
    final String url = links.get(i); 

    f.setSpan(new InternalURLSpan(new OnClickListener() { 
     public void onClick(View v) { 
      Context ctx = v.getContext(); 
      String urlToOpen = url; 
      if (!urlToOpen.startsWith("http://") || !urlToOpen.startsWith("https://")) 
       urlToOpen = "http://" + urlToOpen; 
      openURLInBrowser(urlToOpen, ctx); 
     } 
    }), content.indexOf(url), content.indexOf(url) + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
} 

tv.setText(f); 
+0

Vmerror J'ai de la difficulté à implémenter votre code, pourriez-vous m'aider? Je suis un débutant avec Android – Lucas

Questions connexes