如在Preference里显示链接,在点击后跳转到对应网站。
目录
3. 外部加载UrlPreference, 并设置文本和Url链接
1. 自定义UrlPreference
1.1 继承自Preference, 并提供构造函数
public class UrlPreference extends Preference {
private Context mContext;
public UrlPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//主要构造函数
public UrlPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext = context;
}
public UrlPreference(Context context) {
super(context);
}
}
1.2 onCreateView 时添加自定义布局
@Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.url_preference, parent, false);
return layout;
}
其中, url_preference 就是自定义布局, 其实就是一个TextView
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/url_preference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="38dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical">
</TextView>
1.3 onBindView 时设置带下划线的字体
private TextView mTextView;
@Override
protected void onBindView(View view) {
super.onBindView(view);
mTextView = view.findViewById(R.id.url_preference);
if (mText == null) {
return;
}
SpannableString sp = new SpannableString(mText);
sp.setSpan(new UnderlineSpan(), 0, mText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(sp);
}
其中, SpannableString 实现了 接口 CharSequence, 因此它可以使用TextView 设置文本的方法TextView.setText(CharSequence text)
UnderlineSpan 为下划线样式, 这里的Flag表示:start和end是开区间还是闭区间。
//SpannableString.java
public void setSpan(Object what, int start, int end, int flags) {
super.setSpan(what, start, end, flags);
}
1.4. 提供接口设置文本 和Url
private TextView mTextView;
private String mText;
public void setText(String text) {
mText = text;
}
public void setUrl(String url) {
mUrl = url;
}
1.5. 响应点击事件
@Override
protected void onClick() {
if (mUrl == null) {
super.onClick();
} else {
startUrl(mUrl);
}
}
private void startUrl(String url) {
try {
Uri uri = Uri.parse(url);
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage("com.android.browser");
if (intent != null) {
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(uri);
//Toast.makeText(mContext, "com.android.browser???", Toast.LENGTH_SHORT).show();
} else {
intent = new Intent(Intent.ACTION_VIEW, uri);
//Toast.makeText(mContext, " only action and uri", Toast.LENGTH_SHORT).show();
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
通过url 生成Uri, 然后通过intent 启动Activity
2. XML 文件使用该UrlPreference
<com.example.test.UrlPreference
android:key="@string/pref_test_url_setting" />
3. 外部加载UrlPreference, 并设置文本和Url链接
UrlPreference urlPreference = (UrlPreference) findPreference(getResources().getString(R.string.pref_test_url_setting));
//代码里设置,相对于在UrlPreference设置更灵活
urlPreference.setText("点击获取XXX");
urlPreference.setUrl("http:xxxxxx");
4. 更加简洁的方式(含官方文档)
另外提供更加简单的方式, 在XML 定义intent
<Preference android:title="@string/prefs_web_page" >
<intent android:action="android.intent.action.VIEW"
android:data="http://www.example.com" />
</Preference
或java中使用
this.setIntent(new Intent().setAction(Intent.ACTION_VIEW).setData(
Uri.parse("https://rogerkeays.com")));
其中this 表示是preference
Intent 使用中的浏览网页参考(简洁且规范): https://developer.android.com/training/basics/intents/sending#java