// 没有任何连接
textView.setAutoLinkMask(0);
// 电话数字 网页
Linkify.addLinks(text, Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);
//正则表达式
Pattern pattern = Pattern.compile("\\d{5}([\\-]\\d{4})?");
String scheme = "http://zipinfo.com/cgi-local/zipsrch.exe?zip=";
Linkify.addLinks(text, pattern, scheme);
正则表达式 基本上很有用了,但是有时候需要更灵活写 比如我只要奇数,为了公司需要 还是英文解释吧 大家看得懂吧
// only accepts odd numbers.
MatchFilter oddFilter = new MatchFilter() {
public final boolean acceptMatch(CharSequence s, int start, int end) {
int n = Character.digit(s.charAt(end-1), 10);
return (n & 1) == 1;
}
};
// Match all digits in the pattern but restrict links to only odd numbers using the filter.
Pattern pattern = Pattern.compile("[0-9]+");
Linkify.addLinks(text, pattern, "http://...", oddFilter, null);
import java.util.regex.Pattern;
import android.text.util.Linkify;
import android.text.util.Linkify.TransformFilter;
// A transform filter that simply returns just the text captured by the
// first regular expression group.
TransformFilter mentionFilter = new TransformFilter() {
public final String transformUrl(/blog_article/final Matcher match, String url/index.html) {
return match.group(1);
}
};
// Match @mentions and capture just the username portion of the text.
Pattern pattern = Pattern.compile("@([A-Za-z0-9_-]+)");
String scheme = "http://twitter.com/";
Linkify.addLinks(text, pattern, scheme, null, mentionFilter);
http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/util/Linkify.java
http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/util/Regex.java
在Android中,有一个比较有意思的功能,那就是当你的手机垂直放置时,如果没有设置Activity相关属性,则Activity将会垂直显示,如果你的手机水平放置,则Activity将会水平显示。
而有些时候我们希望不论手机怎样放置,Activity都以某一方式显示,要么水平,要么垂直。
这时候很多人都去设置布局文件,也就是layout目录下的xml文件,将android:orientation设置为vertical或者horizontal,但殊不知这只是设置这个布局的子控件的排列方式,即:如果将android:orientation="vertical"则该布局下的子控件都将垂直排列下去。
正确的设置方法是:在AndroidManifest.xml中配置每个Activity的时候,在相应的Activity中添加一个属性android:screenOrientation,值可以为landscape(水平显示)和portrait(垂直显示)
如:
<activity android:name=".ui.activity.ShowAreaListActivity" android:screenOrientation="landscape"> </activity>
这样就可以保证不管手机如何放置,ShowAreaListActivity总是以水平方向显示
1.Pick File
void pickFile(File aFile) {
Intent theIntent = new Intent(Intent.ACTION_PICK);
theIntent.setData(Uri.fromFile(aFile)); //default file / jump directly to this file/folder
theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
try {
startActivityForResult(theIntent,PICK_FILE_RESULT_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case PICK_FILE_RESULT_CODE: {
if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
String theFilePath = data.getData().getPath();
...
}
break;
}
}
}
Uri.fromFile(aFile) == Uri.parse("file://"+aFile.getPath())
2.
void pickFiles(File aFolder) {
Intent theIntent = new Intent(Intent.ACTION_PICK);
theIntent.setData(Uri.fromFile(aFolder)); //jump directly to this folder
theIntent.putExtra("com.blackmoonit.extra.ALLOW_MULTIPLE",true); //required
theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
try {
startActivityForResult(theIntent,PICK_FILES_RESULT_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case PICK_FILES_RESULT_CODE: {
if (resultCode==RESULT_OK && data!=null && data.getExtras()!=null) {
ArrayList<Uri> theFileUriList = data.getExtras().get(Intent.EXTRA_STREAM);
...
}
break;
}
}
}
void pickFolder(File aFolder) {
Intent theIntent = new Intent(Intent.ACTION_PICK);
theIntent.setData(Uri.parse("folder://"+aFolder.getPath())); //default folder / jump directly to this folder
theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
try {
startActivityForResult(theIntent,PICK_FOLDER_RESULT_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case PICK_FOLDER_RESULT_CODE: {
if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
String theFolderPath = data.getData().getPath();
...
}
break;
}
}
}
2.
void saveToFile(File aFile) {
Uri theUri = Uri.fromFile(aFile).buildUpon().scheme("file.new").build();
Intent theIntent = new Intent(Intent.ACTION_PICK);
theIntent.setData(theUri);
theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
try {
startActivityForResult(theIntent,SAVE_FILE_RESULT_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case SAVE_FILE_RESULT_CODE: {
if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
String theFilePath = data.getData().getPath();
...
}
break;
}
}
}
void saveToFolder(File aFolder) {
Uri theUri = Uri.fromFile(aFolder).buildUpon().scheme("folder.new").build();
Intent theIntent = new Intent(Intent.ACTION_PICK);
theIntent.setData(theUri);
theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
try {
startActivityForResult(theIntent,SAVE_FOLDER_RESULT_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case SAVE_FOLDER_RESULT_CODE: {
if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
String theFolderPath = data.getData().getPath();
...
}
break;
}
}
}
3.
private static final String EXTRA_DIRECTORY = "com.blackmoonit.intent.extra.DIRECTORY";
void createPlaylist(ArrayList<Uri> aFileList) {
String theDefaultFolderPath = "/sdcard/playlists";
Intent theIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
theIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,aFileList);
theIntent.setType("audio/*");
theIntent.putExtra(EXTRA_DIRECTORY,theDefaultFolderPath); //optional
try {
startActivity(theIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
4.
Create Zip file
private static final String EXTRA_DIRECTORY = "com.blackmoonit.intent.extra.DIRECTORY";
void createZipFile(ArrayList<Uri> aFileList) {
Intent theIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
theIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,aFileList);
theIntent.setType("multipart/mixed"); //this should be a good faith attempt at determining the MIME type
String theFolderPath = "/sdcard/some_folder"; //all files in the Zip will be stored relative to this path
theIntent.putExtra(EXTRA_DIRECTORY,theFolderPath);
try {
startActivity(theIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
void createZipFile(File aFile) {
Intent theIntent = new Intent(Intent.ACTION_SEND);
theIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(aFile));
try {
startActivity(theIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
void unpackZipFile(File aFile) {
Intent theIntent = new Intent(Intent.ACTION_VIEW);
theIntent.setDataAndType(Uri.fromFile(aFile),"application/zip");
try {
startActivity(theIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
public static final String MIME_AUTHORITY = "com.blackmoonit.FileBrowser.mimeType";
public static final Uri MIMETYPE_URI = Uri.parse("content://" + MIME_AUTHORITY );
private String getMIMEtypeFromProvider(String aFilename) {
Uri theContentTypeRequest = Uri.withAppendedPath(MIMETYPE_URI,aFilename);
Cursor theTypeResult = managedQuery(theContentTypeRequest, null, null, null, null);
theTypeResult.moveToFirst();
if (!theTypeResult.isNull(0)) {
return theTypeResult.getString(0);
} else {
return null;
}
}