一、 概述
可以說微信做的用戶體驗太棒了,可以做到老少皆宜,給個贊,我們也同時應該勸誡自己,用戶體驗應該向微信看齊,微信就是我們的標桿,那我們明天也來仿一仿微信添加的標簽功用。只能仿著做了,真是做不到微信的那種體驗。甘拜上風。
我們上篇學習了shape屬性的用法,那我們明天就用shape來做下微信的標簽功用。先看一下效果。
我不只用到了shape屬性,還用到了翔哥的標簽規劃FlowLayout跟TagFlowLayout鴻洋的博客
二、效果圖
三 、定義shape
添加標簽
<?XmlRss/ target=_blank class=infotextkey>Xml version="1.0" encoding="utf-8"?> <shape XmlRss/ target=_blank class=infotextkey>Xmlns:Android="http://schemas.Android.com/apk/res/Android" android:shape="rectangle" > <corners android:bottomLeftRadius="30dp" android:bottomRightRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> <stroke android:dashWidth="5dp" android:dashGap="2dp" android:width="1dp" android:color="#e0e0e0" /> <padding android: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"> <corners android:bottomLeftRadius="30dp" android:bottomRightRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> <solid android:color="#00FF00" /> <padding android: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" > <corners android:bottomLeftRadius="30dp" android:bottomRightRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> <stroke android:width="1dp" android:color="#00FF00" /> <padding android: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"> <corners android:bottomLeftRadius="30dp" android:bottomRightRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> <stroke android:width="1dp" android:color="#00FF00" /> <padding android:bottom="2dp" android:left="10dp" android:right="10dp" android:top="2dp" /> </shape>
以上是局部shape定義,大家可以下載源碼自己看。
四、 思緒
我們可以標簽大約有以下邏輯
點擊下面標簽刪除 一切標簽外面更新未選中
點擊一切標簽的某一個 下面標簽添加或許刪除
五、代碼
public class MainActivity extends AppCompatActivity { private FlowLayout flowLayout;//下面的flowLayout private TagFlowLayout allFlowLayout;//一切標簽的TagFlowLayout private 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; @Override protected 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() { @Override public 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 一個EditText editText.setText(label_list.get(i)); addLabel(editText);//添加標簽 } } /** * 初始化默許的添加標簽 */ private void initEdittext(){ editText = new EditText(getApplicationContext()); editText.setHint("添加標簽"); //設置固定寬度 editText.setMinEms(4); editText.setTextSize(12); //設置shape editText.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() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { tagNormal(); } @Override public void afterTextChanged(Editable s) { } }); } /** * 初始化一切標簽列表 */ private void initAllLeblLayout() { //初始化適配器 tagAdapter = new TagAdapter<String>(all_label_List) { @Override public 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() { @Override public 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() { @Override public 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() { @Override public 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完成),希望對大家有所協助,假如大家有任何疑問請給我留言,本站會及時回復大家的。在此也十分感激大家對本站網站的支持!
【iOS仿微信添加標簽效果(shape完成)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!