欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS仿微信添加標(biāo)簽效果(shape實(shí)現(xiàn))

 更新時(shí)間:2016年11月14日 15:56:23   作者:小_源  
微信做的用戶體驗(yàn)非常棒,今天用shape來做下微信的標(biāo)簽功能,非常不錯(cuò),對(duì)ios 仿微信添加標(biāo)簽功能感興趣的朋友一起看看吧

一、 概述

可以說微信做的用戶體驗(yàn)太棒了,可以做到老少皆宜,給個(gè)贊,我們也同時(shí)應(yīng)該告誡自己,用戶體驗(yàn)應(yīng)該向微信看齊,微信就是我們的標(biāo)桿,那我們今天也來仿一仿微信添加的標(biāo)簽功能。只能仿著做了,真是做不到微信的那種體驗(yàn)。甘拜下風(fēng)。

我們上篇學(xué)習(xí)了shape屬性的用法,那我們今天就用shape來做下微信的標(biāo)簽功能。先看一下效果。

我不僅用到了shape屬性,還用到了翔哥的標(biāo)簽布局FlowLayout跟TagFlowLayout鴻洋的博客

二、效果圖

這里寫圖片描述

三 、定義shape

添加標(biāo)簽

<?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: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>

刪除標(biāo)簽

<?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>

正常標(biāo)簽

<?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>

標(biāo)簽選中

<?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定義,大家可以下載源碼自己看。

四、 思路

我們可以標(biāo)簽大概有以下邏輯

點(diǎn)擊上面標(biāo)簽刪除 所有標(biāo)簽里面更新未選中

點(diǎn)擊所有標(biāo)簽的某一個(gè) 上面標(biāo)簽添加或者刪除

五、代碼

public class MainActivity extends AppCompatActivity {
private FlowLayout flowLayout;//上面的flowLayout
private TagFlowLayout allFlowLayout;//所有標(biāo)簽的TagFlowLayout
private List<String> label_list = new ArrayList<>();//上面的標(biāo)簽列表
private List<String> all_label_List = new ArrayList<>();//所有標(biāo)簽列表
final List<TextView> labels = new ArrayList<>();//存放標(biāo)簽
final List<Boolean> labelStates = new ArrayList<>();//存放標(biāo)簽狀態(tài)
final Set<Integer> set = new HashSet<>();//存放選中的
private TagAdapter<String> tagAdapter;//標(biāo)簽適配器
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);
}
}
});
}
/**
* 初始化數(shù)據(jù)
*/
private void initData(){
//初始化上面標(biāo)簽
label_list.add("同事");
label_list.add("親人");
label_list.add("同學(xué)");
label_list.add("朋友");
label_list.add("知己");
//初始化下面標(biāo)簽列表
all_label_List.addAll(label_list);
all_label_List.add("異性朋友");
all_label_List.add("高中同學(xué)");
all_label_List.add("大學(xué)同學(xué)");
all_label_List.add("社會(huì)朋友");
for (int i = 0; i < label_list.size() ; i++) {
editText = new EditText(getApplicationContext());//new 一個(gè)EditText
editText.setText(label_list.get(i));
addLabel(editText);//添加標(biāo)簽
}
}
/**
* 初始化默認(rèn)的添加標(biāo)簽
*/
private void initEdittext(){
editText = new EditText(getApplicationContext());
editText.setHint("添加標(biāo)簽");
//設(shè)置固定寬度
editText.setMinEms(4);
editText.setTextSize(12);
//設(shè)置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) {
}
});
}
/**
* 初始化所有標(biāo)簽列表
*/
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);
//根據(jù)上面標(biāo)簽來判斷下面的標(biāo)簽是否含有上面的標(biāo)簽
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);//設(shè)為選中
}
}
}
tagAdapter.notifyDataChanged();
//給下面的標(biāo)簽添加監(jiān)聽
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());
}
//如果上面包含點(diǎn)擊的標(biāo)簽就刪除
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;
}
});
//已經(jīng)選中的監(jiān)聽
allFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
@Override
public void onSelected(Set<Integer> selectPosSet) {
set.clear();
set.addAll(selectPosSet);
}
});
}
/**
* 添加標(biāo)簽
* @param editText
* @return
*/
private boolean addLabel(EditText editText) {
String editTextContent = editText.getText().toString();
//判斷輸入是否為空
if (editTextContent.equals(""))
return true;
//判斷是否重復(fù)
for (TextView tag : labels) {
String tempStr = tag.getText().toString();
if (tempStr.equals(editTextContent)) {
editText.setText("");
editText.requestFocus();
return true;
}
}
//添加標(biāo)簽
final TextView temp = getTag(editText.getText().toString());
labels.add(temp);
labelStates.add(false);
//添加點(diǎn)擊事件,點(diǎn)擊變成選中狀態(tài),選中狀態(tài)下被點(diǎn)擊則刪除
temp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int curIndex = labels.indexOf(temp);
if (!labelStates.get(curIndex)) {
//顯示 ×號(hào)刪除
temp.setText(temp.getText() + " ×");
temp.setBackgroundResource(R.drawable.label_del);
temp.setTextColor(Color.parseColor("#ffffff"));
//修改選中狀態(tài)
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);
//讓輸入框在最后一個(gè)位置上
editText.bringToFront();
//清空編輯框
editText.setText("");
editText.requestFocus();
return true;
}
/**
* 根據(jù)字符刪除標(biāo)簽
* @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);//重置選中的標(biāo)簽
}
/**
* 標(biāo)簽恢復(fù)到正常狀態(tài)
*/
private void tagNormal() {
//輸入文字時(shí)取消已經(jīng)選中的標(biāo)簽
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"));
}
}
}
/**
* 創(chuàng)建一個(gè)正常狀態(tài)的標(biāo)簽
* @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;
}
}

注釋的很詳細(xì)了。其實(shí)正常一步步來就按照邏輯來就可以實(shí)現(xiàn),別慌,別亂,別急躁。什么功能都能實(shí)現(xiàn)的。

六、源碼

點(diǎn)擊下載

以上所述是小編給大家介紹的iOS仿微信添加標(biāo)簽效果(shape實(shí)現(xiàn)),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論