Android · 2015年3月31日 0

Android常用方法收集

/**

* 获取当前应用程序的版本号

*/

private String getVersion() {

String st = getResources().getString(R.string.Version_number_is_wrong);

PackageManager pm = getPackageManager();

try {

PackageInfo packinfo = pm.getPackageInfo(getPackageName(), 0);

String version = packinfo.versionName;

return version;

} catch (NameNotFoundException e) {

e.printStackTrace();

return st;

}

}

设置字符串点击启动指定Activity,部分字体高亮显示

// 创建一个 SpannableString对象

SpannableString sp = new SpannableString(

getString(R.string.activity_frogetpwd_newuserreader_str));

// 设置超链接

sp.setSpan(new MyURLSpan(“”), sp.length() – 4, sp.length(),

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// 设置背景高亮样式一

// sp.setSpan(new BackgroundColorSpan(Color.RED), 17, 19,

// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// 设置高亮样式二

sp.setSpan(new ForegroundColorSpan(Color.BLUE), sp.length() – 4, sp.length(),

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// // 设置斜体

// sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 27,

// 29, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

myTextView.setText(sp);

// 设置TextView可点击

myTextView.setMovementMethod(LinkMovementMethod.getInstance());

/**

* 自定义URLSpan的点击事件

*

* @author jason

*

*/

private class MyURLSpan extends ClickableSpan {

private String mUrl;

MyURLSpan(String url) {

mUrl = url;

}

@Override

public void onClick(View widget) {

Toast.makeText(RegisterActivity.this, mUrl, Toast.LENGTH_LONG)

.show();

widget.setBackgroundColor(Color.parseColor(“#00000000”));

}

}

Share this: