├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── src │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── wy │ │ │ └── flowlayoutusedemo │ │ │ └── ExampleInstrumentedTest.java │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── wy │ │ │ │ └── flowlayoutusedemo │ │ │ │ ├── Button1Activity.java │ │ │ │ ├── Button2Activity.java │ │ │ │ ├── MainActivity.java │ │ │ │ └── util │ │ │ │ ├── SharedPreferencesUtil.java │ │ │ │ └── ToastUtil.java │ │ └── res │ │ │ ├── color │ │ │ └── select_flowlayout_textview_color.xml │ │ │ ├── drawable │ │ │ ├── logo.png │ │ │ ├── select_flowlayout_textview_bg.xml │ │ │ ├── shape_circle_corner_blue.xml │ │ │ ├── shape_circle_corner_blue3.xml │ │ │ ├── shape_circle_corner_blue5.xml │ │ │ ├── shape_circle_corner_blue_checked.xml │ │ │ └── shape_circle_corner_white.xml │ │ │ ├── layout │ │ │ ├── activity_button1.xml │ │ │ ├── activity_button2.xml │ │ │ ├── activity_main.xml │ │ │ ├── flowlayout_textview.xml │ │ │ ├── flowlayout_textview_no_selected.xml │ │ │ └── flowlayout_textview_selected.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ ├── titlebar_back.png │ │ │ ├── titlebar_bg.png │ │ │ └── titlebar_search.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ ├── titlebar_back.png │ │ │ ├── titlebar_bg.png │ │ │ └── titlebar_search.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ ├── titlebar_back.png │ │ │ ├── titlebar_bg.png │ │ │ └── titlebar_search.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ └── test │ │ └── java │ │ └── com │ │ └── wy │ │ └── flowlayoutusedemo │ │ └── ExampleUnitTest.java ├── 产品标签.gif └── 本地历史记录.gif ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FlowLayouUseDemo 2 | Android 本地历史记录、及产品标签(支持单选、多选)实现(使用鸿洋大神的FlowLayout开源库),主要功能:FlowLayout的使用、从SP中读取历史记录、将历史记录写入到SP中、历史记录最大数量限制、历史记录不可重复、最新查询的在最前边、清楚历史记录;FlowLayout预先设置选中、设置最大选中数、设置标签点击和选中监听、获取选中的标签、通过selecter完成标签选择的切换等。
项目博客地址:https://blog.csdn.net/qq941263013/article/details/81223574
个人博客地址:https://blog.csdn.net/qq941263013
本地历史记录的GIF效果图:


产品标签(单选、多选)GIF效果图:


