下面列出了android.text.util.Linkify#addLinks ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private View inflateRowItem(String title, String value) {
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.detailed_poi_tagitem, null);
//LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.rowContainer);
TextView titleTextView = (TextView) view.findViewById(R.id.rowTitle);
TextView valueTextView = (TextView) view.findViewById(R.id.rowValue);
titleTextView.setText(title);
valueTextView.setText(value);
//Linking content
if (title.toLowerCase().equals("email") || title.toLowerCase().equals("contact:email")) {
Linkify.addLinks(valueTextView, Linkify.EMAIL_ADDRESSES);
valueTextView.setLinksClickable(true);
}
if (title.toLowerCase().equals("website") || title.toLowerCase().equals("contact:website")) {
Linkify.addLinks(valueTextView, Linkify.WEB_URLS);
valueTextView.setLinksClickable(true);
}
if (title.toLowerCase().equals("phone") || title.toLowerCase().equals("phone:mobile") || title.toLowerCase().equals("contact:mobile") || title.toLowerCase().equals("contact:phone")) {
Linkify.addLinks(valueTextView, Linkify.PHONE_NUMBERS);
valueTextView.setLinksClickable(true);
}
return view;
}
private SpannableString linkifyMessageBody(SpannableString messageBody) {
int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS;
boolean hasLinks = Linkify.addLinks(messageBody, linkPattern);
if (hasLinks) {
Stream.of(messageBody.getSpans(0, messageBody.length(), URLSpan.class))
.filterNot(url -> LinkPreviewUtil.isLegalUrl(url.getURL()))
.forEach(messageBody::removeSpan);
}
return messageBody;
}
private SpannableString getDescriptionMessage(Context context) {
InputStream message = context.getResources().openRawResource(R.raw.description);
String stringMessage = convertFromInputStreamToString(message).toString();
final SpannableString linkedMessage = new SpannableString(Html.fromHtml(stringMessage));
Linkify.addLinks(linkedMessage, Linkify.EMAIL_ADDRESSES | Linkify.WEB_URLS);
return linkedMessage;
}
private static void linkifyUrl(Spannable spannable) {
Linkify.TransformFilter transformFilter = new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher matcher, String value) {
return value;
}
};
Linkify.addLinks(spannable, PATTERN_URL, SCHEME_URL, null, transformFilter);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View customView = getActivity().getLayoutInflater().inflate(R.layout.blur_dialog_dialog_fragment, null);
TextView label = ((TextView) customView.findViewById(R.id.textView));
label.setMovementMethod(LinkMovementMethod.getInstance());
Linkify.addLinks(label, Linkify.WEB_URLS);
builder.setView(customView);
return builder.create();
}
@Test
public void testSpannable() {
final Spannable text = new SpannableString("Test http://www.test.de !");
Linkify.addLinks(text, Linkify.WEB_URLS);
assertEquals(
"<p dir=\"ltr\">Test <a href=\"http://www.test.de\">http://www.test.de</a> !</p>\n",
Html.toHtml(text));
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.textview);
Linkify.addLinks(view, Linkify.WEB_URLS| Linkify.EMAIL_ADDRESSES);
}
private void showEula()
{
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean hasBeenShown = prefs.getBoolean(getString(R.string.app_eulaaccepted), false);
if (hasBeenShown == false)
{
final SpannableString message = new SpannableString(Html.fromHtml(getString(R.string.app_eula).replace("/n", "<br/>")));
Linkify.addLinks(message, Linkify.WEB_URLS);
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(getString(R.string.app_name)).setIcon(R.drawable.icon).setMessage(message)
.setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
prefs.edit().putBoolean(getString(R.string.app_eulaaccepted), true).commit();
wait = false;
dialogInterface.dismiss();
}
}).setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialoginterface, int keyCode, KeyEvent event)
{
return !(keyCode == KeyEvent.KEYCODE_HOME);
}
}).setCancelable(false);
AlertDialog d = builder.create();
d.show();
// Make the textview clickable. Must be called after show()
((TextView) d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
}
else
{
wait = false;
}
}
/**
* Just info about the app :-)
*/
private void showAboutDialog() {
final SpannableString spannableString = new SpannableString(getString(R.string.about_msg));
Linkify.addLinks(spannableString, Linkify.ALL);
new MaterialDialog.Builder(this)
.positiveText(android.R.string.ok)
.title(getString(R.string.app_name) + " " + getString(R.string.app_version))
.content(spannableString)
.show();
}
private void handleReceivedErrors(Bundle arguments) {
if (arguments.containsKey(CREDENTIAL_ERRORS.PASSWORD_INVALID_LENGTH.toString())) {
updatePasswordError(getString(R.string.error_not_valid_password_user_message));
} else if (arguments.containsKey(CREDENTIAL_ERRORS.RISEUP_WARNING.toString())) {
userMessage.setVisibility(VISIBLE);
userMessage.setText(R.string.login_riseup_warning);
}
if (arguments.containsKey(CREDENTIALS_USERNAME)) {
String username = arguments.getString(CREDENTIALS_USERNAME);
usernameField.setText(username);
}
if (arguments.containsKey(CREDENTIAL_ERRORS.USERNAME_MISSING.toString())) {
updateUsernameError(getString(R.string.username_ask));
}
if (arguments.containsKey(USER_MESSAGE)) {
String userMessageString = arguments.getString(USER_MESSAGE);
try {
userMessageString = new JSONArray(userMessageString).getString(0);
} catch (JSONException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= VERSION_CODES.N) {
userMessage.setText(Html.fromHtml(userMessageString, Html.FROM_HTML_MODE_LEGACY));
} else {
userMessage.setText(Html.fromHtml(userMessageString));
}
Linkify.addLinks(userMessage, Linkify.ALL);
userMessage.setMovementMethod(LinkMovementMethod.getInstance());
userMessage.setVisibility(VISIBLE);
} else if (userMessage.getVisibility() != GONE) {
userMessage.setVisibility(GONE);
}
if (!usernameField.getText().toString().isEmpty() && passwordField.isFocusable())
passwordField.requestFocus();
mConfigState.setAction(SHOWING_FORM);
hideProgressBar();
}
private void userActivateCodec(final Map<String, Object> codec, boolean activate) {
String codecName = (String) codec.get(CODEC_ID);
final short newPrio = activate ? (short) 1 : (short) 0;
boolean isDisabled = ((Short) codec.get(CODEC_PRIORITY) == 0);
if(isDisabled == !activate) {
// Nothing to do, this codec is already enabled
return;
}
if(NON_FREE_CODECS.containsKey(codecName) && activate) {
final TextView message = new TextView(getActivity());
final SpannableString s = new SpannableString(getString(R.string.this_codec_is_not_free) + NON_FREE_CODECS.get(codecName));
Linkify.addLinks(s, Linkify.WEB_URLS);
message.setText(s);
message.setMovementMethod(LinkMovementMethod.getInstance());
message.setPadding(10, 10, 10, 10);
//Alert user that we will disable for all incoming calls as he want to quit
new AlertDialog.Builder(getActivity())
.setTitle(R.string.warning)
.setView(message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
setCodecActivated(codec, newPrio);
}
})
.setNegativeButton(R.string.cancel, null)
.show();
}else {
setCodecActivated(codec, newPrio);
}
}
public RecyclerViewHolder linkify(int viewId) {
TextView view = findViewById(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public ViewHolder linkify(int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public BaseRecyclerHolder linkify(int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public ViewHolder linkify(int viewId)
{
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public ViewHolder linkify(int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
@Override
public MartianViewHolder linkify(int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public XViewHolder linkify(@IdRes int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public ViewHolder linkify(int viewId)
{
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
/**
* Add links into a TextView.
*
* @param viewId The id of the TextView to linkify.
* @return The BaseViewHolder for chaining.
*/
public BaseViewHolder linkify(int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}