2012-06-29 81 views
1

我試着按照下面的例子來使我的警告對話框中點擊我的TextView的HTTP鏈接,但它不工作:HTTP鏈接對話框不工作

How do I make links in a TextView clickable?

我也只有這樣,得到它的工作是如果我把鏈接的文本直接放入我的字符串並將android:autoLink="web"放入我的TextView中。但我不想看到整個鏈接,只是名稱。

這裏就是創建我的對話:

@Override 
protected Dialog onCreateDialog(int id) { 
    LayoutInflater factory = LayoutInflater.from(this); 

    switch (id) { 
    case DISCLAIMER: 
      final View disclaimerView = factory.inflate(R.layout.disclaimer_dialog, null); 
      final TextView dtv = (TextView) disclaimerView.findViewById(R.id.disclaimer_body); 

      dtv.setText("v"+versionName+"\n\n\u00A9"+Calendar.getInstance().get(Calendar.YEAR)+" "+"Developer\n\n" + 
       getString(R.string.alert_dialog_disclaimer_body)); 

      dtv.setMovementMethod(LinkMovementMethod.getInstance());     

      return new AlertDialog.Builder(MyActivity.this)     
       .setView(disclaimerView) 
       .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
        } 
       }) 
       .create(); 
    } 

    return null; 
} 

,這裏是我的TextView位於R.layout.disclaimer_dialog

<TextView 
    android:id="@+id/disclaimer_body" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="10dp" 
    android:gravity="center" 
    /> 

這裏是我的字符串資源:

<string name="alert_dialog_disclaimer_body">This application was developed and written by me.\n\nGraphs use AChartEngine licensed under <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License 2.0</a>.\n\nThe rest goes here.</string> 

回答

1

的文檔TextView.setText(CharSequence)說明:

設置TextView的字符串值。 TextView確實不是接受類似HTML的格式,您可以使用XML資源文件中的文本字符串進行格式設置。要設置字符串的樣式,請將android.text.style。*對象附加到SpannableString,或者參閱可用資源類型文檔以獲取在XML資源文件中設置格式化文本的示例。

而繼他們的建議,Formatting and Styling解釋的那樣,造型與HTML標記標題,你怎麼可以同時擁有HTML標記和格式代碼下。

我認爲你情況中最簡單的事情就是調整你的佈局,讓你的版權聲明在一個你已經使用的視圖中,並且將下面的聲明放在第二個視圖中,

<TextView 
    android:id="@+id/disclaimer_body" 
    android:text="@string/alert_dialog_disclaimer_body" 
    ... 
    /> 
+0

嗯,我想你可能還需要'格式化= 「假」'字符串中的元素:'<字符串格式化= 「假」 名稱= 「alert_dialog_disclaimer_body」>'。 –

+0

@ jsstp24n5我意識到真正的問題是什麼,並重寫了我的答案。 –

+0

謝謝......在與html格式文檔爭鬥後,您對2個textviews的建議正是我在看到它之前所追求的。再次感謝。 – jsstp24n5