本地历史记录功能的简单说明(详细描述见博客、源码):
    1.页面布局:主要是在布局文件中使用TagFlowLayout实现流失布局,并通过自定义属性(zhy:max_select="0")规定标签的最大可选数。
    2.定义从SP中读取历史记录方法:从SP中获取存储的字符串,并通过wy标识将字符串分割为字符串数组,然后添加到List中,最后将list返回。
    3.定义将历史记录写入到SP中方法:先判空,不为空则获取已有的历史记录,再将新的历史记录添加list中,最后把list转换成字符串(添加wy标识)存入到SP中。需要注意的是:1.历史记录最大数量限制,2.历史记录不可重复,3.最新查询的在最前边。
    4.初始化历史记录:先通过本地历史记录判断页面的显示与隐藏,在为FlowLayout填充数据、设置点击事件监听。
    5.点击搜索按钮:首先获取输入框中的内容,然后添加到本地历史记录中,最后再调用initHistory()刷新页面数据。具体的搜索逻辑就不写了,不是重点。
    6.清除历史记录:将本地历史记录置为空字符串,再调用initHistory()刷新页面数据即可,大功告成。 3 | 4 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.wy.flowlayoutusedemo" 8 | minSdkVersion 16 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | testCompile 'junit:junit:4.12' 29 | compile 'com.hyman:flowlayout-lib:1.1.2' 30 | compile 'com.jakewharton:butterknife:8.8.1' 31 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' 32 | } 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\ZQZY-102\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/wy/flowlayoutusedemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.wy.flowlayoutusedemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.wy.flowlayoutusedemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 20 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/wy/flowlayoutusedemo/Button1Activity.java: -------------------------------------------------------------------------------- 1 | package com.wy.flowlayoutusedemo; 2 | 3 | import android.content.Context; 4 | import android.graphics.Paint; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.text.TextUtils; 9 | import android.view.View; 10 | import android.widget.EditText; 11 | import android.widget.ImageView; 12 | import android.widget.LinearLayout; 13 | import android.widget.RelativeLayout; 14 | import android.widget.TextView; 15 | import android.widget.Toast; 16 | 17 | import com.wy.flowlayoutusedemo.util.SharedPreferencesUtil; 18 | import com.wy.flowlayoutusedemo.util.ToastUtil; 19 | import com.zhy.view.flowlayout.FlowLayout; 20 | import com.zhy.view.flowlayout.TagAdapter; 21 | import com.zhy.view.flowlayout.TagFlowLayout; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import butterknife.OnClick; 29 | 30 | /** 31 | * Created by WY on 2018/7/25. 32 | * 本地历史记录 33 | */ 34 | public class Button1Activity extends AppCompatActivity { 35 | 36 | @BindView(R.id.tv_titlebar_center) 37 | TextView tvTitlebarCenter; 38 | @BindView(R.id.iv_titlebar_left) 39 | ImageView ivTitlebarLeft; 40 | @BindView(R.id.rl) 41 | RelativeLayout rl; 42 | @BindView(R.id.iv_search) 43 | ImageView ivSearch; 44 | @BindView(R.id.et_search) 45 | EditText etSearch; 46 | @BindView(R.id.rl_search) 47 | RelativeLayout rlSearch; 48 | @BindView(R.id.flowlayout_history) 49 | TagFlowLayout flowlayoutHistory; 50 | @BindView(R.id.tv_clear) 51 | TextView tvClear; 52 | @BindView(R.id.ll_history) 53 | LinearLayout llHistory; 54 | private Context context; 55 | 56 | @Override 57 | protected void onCreate(@Nullable Bundle savedInstanceState) { 58 | super.onCreate(savedInstanceState); 59 | setContentView(R.layout.activity_button1); 60 | ButterKnife.bind(this); 61 | context = this; 62 | 63 | //测试数据 64 | String testData = "活着wy1234567890wyISBNwy中华人民共和国wy唉wysfsfsfswy1wy搜索wy尴尬啦wy活着啊"; 65 | SharedPreferencesUtil.putString(context, "search_history", testData); 66 | 67 | initUI(); 68 | initHistory(); 69 | } 70 | 71 | /** 72 | * 初始化UI 73 | */ 74 | private void initUI() { 75 | //下划线 76 | tvClear.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); 77 | } 78 | 79 | @OnClick({R.id.iv_titlebar_left, R.id.iv_search, R.id.tv_clear}) 80 | public void onViewClicked(View view) { 81 | switch (view.getId()) { 82 | case R.id.iv_titlebar_left://返回 83 | finish(); 84 | break; 85 | case R.id.iv_search://搜索 86 | initSearch(); 87 | break; 88 | case R.id.tv_clear://清除历史记录 89 | SharedPreferencesUtil.putString(context, "search_history", ""); 90 | initHistory(); 91 | break; 92 | } 93 | } 94 | 95 | /** 96 | * 初始化历史记录 97 | */ 98 | private void initHistory() { 99 | final List readHistory = readHistory(); 100 | if (readHistory != null && readHistory.size() > 0) { 101 | llHistory.setVisibility(View.VISIBLE); 102 | } else { 103 | llHistory.setVisibility(View.GONE); 104 | } 105 | 106 | //为FlowLayout填充数据 107 | flowlayoutHistory.setAdapter(new TagAdapter(readHistory) { 108 | @Override 109 | public View getView(FlowLayout parent, int position, Object o) { 110 | 111 | TextView view = (TextView) View.inflate(context, R.layout.flowlayout_textview, null); 112 | view.setText(readHistory.get(position)); 113 | return view; 114 | } 115 | }); 116 | 117 | //为FlowLayout的标签设置监听事件 118 | flowlayoutHistory.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() { 119 | @Override 120 | public boolean onTagClick(View view, int position, FlowLayout parent) { 121 | ToastUtil.makeText(context, readHistory.get(position)); 122 | etSearch.setText(readHistory.get(position)); 123 | etSearch.setSelection(readHistory.get(position).length()); 124 | return true; 125 | } 126 | }); 127 | } 128 | 129 | /** 130 | * 从SP中读取历史记录 131 | */ 132 | private List readHistory() { 133 | List readHistoryList = new ArrayList<>(); 134 | String search_history = SharedPreferencesUtil.getString(context, "search_history", null); 135 | 136 | //将String转为List 137 | if (!TextUtils.isEmpty(search_history)) { 138 | String[] strs = search_history.split("wy"); 139 | for (int i = 0; i < strs.length; i++) { 140 | readHistoryList.add(i, strs[i]); 141 | } 142 | } 143 | 144 | return readHistoryList; 145 | } 146 | 147 | /** 148 | * 将历史记录写入到SP中 149 | */ 150 | private void writeHistory(String write) { 151 | if (TextUtils.isEmpty(write)) { 152 | return; 153 | } 154 | 155 | String writeHistory = ""; 156 | //获取历史记录 157 | List readHistoryList = readHistory(); 158 | 159 | //如果不重复,则添加为第一个历史记录; 160 | //如果重复,则删除已有,再添加为第一个历史记录; 161 | for (int i = 0; i < readHistoryList.size(); i++) { 162 | boolean hasWrite = readHistoryList.get(i).equals(write); 163 | if (hasWrite) { 164 | readHistoryList.remove(i); 165 | break; 166 | } 167 | } 168 | readHistoryList.add(0, write); 169 | 170 | //历史记录最多为10个 171 | if (readHistoryList.size() > 10) { 172 | readHistoryList = readHistoryList.subList(0, 10); 173 | } 174 | 175 | //将ArrayList转为String 176 | for (int i = 0; i < readHistoryList.size(); i++) { 177 | writeHistory += readHistoryList.get(i) + "wy"; 178 | } 179 | SharedPreferencesUtil.putString(context, "search_history", writeHistory); 180 | } 181 | 182 | /** 183 | * 初始化搜索 184 | */ 185 | private void initSearch() { 186 | String inputSearch = etSearch.getText().toString().trim(); 187 | if (TextUtils.isEmpty(inputSearch)) { 188 | ToastUtil.makeText(context, "请输入要查询的条码"); 189 | return; 190 | } 191 | writeHistory(inputSearch); 192 | initHistory(); 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /app/src/main/java/com/wy/flowlayoutusedemo/Button2Activity.java: -------------------------------------------------------------------------------- 1 | package com.wy.flowlayoutusedemo; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | import android.widget.TextView; 9 | 10 | import com.wy.flowlayoutusedemo.util.ToastUtil; 11 | import com.zhy.view.flowlayout.FlowLayout; 12 | import com.zhy.view.flowlayout.TagAdapter; 13 | import com.zhy.view.flowlayout.TagFlowLayout; 14 | 15 | import java.util.Iterator; 16 | import java.util.Set; 17 | 18 | import butterknife.BindView; 19 | import butterknife.ButterKnife; 20 | 21 | /** 22 | * Created by WY on 2018/7/26. 23 | * 标签(单选、多选) 24 | * ##特色 25 | * 以setAdapter形式注入数据 26 | * 直接设置selector为background即可完成标签选则的切换,类似CheckBox 27 | * 支持控制选择的Tag数量,比如:单选、多选 28 | * 支持setOnTagClickListener,当点击某个Tag回调 29 | * 支持setOnSelectListener,当选择某个Tag后回调 30 | * 支持adapter.notifyDataChanged 31 | * Activity重建(或者旋转)后,选择的状态自动保存 32 | */ 33 | public class Button2Activity extends AppCompatActivity { 34 | 35 | @BindView(R.id.flowlayout) 36 | TagFlowLayout flowlayout; 37 | @BindView(R.id.tv_selected) 38 | TextView tvSelected; 39 | @BindView(R.id.flowlayout_selected) 40 | TagFlowLayout flowlayoutSelected; 41 | private Context context; 42 | private String[] mVals = new String[] 43 | {"Hello", "Android", "Weclome Hi ", "Button", "TextView", "Hello", 44 | "Android", "Weclome", "Button ImageView", "TextView", "Helloworld", 45 | "Android", "Weclome Hello", "Button Text", "TextView"}; 46 | private int maxSelected = 5; 47 | private Set selectedList; 48 | private String[] mValsSelected; 49 | 50 | @Override 51 | protected void onCreate(@Nullable Bundle savedInstanceState) { 52 | super.onCreate(savedInstanceState); 53 | setContentView(R.layout.activity_button2); 54 | ButterKnife.bind(this); 55 | context = this; 56 | 57 | initFlowLayout(); 58 | initFlowLayoutSelected(); 59 | } 60 | 61 | /** 62 | * 初始化所有标签的FlowLayout 63 | */ 64 | private void initFlowLayout() { 65 | TagAdapter tagAdapter = new TagAdapter(mVals) { 66 | @Override 67 | public View getView(FlowLayout parent, int position, Object o) { 68 | 69 | TextView view = (TextView) View.inflate(context, R.layout.flowlayout_textview_selected, null); 70 | view.setText(mVals[position]); 71 | return view; 72 | } 73 | }; 74 | //预先设置选中 75 | tagAdapter.setSelectedList(0, 1); 76 | flowlayout.setAdapter(tagAdapter); 77 | 78 | //设置最大选中数 79 | flowlayout.setMaxSelectCount(maxSelected); 80 | 81 | //为FlowLayout的标签设置监听事件 82 | flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() { 83 | @Override 84 | public boolean onTagClick(View view, int position, FlowLayout parent) { 85 | if (selectedList.size() >= maxSelected) { 86 | ToastUtil.makeText(context, "已达最大选中数" + maxSelected); 87 | } else { 88 | ToastUtil.makeText(context, mVals[position]); 89 | } 90 | return true; 91 | } 92 | }); 93 | 94 | //为FlowLayout的标签设置选中监听事件 95 | flowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() { 96 | @Override 97 | public void onSelected(Set selectPosSet) { 98 | initFlowLayoutSelected(); 99 | } 100 | }); 101 | } 102 | 103 | /** 104 | * 初始化选中标签的FlowLayout 105 | */ 106 | private void initFlowLayoutSelected() { 107 | int i = 0; 108 | //获得所有选中的position集合,例如[1,2,3,4] 109 | selectedList = flowlayout.getSelectedList(); 110 | mValsSelected = new String[selectedList.size()]; 111 | Iterator iterator = selectedList.iterator(); 112 | while (iterator.hasNext()) { 113 | mValsSelected[i] = mVals[iterator.next()]; 114 | i++; 115 | } 116 | 117 | tvSelected.setText("最大选中数为:" + maxSelected + "(已选中" + selectedList.size() + ")" + "(position:" + selectedList.toString() + ")"); 118 | flowlayoutSelected.setAdapter(new TagAdapter(mValsSelected) { 119 | @Override 120 | public View getView(FlowLayout parent, int position, Object o) { 121 | 122 | TextView view = (TextView) View.inflate(context, R.layout.flowlayout_textview_no_selected, null); 123 | view.setText(mValsSelected[position]); 124 | return view; 125 | } 126 | }); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /app/src/main/java/com/wy/flowlayoutusedemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.wy.flowlayoutusedemo; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | import butterknife.BindView; 10 | import butterknife.ButterKnife; 11 | import butterknife.OnClick; 12 | 13 | public class MainActivity extends AppCompatActivity { 14 | 15 | @BindView(R.id.btn1) 16 | Button btn1; 17 | @BindView(R.id.btn2) 18 | Button btn2; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | ButterKnife.bind(this); 25 | } 26 | 27 | @OnClick({R.id.btn1, R.id.btn2}) 28 | public void onViewClicked(View view) { 29 | switch (view.getId()) { 30 | case R.id.btn1://本地历史记录 31 | startActivity(new Intent(getApplicationContext(),Button1Activity.class)); 32 | break; 33 | case R.id.btn2://标签(单选、多选) 34 | startActivity(new Intent(getApplicationContext(),Button2Activity.class)); 35 | break; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/wy/flowlayoutusedemo/util/SharedPreferencesUtil.java: -------------------------------------------------------------------------------- 1 | package com.wy.flowlayoutusedemo.util; 2 | 3 | import android.content.Context; 4 | import android.content.SharedPreferences; 5 | 6 | /** 7 | * SharedPreferences:共享偏好,用来做数据存储,通过xml,存放标记性数据和设置信息 8 | */ 9 | public class SharedPreferencesUtil { 10 | private static SharedPreferences sharedPreferences; 11 | 12 | /**写入Boolean变量至sharedPreferences中 13 | * @param context 上下文环境 14 | * @param key 存储节点名称 15 | * @param value 存储节点的值 16 | */ 17 | public static void putBoolean(Context context,String key,Boolean value){ 18 | //(存储节点文件名称,读写方式) 19 | if (sharedPreferences == null){ 20 | sharedPreferences = context.getSharedPreferences("config",Context.MODE_PRIVATE); 21 | } 22 | sharedPreferences.edit().putBoolean(key,value).commit(); 23 | } 24 | 25 | /**读取boolean标识从sharedPreferences中 26 | * @param context 上下文环境 27 | * @param key 存储节点名称 28 | * @param value 没有此节点的默认值 29 | * @return 默认值或者此节点读取到的结果 30 | */ 31 | public static boolean getBoolean(Context context, String key, Boolean value){ 32 | //(存储节点文件名称,读写方式) 33 | if (sharedPreferences == null){ 34 | sharedPreferences = context.getSharedPreferences("config",Context.MODE_PRIVATE); 35 | } 36 | return sharedPreferences.getBoolean(key,value); 37 | } 38 | 39 | /**写入String变量至sharedPreferences中 40 | * @param context 上下文环境 41 | * @param key 存储节点名称 42 | * @param value 存储节点的值String 43 | */ 44 | public static void putString(Context context,String key,String value){ 45 | //存储节点文件的名称,读写方式 46 | if(sharedPreferences == null){ 47 | sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE); 48 | } 49 | sharedPreferences.edit().putString(key, value).commit(); 50 | } 51 | 52 | 53 | /**读取String标识从sharedPreferences中 54 | * @param context 上下文环境 55 | * @param key 存储节点名称 56 | * @param defValue 没有此节点的默认值 57 | * @return 返回默认值或者此节点读取到的结果 58 | */ 59 | public static String getString(Context context,String key,String defValue){ 60 | //存储节点文件的名称,读写方式 61 | if(sharedPreferences == null){ 62 | sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE); 63 | } 64 | return sharedPreferences.getString(key, defValue); 65 | } 66 | 67 | /**写入int变量至sharedPreferences中 68 | * @param context 上下文环境 69 | * @param key 存储节点名称 70 | * @param value 存储节点的值String 71 | */ 72 | public static void putInt(Context context,String key,int value){ 73 | //存储节点文件的名称,读写方式 74 | if(sharedPreferences == null){ 75 | sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE); 76 | } 77 | sharedPreferences.edit().putInt(key, value).commit(); 78 | } 79 | 80 | 81 | /**读取int标识从sharedPreferences中 82 | * @param context 上下文环境 83 | * @param key 存储节点名称 84 | * @param defValue 没有此节点的默认值 85 | * @return 返回默认值或者此节点读取到的结果 86 | */ 87 | public static int getInt(Context context,String key,int defValue){ 88 | //存储节点文件的名称,读写方式 89 | if(sharedPreferences == null){ 90 | sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE); 91 | } 92 | return sharedPreferences.getInt(key, defValue); 93 | } 94 | 95 | /** 96 | * 从sharedPreferences中移除指定节点 97 | * @param context 上下文环境 98 | * @param key 需要移除节点的名称 99 | */ 100 | public static void remove(Context context,String key){ 101 | //存储节点文件的名称,读写方式 102 | if(sharedPreferences == null){ 103 | sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE); 104 | } 105 | sharedPreferences.edit().remove(key).commit(); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /app/src/main/java/com/wy/flowlayoutusedemo/util/ToastUtil.java: -------------------------------------------------------------------------------- 1 | package com.wy.flowlayoutusedemo.util; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.widget.Toast; 6 | 7 | /** 8 | * 下一个Toast覆盖上一个Toast 9 | */ 10 | public class ToastUtil { 11 | private static Toast mToast; 12 | private static Handler mHandler = new Handler(); 13 | private static Runnable r = new Runnable() { 14 | public void run() { 15 | mToast.cancel(); 16 | } 17 | }; 18 | 19 | public static void makeText(Context mContext, String text) { 20 | 21 | mHandler.removeCallbacks(r); 22 | if (mToast != null) 23 | mToast.setText(text); 24 | else 25 | mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG); 26 | mHandler.postDelayed(r, 2000); 27 | 28 | mToast.show(); 29 | } 30 | 31 | // public static void makeText(Context mContext, int resId, int duration) { 32 | // makeText(mContext, mContext.getResources().getString(resId), duration); 33 | // } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/res/color/select_flowlayout_textview_color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangyang0313/FlowLayouUseDemo/83f63b01c3a94052b38e5be2be60d0cc0509269e/app/src/main/res/drawable/logo.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/select_flowlayout_textview_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_circle_corner_blue.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_circle_corner_blue3.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_circle_corner_blue5.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_circle_corner_blue_checked.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_circle_corner_white.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_button1.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 19 | 20 | 32 | 33 | 41 | 42 | 43 | 52 | 53 | 63 | 64 | 77 | 78 | 79 | 86 | 87 | 95 | 96 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_button2.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 21 | 22 | 28 | 29 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |