一、 概述
可以說微信做的用戶體驗太棒了,可以做到老少皆宜,給個贊,我們也同時應該告誡自己,用戶體驗應該向微信看齊,微信就是我們的標桿,那我們今天也來仿一仿微信添加的標簽功能。只能仿著做了,真是做不到微信的那種體驗。甘拜下風。
我們上篇學習了shape屬性的用法,那我們今天就用shape來做下微信的標簽功能。先看一下效果。
我不僅用到了shape屬性,還用到了翔哥的標簽布局FlowLayout跟TagFlowLayout鴻洋的博客
二、效果圖
三 、定義shape
添加標簽
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><cornersandroid:bottomLeftRadius="30dp"android:bottomRightRadius="30dp"android:topLeftRadius="30dp"android:topRightRadius="30dp" /><stroke android:dashWidth="5dp" android:dashGap="2dp" android:width="1dp" android:color="#e0e0e0" /><paddingandroid:bottom="2dp"android:left="10dp"android:right="10dp"android:top="2dp" /></shape>
刪除標簽
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><cornersandroid:bottomLeftRadius="30dp"android:bottomRightRadius="30dp"android:topLeftRadius="30dp"android:topRightRadius="30dp" /><solid android:color="#00FF00" /><paddingandroid:bottom="2dp"android:left="10dp"android:right="10dp"android:top="2dp" /></shape>
正常標簽
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><cornersandroid:bottomLeftRadius="30dp"android:bottomRightRadius="30dp"android:topLeftRadius="30dp"android:topRightRadius="30dp" /><stroke android:width="1dp" android:color="#00FF00" /><paddingandroid:bottom="2dp"android:left="10dp"android:right="10dp"android:top="2dp" /></shape>
標簽選中
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><cornersandroid:bottomLeftRadius="30dp"android:bottomRightRadius="30dp"android:topLeftRadius="30dp"android:topRightRadius="30dp" /><strokeandroid:width="1dp"android:color="#00FF00" /><paddingandroid:bottom="2dp"android:left="10dp"android:right="10dp"android:top="2dp" /></shape>
以上是部分shape定義,大家可以下載源碼自己看。
四、 思路
我們可以標簽大概有以下邏輯
點擊上面標簽刪除 所有標簽里面更新未選中
點擊所有標簽的某一個 上面標簽添加或者刪除
五、代碼
public class MainActivity extends AppCompatActivity {private FlowLayout flowLayout;//上面的flowLayoutprivate TagFlowLayout allFlowLayout;//所有標簽的TagFlowLayoutprivate List<String> label_list = new ArrayList<>();//上面的標簽列表private List<String> all_label_List = new ArrayList<>();//所有標簽列表final List<TextView> labels = new ArrayList<>();//存放標簽final List<Boolean> labelStates = new ArrayList<>();//存放標簽狀態final Set<Integer> set = new HashSet<>();//存放選中的private TagAdapter<String> tagAdapter;//標簽適配器private LinearLayout.LayoutParams params;private EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initData();initEdittext();initAllLeblLayout();}/*** 初始化View*/private void initView() {flowLayout = (FlowLayout) findViewById(R.id.id_flowlayout);allFlowLayout = (TagFlowLayout) findViewById(R.id.id_flowlayout_two);params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);params.setMargins(20, 20, 20, 20);flowLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String editTextContent = editText.getText().toString();if (TextUtils.isEmpty(editTextContent)) {tagNormal();} else {addLabel(editText);}}});}/*** 初始化數據*/private void initData(){//初始化上面標簽label_list.add("同事");label_list.add("親人");label_list.add("同學");label_list.add("朋友");label_list.add("知己");//初始化下面標簽列表all_label_List.addAll(label_list);all_label_List.add("異性朋友");all_label_List.add("高中同學");all_label_List.add("大學同學");all_label_List.add("社會朋友");for (int i = 0; i < label_list.size() ; i++) {editText = new EditText(getApplicationContext());//new 一個EditTexteditText.setText(label_list.get(i));addLabel(editText);//添加標簽}}/*** 初始化默認的添加標簽*/private void initEdittext(){editText = new EditText(getApplicationContext());editText.setHint("添加標簽");//設置固定寬度editText.setMinEms(4);editText.setTextSize(12);//設置shapeeditText.setBackgroundResource(R.drawable.label_add);editText.setHintTextColor(Color.parseColor("#b4b4b4"));editText.setTextColor(Color.parseColor("#000000"));editText.setLayoutParams(params);//添加到layout中flowLayout.addView(editText);editText.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {tagNormal();}@Overridepublic void afterTextChanged(Editable s) {}});}/*** 初始化所有標簽列表*/private void initAllLeblLayout() {//初始化適配器tagAdapter = new TagAdapter<String>(all_label_List) {@Overridepublic View getView(FlowLayout parent, int position, String s) {TextView tv = (TextView) getLayoutInflater().inflate(R.layout.flag_adapter,allFlowLayout, false);tv.setText(s);return tv;}};allFlowLayout.setAdapter(tagAdapter);//根據上面標簽來判斷下面的標簽是否含有上面的標簽for (int i = 0; i < label_list.size(); i++) {for (int j = 0; j < all_label_List.size(); j++) {if (label_list.get(i).equals(all_label_List.get(j))) {tagAdapter.setSelectedList(i);//設為選中}}}tagAdapter.notifyDataChanged();//給下面的標簽添加監聽allFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {@Overridepublic boolean onTagClick(View view, int position, FlowLayout parent) {if (labels.size() == 0) {editText.setText(all_label_List.get(position));addLabel(editText);return false;}List<String> list = new ArrayList<>();for (int i = 0; i < labels.size(); i++) {list.add(labels.get(i).getText().toString());}//如果上面包含點擊的標簽就刪除if (list.contains(all_label_List.get(position))) {for (int i = 0; i < list.size(); i++) {if (all_label_List.get(position).equals(list.get(i))) {flowLayout.removeView(labels.get(i));labels.remove(i);}}} else {editText.setText(all_label_List.get(position));addLabel(editText);}return false;}});//已經選中的監聽allFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {@Overridepublic void onSelected(Set<Integer> selectPosSet) {set.clear();set.addAll(selectPosSet);}});}/*** 添加標簽* @param editText* @return*/private boolean addLabel(EditText editText) {String editTextContent = editText.getText().toString();//判斷輸入是否為空if (editTextContent.equals(""))return true;//判斷是否重復for (TextView tag : labels) {String tempStr = tag.getText().toString();if (tempStr.equals(editTextContent)) {editText.setText("");editText.requestFocus();return true;}}//添加標簽final TextView temp = getTag(editText.getText().toString());labels.add(temp);labelStates.add(false);//添加點擊事件,點擊變成選中狀態,選中狀態下被點擊則刪除temp.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int curIndex = labels.indexOf(temp);if (!labelStates.get(curIndex)) {//顯示 ×號刪除temp.setText(temp.getText() + " ×");temp.setBackgroundResource(R.drawable.label_del);temp.setTextColor(Color.parseColor("#ffffff"));//修改選中狀態labelStates.set(curIndex, true);} else {delByTest(temp.getText().toString());flowLayout.removeView(temp);labels.remove(curIndex);labelStates.remove(curIndex);for (int i = 0; i < label_list.size(); i++) {for (int j = 0; j < labels.size(); j++) {if (label_list.get(i).equals(labels.get(j).getText())) {tagAdapter.setSelectedList(i);}}}tagAdapter.notifyDataChanged();}}});flowLayout.addView(temp);//讓輸入框在最后一個位置上editText.bringToFront();//清空編輯框editText.setText("");editText.requestFocus();return true;}/*** 根據字符刪除標簽* @param text*/private void delByTest(String text) {for (int i = 0; i < all_label_List.size(); i++) {String a = all_label_List.get(i) + " ×";if (a.equals(text)) {set.remove(i);}}tagAdapter.setSelectedList(set);//重置選中的標簽}/*** 標簽恢復到正常狀態*/private void tagNormal() {//輸入文字時取消已經選中的標簽for (int i = 0; i < labelStates.size(); i++) {if (labelStates.get(i)) {TextView tmp = labels.get(i);tmp.setText(tmp.getText().toString().replace(" ×", ""));labelStates.set(i, false);tmp.setBackgroundResource(R.drawable.label_normal);tmp.setTextColor(Color.parseColor("#00FF00"));}}}/*** 創建一個正常狀態的標簽* @param label* @return*/private TextView getTag(String label) {TextView textView = new TextView(getApplicationContext());textView.setTextSize(12);textView.setBackgroundResource(R.drawable.label_normal);textView.setTextColor(Color.parseColor("#00FF00"));textView.setText(label);textView.setLayoutParams(params);return textView;}}
注釋的很詳細了。其實正常一步步來就按照邏輯來就可以實現,別慌,別亂,別急躁。什么功能都能實現的。
六、源碼
點擊下載
以上所述是小編給大家介紹的iOS仿微信添加標簽效果(shape實現),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答