2012-02-09 22 views
21

में एक क्लिक करने योग्य लिंक जोड़ना संभव है, जब मैं उपयोगकर्ता को अपने ऐप्स में से किसी एक का उपयोग करता हूं तो मैं आमतौर पर किसी प्रकार का AlertDialog सेट करता हूं और मैं बताता हूं कि ऐप का उपयोग कैसे करें और समग्र परिचय दें जो उन्होंने अभी डाउनलोड किया है। मैं आमतौर पर strings.xml फ़ाइल से अपने तारों को लोड करता हूं।एंड्रॉइड- क्या एक स्ट्रिंग संसाधन

मैं जो करना चाहता हूं वह मेरे स्ट्रिंग संसाधन में शब्दों में से एक को वेब पेज पर हाइपरलिंक की तरह क्लिक करने योग्य बनाता है। असल में आपके पास AlertDialog होगा और स्ट्रिंग संसाधन के भीतर एक हाइलाइट किया गया शब्द होगा या संभवतः केवल एक वेब पता होगा जिसे वे दबा सकते हैं। मुझे लगता है कि मैं सिर्फ एक बटन जोड़ सकता हूं जो उन्हें साइट पर ले जाएगा लेकिन मैं सिर्फ यह जानना चाहता था कि आपके स्ट्रिंग संसाधन में कोई शब्द बनाना एक क्लिक करने योग्य हाइपरलिंक संभव था।

उत्तर

43

बस अपने संसाधन में एक HTML स्वरूप लिंक का उपयोग करें:

<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>

तब आप अपने TextView पर setMovementMethod(LinkMovementMethod.getInstance()) उपयोग कर सकते हैं लिंक क्लिक करने योग्य बनाने के लिए।

TextView की android:autoLink विशेषता भी है जो काम करना चाहिए।

+1

बहुत बढ़िया :) मैं इसे स्थापित करने की कोशिश करता हूं और जब मैं इसे काम करने के लिए वापस पोस्ट करता हूं :) –

+4

एंड्रॉइड: autoLink = "web" मेरे लिए पूरी तरह से काम करता है। – Rajkiran

+1

मुझे लगता है कि स्ट्रिंग * सब कुछ * लिंक के बाद प्रदर्शित नहीं होता है (इसलिए ' यह दिखाता है और so does this लेकिन यह' नहीं है)। आप यह कैसे करते हैं? –

5

एंड्रॉइड स्ट्रिंग नहीं बनाता है जिसमें वैध लिंक स्वचालित रूप से क्लिक करने योग्य होता है। आप क्या कर सकते हैं, अपने संवाद में कस्टम व्यू जोड़ना और अलर्ट संदेश दिखाने के लिए वेब व्यू का उपयोग करना है। उस स्थिति में, आप अपने संसाधनों में एचटीएमएल स्टोर कर सकते हैं और वे क्लिक करने योग्य होंगे।

View alertDialogView = LayoutInflater.inflate(R.layout.alert_dialog_layout, null); 

WebView myWebView = (WebView) alertDialogView.findViewById(R.id.dialogWebView); 
myWebView.loadData("<a href=\"http://google.com\">Google!</a>", "text/html", "utf-8"); 
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this); 
builder.setView(alertDialogView); 

alert_dialog_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="vertical"> 
<WebView android:id="@+id/dialogWebView" android:layout_height="wrap_content" 
    android:layout_width="wrap_content" /> 
+0

अरे धन्यवाद के साथ काम करता है जानकारी :) मैंने एक वेबव्यू का उपयोग करने के बारे में भी सोचा नहीं है जो बहुत अच्छा है। –

+2

ऐसी छोटी सी चीज़ के लिए वेबव्यू का उपयोग करना वास्तव में एक बुरा विचार है। –

3

मुझे कुछ दिलचस्प मिला। अगर आप में से कोई भी इसे देखता है तो मुझे बताएं।

हाइपरलिंक नीचे काम नहीं कर रहा है, तो आप

android:autoLink="web" 

का उपयोग लेकिन

TextView link = (TextView) findViewById(R.id.link); 
link.setMovementMethod(LinkMovementMethod.getInstance()); 

<string name="my_link"> 
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> 
     Click me! 
    </a> 
</string> 

साथ काम करता है, लेकिन अगर आप नीचे दिए गए लिंक का उपयोग इसके लिए दोनों

android:autoLink="web" (or) 
TextView link = (TextView) findViewById(R.id.link); 
link.setMovementMethod(LinkMovementMethod.getInstance()); 

<string name="my_link"> 
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> 
     http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource 
</string> 
    </a> 
संबंधित मुद्दे