यहां कई समाधान हैं जो एकाधिक स्टैक ओवरफ़्लो पोस्ट को देखने के बाद मेरे लिए काम करते हैं। मैं इसे अपने कार्यान्वयन के अनुरूप है: LinkMovementMethod का उपयोग कर के पक्ष में
1.निकालें स्वत: लिंक, और सच
<TextView
android:id="@+id/termsOfUse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textSize="14sp"
android:clickable="true"
android:linksClickable="true"
android:textColorLink="@color/colorPrimary"
android:text="@string/agree_terms_privacy"/>
को linksClickable सेट आप android:autoLink="web"
संपत्ति का उपयोग करते हैं तो आपके पास होंगे अपने टेक्स्ट व्यू पर setText()
पर कॉल करने से पहले textView.setAutoLinkMask(0);
के साथ इसे ओवरराइड करने के लिए। यदि आप पसंद करते हैं तो हर्षल के उत्तर की तरह आप अपनी गतिविधि में क्लिक करने योग्य लिंक भी सेट कर सकते हैं, लेकिन मैंने इसे छोड़ दिया क्योंकि आपके पास पहले से ही लेआउट में था। मैंने आपके टेक्स्ट व्यू में एक आईडी भी जोड़ा जिसे termsOfUse
कहा जाता है जिसे हम बाद में उपयोग करेंगे।
2.strings.xml में <
साथ < बदलें और यूआरएल
इसका कारण यह है कि जब आप स्ट्रिंग संसाधन को पुनः प्राप्त, इसे सही ढंग से एम्बेडेड एचटीएमएल पार्स नहीं होगा दोहरे उद्धरण चिह्नों के हटाने, और किसी कारण से यह डबल कोट्स से बच नहीं आता है। तो बजाय:
<string name="agree_terms_privacy">By continuing, you agree to our <a href="http://link1/terms">Terms of Use</a> and read the <a href="http://link1/privacy">Privacy Policy</a></string>
आप क्या करना चाहते होगी:
<string name="agree_terms_privacy">By continuing, you agree to our <a href=http://link1/terms>Terms of Use</a> and read the <a href=http://link1/privacy>Privacy Policy</a></string>
3. स्ट्रिंग संसाधन पार्स और हमारे TextView के लिए यह बाध्यकारी
Spanned policy = Html.fromHtml(getString(R.string.agree_terms_privacy));
TextView termsOfUse = (TextView)findViewById(R.id.termsOfUse);
termsOfUse.setText(policy);
termsOfUse.setMovementMethod(LinkMovementMethod.getInstance());
नोट: एपीआई 24 में Html.fromHtml को बहिष्कृत कर दिया गया है (यदि आवश्यक हो तो इसे संभालने के तरीके के बारे में अधिक जानकारी के लिए this post देखें)। हम अपनी स्ट्रिंग से अपेक्षित HTML स्वरूपण प्राप्त करने के लिए इस विधि का उपयोग करते हैं।
क्या आपने http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable की जांच की है? – user5599807
उत्तर दें http://stackoverflow.com/a/2746708/2435238 – sJy
मैं स्पैन करने योग्य से परिचित होने का सुझाव देता हूं। http://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable। – piotrpo