├── .classpath
├── .gitattributes
├── .gitignore
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── README.md
├── bin
├── AndroidManifest.xml
├── ColorPhrase.apk
├── classes.dex
├── classes
│ └── com
│ │ ├── example
│ │ └── colorphrase
│ │ │ ├── BuildConfig.class
│ │ │ ├── MainActivity$1.class
│ │ │ ├── MainActivity.class
│ │ │ ├── R$attr.class
│ │ │ ├── R$dimen.class
│ │ │ ├── R$drawable.class
│ │ │ ├── R$id.class
│ │ │ ├── R$layout.class
│ │ │ ├── R$menu.class
│ │ │ ├── R$string.class
│ │ │ ├── R$style.class
│ │ │ └── R.class
│ │ └── medusa
│ │ └── lib
│ │ ├── ColorPhrase$InnerToken.class
│ │ ├── ColorPhrase$LeftSeparatorToken.class
│ │ ├── ColorPhrase$OuterToken.class
│ │ ├── ColorPhrase$Token.class
│ │ └── ColorPhrase.class
├── dexedLibs
│ └── android-support-v4-280ef5398d81ad2ece5360949c0b77e8.jar
├── res
│ └── crunch
│ │ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ │ └── drawable-xxhdpi
│ │ └── ic_launcher.png
└── resources.ap_
├── gen
└── com
│ └── example
│ └── colorphrase
│ ├── BuildConfig.java
│ └── R.java
├── ic_launcher-web.png
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── layout
│ └── activity_main.xml
├── menu
│ └── main.xml
├── values-sw600dp
│ └── dimens.xml
├── values-sw720dp-land
│ └── dimens.xml
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
└── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── screenshot
└── Screenshot_2015-05-16-18-12-23.jpeg
└── src
└── com
├── example
└── colorphrase
│ └── MainActivity.java
└── medusa
└── lib
└── ColorPhrase.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows image file caches
2 | Thumbs.db
3 | ehthumbs.db
4 |
5 | # Folder config file
6 | Desktop.ini
7 |
8 | # Recycle Bin used on file shares
9 | $RECYCLE.BIN/
10 |
11 | # Windows Installer files
12 | *.cab
13 | *.msi
14 | *.msm
15 | *.msp
16 |
17 | # Windows shortcuts
18 | *.lnk
19 |
20 | # =========================
21 | # Operating System Files
22 | # =========================
23 |
24 | # OSX
25 | # =========================
26 |
27 | .DS_Store
28 | .AppleDouble
29 | .LSOverride
30 |
31 | # Thumbnails
32 | ._*
33 |
34 | # Files that might appear on external disk
35 | .Spotlight-V100
36 | .Trashes
37 |
38 | # Directories potentially created on remote AFP share
39 | .AppleDB
40 | .AppleDesktop
41 | Network Trash Folder
42 | Temporary Items
43 | .apdisk
44 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ColorPhrase
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ColorPhrase
2 | Phrase is an Android string resource color setting library
3 |
4 | ## Usage
5 | ```java
6 | CharSequence formatted = ColorPhrase.from("I'm {Chinese}, I love {China}")
7 | .withSeparator("{}")
8 | .innerColor(0xFFE6454A)
9 | .outerColor(0xFF666666)
10 | .format();
11 | ```
12 | ## Preview
13 | 
14 | ##Download
15 | 1)download the project
16 | 2)find ColorPhrase.java in "\src\com\medusa\lib" and copy it into your project
17 | 3)then you can use it!
18 | 4)ps: you should include the android-support-v4.jar in your porject
19 | [](https://android-arsenal.com/details/1/1915)
20 |
--------------------------------------------------------------------------------
/bin/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/bin/ColorPhrase.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/ColorPhrase.apk
--------------------------------------------------------------------------------
/bin/classes.dex:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes.dex
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/BuildConfig.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/BuildConfig.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/MainActivity$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/MainActivity$1.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/MainActivity.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/MainActivity.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$attr.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$attr.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$dimen.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$dimen.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$drawable.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$drawable.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$id.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$id.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$layout.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$layout.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$menu.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$menu.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$string.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$string.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R$style.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R$style.class
--------------------------------------------------------------------------------
/bin/classes/com/example/colorphrase/R.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/example/colorphrase/R.class
--------------------------------------------------------------------------------
/bin/classes/com/medusa/lib/ColorPhrase$InnerToken.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/medusa/lib/ColorPhrase$InnerToken.class
--------------------------------------------------------------------------------
/bin/classes/com/medusa/lib/ColorPhrase$LeftSeparatorToken.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/medusa/lib/ColorPhrase$LeftSeparatorToken.class
--------------------------------------------------------------------------------
/bin/classes/com/medusa/lib/ColorPhrase$OuterToken.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/medusa/lib/ColorPhrase$OuterToken.class
--------------------------------------------------------------------------------
/bin/classes/com/medusa/lib/ColorPhrase$Token.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/medusa/lib/ColorPhrase$Token.class
--------------------------------------------------------------------------------
/bin/classes/com/medusa/lib/ColorPhrase.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/classes/com/medusa/lib/ColorPhrase.class
--------------------------------------------------------------------------------
/bin/dexedLibs/android-support-v4-280ef5398d81ad2ece5360949c0b77e8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/dexedLibs/android-support-v4-280ef5398d81ad2ece5360949c0b77e8.jar
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/res/crunch/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/res/crunch/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/res/crunch/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/res/crunch/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/resources.ap_:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/bin/resources.ap_
--------------------------------------------------------------------------------
/gen/com/example/colorphrase/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package com.example.colorphrase;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/gen/com/example/colorphrase/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package com.example.colorphrase;
9 |
10 | public final class R {
11 | public static final class attr {
12 | }
13 | public static final class dimen {
14 | /** Default screen margins, per the Android Design guidelines.
15 |
16 | Customize dimensions originally defined in res/values/dimens.xml (such as
17 | screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
18 |
19 | */
20 | public static final int activity_horizontal_margin=0x7f040000;
21 | public static final int activity_vertical_margin=0x7f040001;
22 | }
23 | public static final class drawable {
24 | public static final int ic_launcher=0x7f020000;
25 | }
26 | public static final class id {
27 | public static final int action_settings=0x7f080003;
28 | public static final int button1=0x7f080002;
29 | public static final int editText1=0x7f080000;
30 | public static final int textView1=0x7f080001;
31 | }
32 | public static final class layout {
33 | public static final int activity_main=0x7f030000;
34 | }
35 | public static final class menu {
36 | public static final int main=0x7f070000;
37 | }
38 | public static final class string {
39 | public static final int action_settings=0x7f050001;
40 | public static final int app_name=0x7f050000;
41 | public static final int hello_world=0x7f050002;
42 | public static final int pattern=0x7f050003;
43 | }
44 | public static final class style {
45 | /**
46 | Base application theme, dependent on API level. This theme is replaced
47 | by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
48 |
49 |
50 | Theme customizations available in newer API levels can go in
51 | res/values-vXX/styles.xml, while customizations related to
52 | backward-compatibility can go here.
53 |
54 |
55 | Base application theme for API 11+. This theme completely replaces
56 | AppBaseTheme from res/values/styles.xml on API 11+ devices.
57 |
58 | API 11 theme customizations can go here.
59 |
60 | Base application theme for API 14+. This theme completely replaces
61 | AppBaseTheme from BOTH res/values/styles.xml and
62 | res/values-v11/styles.xml on API 14+ devices.
63 |
64 | API 14 theme customizations can go here.
65 | */
66 | public static final int AppBaseTheme=0x7f060000;
67 | /** Application theme.
68 | All customizations that are NOT specific to a particular API-level can go here.
69 | */
70 | public static final int AppTheme=0x7f060001;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
12 |
13 |
20 |
21 |
22 |
23 |
24 |
30 |
31 |
37 |
38 |
--------------------------------------------------------------------------------
/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | 128dp
8 |
9 |
10 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ColorPhrase
5 | Settings
6 | Hello world!
7 | 紫陌红尘,蓦然{回首}。多少的{春花秋月};多少的{逝水沉香};多少的{海誓山盟},如沿途的风景{花开花谢}。人世间的情缘{触痛了}多少无言的{感慨},{情深缘浅}的风吹散了多少{相聚离散}。花开是有情,花落是无意。来者是{萍水相逢},去者是{江湖相忘}。缘起时,我在{人群}中看到你。缘灭时,你{消失}在人群中。
8 |
9 |
10 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/screenshot/Screenshot_2015-05-16-18-12-23.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/THEONE10211024/ColorPhrase/e6ae11893279fc99165d33295c45d6297d77d476/screenshot/Screenshot_2015-05-16-18-12-23.jpeg
--------------------------------------------------------------------------------
/src/com/example/colorphrase/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.colorphrase;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.Menu;
6 | import android.view.View;
7 | import android.view.View.OnClickListener;
8 | import android.widget.Button;
9 | import android.widget.EditText;
10 | import android.widget.TextView;
11 |
12 | import com.medusa.lib.ColorPhrase;
13 |
14 | public class MainActivity extends Activity {
15 | private EditText editText;
16 | private TextView textView;
17 | private Button button;
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 | editText = (EditText) findViewById(R.id.editText1);
23 | textView = (TextView) findViewById(R.id.textView1);
24 | button = (Button) findViewById(R.id.button1);
25 | button.setOnClickListener(new OnClickListener() {
26 |
27 | @Override
28 | public void onClick(View v) {
29 | String pattern = editText.getText().toString();
30 | CharSequence chars = ColorPhrase.from(pattern).withSeparator("{}").innerColor(0xFFE6454A).outerColor(0xFF666666).format();
31 | textView.setText(chars);
32 | }
33 | });
34 | }
35 |
36 | @Override
37 | public boolean onCreateOptionsMenu(Menu menu) {
38 | // Inflate the menu; this adds items to the action bar if it is present.
39 | getMenuInflater().inflate(R.menu.main, menu);
40 | return true;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/com/medusa/lib/ColorPhrase.java:
--------------------------------------------------------------------------------
1 | package com.medusa.lib;
2 |
3 | import java.util.Stack;
4 |
5 | import android.annotation.TargetApi;
6 | import android.app.Fragment;
7 | import android.content.Context;
8 | import android.content.res.Resources;
9 | import android.os.Build;
10 | import android.text.Spannable;
11 | import android.text.SpannableStringBuilder;
12 | import android.text.TextUtils;
13 | import android.text.style.ForegroundColorSpan;
14 | import android.view.View;
15 | /**
16 | * A fluent API for formatting Strings. Canonical usage:
17 | *
18 | * CharSequence chars = ColorPhrase.from("I'm,I love ").
19 | * withSeparator("<>").
20 | * innerColor(0xFFE6454A).
21 | * outerColor(0xFF666666).
22 | * format();
23 | *
24 | *
25 | * - Surround keys with curly braces; use two {{ to escape.
26 | * - Fails fast on any mismatched keys.
27 | *
28 | * The constructor parses the original pattern into a doubly-linked list of {@link Token}s.
29 | * These tokens do not modify the original pattern, thus preserving any spans.
30 | *
31 | * The {@link #format()} method iterates over the tokens, replacing and coloring the text as it iterates. The
32 | * doubly-linked list allows each token to ask its predecessor for the expanded length.
33 | */
34 | public class ColorPhrase {
35 | /** The unmodified original pattern. */
36 | private final CharSequence pattern;
37 | /** Cached result after replacing all keys with corresponding values. */
38 | private CharSequence formatted;
39 | /**
40 | * The constructor parses the original pattern into this doubly-linked list
41 | * of tokens.
42 | */
43 | private Token head;
44 |
45 | /** When parsing, this is the current character. */
46 | private char curChar;
47 | private String separator;// default "{}"
48 | private int curCharIndex;
49 | private int outerColor;// color that outside the separators,default 0xFF666666
50 | private int innerColor;// color that between the separators,default 0xFFA6454A
51 | /** Indicates parsing is complete. */
52 | private static final int EOF = 0;
53 |
54 | /**
55 | * Entry point into this API.
56 | *
57 | * @throws IllegalArgumentException
58 | * if pattern contains any syntax errors.
59 | */
60 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
61 | public static ColorPhrase from(Fragment f, int patternResourceId) {
62 | return from(f.getResources(), patternResourceId);
63 | }
64 |
65 | /**
66 | * Entry point into this API.
67 | *
68 | * @throws IllegalArgumentException
69 | * if pattern contains any syntax errors.
70 | */
71 | public static ColorPhrase from(View v, int patternResourceId) {
72 | return from(v.getResources(), patternResourceId);
73 | }
74 |
75 | /**
76 | * Entry point into this API.
77 | *
78 | * @throws IllegalArgumentException
79 | * if pattern contains any syntax errors.
80 | */
81 | public static ColorPhrase from(Context c, int patternResourceId) {
82 | return from(c.getResources(), patternResourceId);
83 | }
84 |
85 | /**
86 | * Entry point into this API.
87 | *
88 | * @throws IllegalArgumentException
89 | * if pattern contains any syntax errors.
90 | */
91 | public static ColorPhrase from(Resources r, int patternResourceId) {
92 | return from(r.getText(patternResourceId));
93 | }
94 |
95 | /**
96 | * Entry point into this API; pattern must be non-null.
97 | *
98 | * @throws IllegalArgumentException
99 | * if pattern contains any syntax errors.
100 | */
101 | public static ColorPhrase from(CharSequence pattern) {
102 | return new ColorPhrase(pattern);
103 | }
104 |
105 | private ColorPhrase(CharSequence pattern) {
106 | curChar = (pattern.length() > 0) ? pattern.charAt(0) : EOF;
107 |
108 | this.pattern = pattern;
109 | // Invalidate the cached formatted text.
110 | formatted = null;
111 | separator = "{}";// initialize the default separator
112 | outerColor = 0xFF666666;//initialize the default value
113 | innerColor =0xFFE6454A;//initialize the default value
114 | }
115 |
116 | /**
117 | * set the separator of the target,called after from() method.
118 | *
119 | * @param _separator
120 | * @return
121 | */
122 | public ColorPhrase withSeparator(String _separator) {
123 | if (TextUtils.isEmpty(_separator)) {
124 | throw new IllegalArgumentException("separator must not be empty!");
125 | }
126 | if (_separator.length() > 2) {
127 | throw new IllegalArgumentException("separator‘s length must not be more than 3 charactors!");
128 | }
129 | this.separator = _separator;
130 | return this;
131 | }
132 |
133 | /**
134 | * init the outerColor
135 | *
136 | * @param _outerColor
137 | * @return
138 | */
139 | public ColorPhrase outerColor(int _outerColor) {
140 | this.outerColor = _outerColor;
141 | return this;
142 | }
143 |
144 | /**
145 | * init the innerColor
146 | *
147 | * @param _innerColor
148 | * @return
149 | */
150 | public ColorPhrase innerColor(int _innerColor) {
151 | this.innerColor = _innerColor;
152 | return this;
153 | }
154 |
155 | /**
156 | * cut the pattern with the separators and linked them with double link
157 | * structure;
158 | */
159 | private void createDoubleLinkWithToken() {
160 | // A hand-coded lexer based on the idioms in
161 | // "Building Recognizers By Hand".
162 | // http://www.antlr2.org/book/byhand.pdf.
163 | Token prev = null;
164 | Token next;
165 | while ((next = token(prev)) != null) {
166 | // Creates a doubly-linked list of tokens starting with head.
167 | if (head == null)
168 | head = next;
169 | prev = next;
170 | }
171 | }
172 |
173 | /**
174 | * Returns the next token from the input pattern, or null when finished
175 | * parsing.
176 | */
177 | private Token token(Token prev) {
178 | if (curChar == EOF) {
179 | return null;
180 | }
181 | if (curChar == getLeftSeparator()) {
182 | char nextChar = lookahead();
183 | if (nextChar == getLeftSeparator()) {
184 | return leftSeparator(prev);
185 | } else {
186 | return inner(prev);
187 | }
188 | }
189 | return outer(prev);
190 | }
191 |
192 | private char getLeftSeparator() {
193 | return separator.charAt(0);
194 | }
195 |
196 | private char getRightSeparator() {
197 | if (separator.length() == 2) {
198 | return separator.charAt(1);
199 | }
200 | return separator.charAt(0);
201 | }
202 |
203 | /**
204 | * Returns the text after replacing all keys with values.
205 | *
206 | * @throws IllegalArgumentException
207 | * if any keys are not replaced.
208 | */
209 | public CharSequence format() {
210 | if (formatted == null) {
211 | if (!checkPattern()) {
212 | throw new IllegalStateException("the separators don't match in the pattern!");
213 | }
214 | createDoubleLinkWithToken();
215 | // Copy the original pattern to preserve all spans, such as bold,
216 | // italic, etc.
217 | SpannableStringBuilder sb = new SpannableStringBuilder(pattern);
218 | for (Token t = head; t != null; t = t.next) {
219 | t.expand(sb);
220 | }
221 |
222 | formatted = sb;
223 | }
224 | return formatted;
225 | }
226 |
227 | /**
228 | * check if the pattern has legal separators
229 | *
230 | * @return
231 | */
232 | private boolean checkPattern() {
233 | if (pattern == null) {
234 | return false;
235 | }
236 | char leftSeparator = getLeftSeparator();
237 | char rightSeparator = getRightSeparator();
238 | Stack separatorStack = new Stack();
239 | for (int i = 0; i < pattern.length(); i++) {
240 | char cur = pattern.charAt(i);
241 | if (cur == leftSeparator) {
242 | separatorStack.push(cur);
243 | } else if (cur == rightSeparator) {
244 | if (!separatorStack.isEmpty() && (separatorStack.pop() == leftSeparator)) {
245 | continue;
246 | } else {
247 | return false;
248 | }
249 | }
250 | }
251 | return separatorStack.isEmpty();
252 | }
253 |
254 | private InnerToken inner(Token prev) {
255 |
256 | // Store keys as normal Strings; we don't want keys to contain spans.
257 | StringBuilder sb = new StringBuilder();
258 |
259 | // Consume the left separator.
260 | consume();
261 | char rightSeparator = getRightSeparator();
262 | while (curChar != rightSeparator && curChar != EOF) {
263 | sb.append(curChar);
264 | consume();
265 | }
266 |
267 | if (curChar == EOF) {
268 | throw new IllegalArgumentException("Missing closing separator");
269 | }
270 | //consume the right separator.
271 | consume();
272 |
273 | if (sb.length() == 0) {
274 | throw new IllegalStateException("Disallow empty content between separators,for example {}");
275 | }
276 |
277 | String key = sb.toString();
278 | return new InnerToken(prev, key, innerColor);
279 | }
280 |
281 | /** Consumes and returns a token for a sequence of text. */
282 | private OuterToken outer(Token prev) {
283 | int startIndex = curCharIndex;
284 |
285 | while (curChar != getLeftSeparator() && curChar != EOF) {
286 | consume();
287 | }
288 | return new OuterToken(prev, curCharIndex - startIndex, outerColor);
289 | }
290 |
291 | /**
292 | * Consumes and returns a token representing two consecutive curly brackets.
293 | */
294 | private LeftSeparatorToken leftSeparator(Token prev) {
295 | consume();
296 | consume();
297 | return new LeftSeparatorToken(prev, getLeftSeparator());
298 | }
299 |
300 | /** Returns the next character in the input pattern without advancing. */
301 | private char lookahead() {
302 | return curCharIndex < pattern.length() - 1 ? pattern.charAt(curCharIndex + 1) : EOF;
303 | }
304 |
305 | /**
306 | * Advances the current character position without any error checking.
307 | * Consuming beyond the end of the string can only happen if this parser
308 | * contains a bug.
309 | */
310 | private void consume() {
311 | curCharIndex++;
312 | curChar = (curCharIndex == pattern.length()) ? EOF : pattern.charAt(curCharIndex);
313 | }
314 |
315 | /**
316 | * Returns the raw pattern without expanding keys; only useful for
317 | * debugging. Does not pass through to {@link #format()} because doing so
318 | * would drop all spans.
319 | */
320 | @Override
321 | public String toString() {
322 | return pattern.toString();
323 | }
324 |
325 | private abstract static class Token {
326 | private final Token prev;
327 | private Token next;
328 |
329 | protected Token(Token prev) {
330 | this.prev = prev;
331 | if (prev != null)
332 | prev.next = this;
333 | }
334 |
335 | /** Replace text in {@code target} with this token's associated value. */
336 | abstract void expand(SpannableStringBuilder target);
337 |
338 | /** Returns the number of characters after expansion. */
339 | abstract int getFormattedLength();
340 |
341 | /** Returns the character index after expansion. */
342 | final int getFormattedStart() {
343 | if (prev == null) {
344 | // The first token.
345 | return 0;
346 | } else {
347 | // Recursively ask the predecessor node for the starting index.
348 | return prev.getFormattedStart() + prev.getFormattedLength();
349 | }
350 | }
351 | }
352 |
353 | /** Ordinary text between tokens. */
354 | private static class OuterToken extends Token {
355 | private final int textLength;
356 | private int color;
357 |
358 | OuterToken(Token prev, int textLength, int _color) {
359 | super(prev);
360 | this.textLength = textLength;
361 | this.color = _color;
362 | }
363 |
364 | @Override
365 | void expand(SpannableStringBuilder target) {
366 |
367 | int startPoint = getFormattedStart();
368 | int endPoint = startPoint + textLength;
369 | target.setSpan(new ForegroundColorSpan(color), startPoint, endPoint, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
370 | }
371 |
372 | @Override
373 | int getFormattedLength() {
374 | return textLength;
375 | }
376 | }
377 |
378 | /** A sequence of two curly brackets. */
379 | private static class LeftSeparatorToken extends Token {
380 | private char leftSeparetor;
381 |
382 | LeftSeparatorToken(Token prev, char _leftSeparator) {
383 | super(prev);
384 | leftSeparetor = _leftSeparator;
385 | }
386 |
387 | @Override
388 | void expand(SpannableStringBuilder target) {
389 | int start = getFormattedStart();
390 | target.replace(start, start + 2, String.valueOf(leftSeparetor));
391 | }
392 |
393 | @Override
394 | int getFormattedLength() {
395 | // for example,,Replace "{{" with "{".
396 | return 1;
397 | }
398 | }
399 |
400 | private static class InnerToken extends Token {
401 | /** The InnerText without separators,like '{' and '}'. */
402 | private final String innerText;
403 |
404 | private int color;
405 |
406 | InnerToken(Token prev, String _inner, int _color) {
407 | super(prev);
408 | this.innerText = _inner;
409 | color = _color;
410 | }
411 |
412 | @Override
413 | void expand(SpannableStringBuilder target) {
414 |
415 | int replaceFrom = getFormattedStart();
416 | // Add 2 to account for the separators.
417 | int replaceTo = replaceFrom + innerText.length() + 2;
418 | target.replace(replaceFrom, replaceTo, innerText);
419 | target.setSpan(new ForegroundColorSpan(color), replaceFrom, replaceTo - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
420 | }
421 |
422 | @Override
423 | int getFormattedLength() {
424 | // Note that value is only present after expand. Don't error check
425 | // because this is all
426 | // private code.
427 | return innerText.length();
428 | }
429 | }
430 | }
431 |
--------------------------------------------------------------------------------