最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android仿微信標(biāo)簽功能

 更新時間:2018年12月07日 14:13:06   作者:qq402335257  
這篇文章主要為大家詳細(xì)介紹了Android仿微信標(biāo)簽功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

微信中有對聯(lián)系人添加標(biāo)簽的功能,如下圖所示。

這里有三種狀態(tài)的標(biāo)簽,分別的未選擇,選中,編輯中,由于前兩種標(biāo)簽不需要提供輸入,所以用TextView實現(xiàn)即可,編輯中的標(biāo)簽用EditText來實現(xiàn)。而標(biāo)簽的形狀就用Shape來實現(xiàn)。

在drawable下新建xml文件,這里先上Shape的xml文件。

tag_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
 
  <corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />
 
 <stroke android:width="1dp" android:color="#66CDAA" />
 
  <padding
    android:bottom="4dp"
    android:left="8dp"
    android:right="8dp"
    android:top="4dp" />
 
</shape>

tag_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
 
  <corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />
 
 <stroke android:width="1dp" android:color="#66CDAA" />
 
  <padding
    android:bottom="4dp"
    android:left="8dp"
    android:right="8dp"
    android:top="4dp" />
 
</shape>

tag_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
 
  <corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />
  <!-- 這里實現(xiàn)虛線邊框-->
 <stroke android:dashWidth="5dp" android:dashGap="2dp" android:width="1dp" android:color="#e0e0e0" />
 
  <padding
    android:bottom="4dp"
    android:left="8dp"
    android:right="8dp"
    android:top="4dp" />
</shape>

接著在在布局文件中新建一個LinearLayout用以存放標(biāo)簽(如果要實現(xiàn)多行標(biāo)簽自適應(yīng)添加,用自定義的FlowLayout,代碼網(wǎng)上很多。)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/tag_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  tools:context="com.qtree.tagdemo.MainActivity">
 
</LinearLayout>

      根據(jù)對微信標(biāo)簽的分析,這里可以這樣實現(xiàn),創(chuàng)建一個EditText,對其軟鍵盤的Enter和Delete按鍵進(jìn)行監(jiān)聽,當(dāng)輸入完成后按下Enter則生成一個標(biāo)簽,添加到LinearLayout中。然后如果當(dāng)標(biāo)簽內(nèi)文字為空時,按下刪除鍵,就將它前一個標(biāo)簽的狀態(tài)修改為選中狀態(tài)。同樣地,當(dāng)點擊未選擇的標(biāo)簽也可以選中該標(biāo)簽進(jìn)行刪除。

詳細(xì)實現(xiàn)如下

package com.qtree.tagdemo;
 
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
 
  private LinearLayout layout;
  private LinearLayout.LayoutParams params;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    layout=(LinearLayout)findViewById(R.id.tag_container);
 
    params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(30,30,0,0);
 
    //存放標(biāo)簽和標(biāo)簽選擇狀態(tài)
    final List<TextView> tagView=new ArrayList<>();
    final List<Boolean> tagViewState=new ArrayList<>();
 
    //創(chuàng)建編輯中的標(biāo)簽
    final EditText editText=new EditText(getApplicationContext());
    editText.setHint("添加標(biāo)簽");
    //設(shè)置固定寬度
    editText.setMinEms(4);
    editText.setTextSize(12);
    //設(shè)置shape
    editText.setBackgroundResource(R.drawable.tag_edit);
    editText.setHintTextColor(Color.parseColor("#b4b4b4"));
    editText.setTextColor(Color.parseColor("#000000"));
    editText.setLayoutParams(params);
    
    //添加到layout中
    layout.addView(editText);
 
    //對軟鍵盤的Enter和Del鍵監(jiān)聽
    editText.setOnKeyListener(new View.OnKeyListener() {
      @Override
      public boolean onKey(View v, int keyCode, KeyEvent event) {
 
        if (KeyEvent.ACTION_DOWN == event.getAction()) {
          switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:
              String editTextContent = editText.getText().toString();
              //判斷輸入是否為空
              if (editTextContent.equals(""))
                return true;
              //判斷標(biāo)簽是否重復(fù)添加
              for(TextView tag:tagView){
                String tempStr=tag.getText().toString();
                if(tempStr.equals(editTextContent)) {
                  Log.e("tag","重復(fù)添加");
                  editText.setText("");
                  editText.requestFocus();
                  return true;
                }
              }
              //添加標(biāo)簽
              final TextView temp = getTag(editText.getText().toString());
              tagView.add(temp);
              tagViewState.add(false);
              //添加點擊事件,點擊變成選中狀態(tài),選中狀態(tài)下被點擊則刪除
              temp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  int curIndex = tagView.indexOf(temp);
                  if (!tagViewState.get(curIndex)) {
                    //顯示 ×號刪除
                    temp.setText(temp.getText() + " ×");
                    temp.setBackgroundResource(R.drawable.tag_selected);
                    temp.setTextColor(Color.parseColor("#ffffff"));
                    //修改選中狀態(tài)
                    tagViewState.set(curIndex, true);
                  } else {
                    layout.removeView(temp);
                    tagView.remove(curIndex);
                    tagViewState.remove(curIndex);
                  }
                }
              });
              layout.addView(temp);
              //讓編輯框在最后一個位置上
              editText.bringToFront();
              //清空編輯框
              editText.setText("");
              editText.requestFocus();
              return true;
            case KeyEvent.KEYCODE_DEL:
              int lastIndex = tagView.size() - 1;
              //沒有添加標(biāo)簽則不繼續(xù)執(zhí)行
              if (lastIndex < 0)
                return false;
              //獲取前一個標(biāo)簽
              TextView prevTag = tagView.get(lastIndex);
              //第一次按下Del鍵則變成選中狀態(tài),選中狀態(tài)下按Del鍵則刪除
              if (tagViewState.get(lastIndex)) {
                tagView.remove(prevTag);
                tagViewState.remove(lastIndex);
                layout.removeView(prevTag);
              } else {
                String te = editText.getText().toString();
                if (te.equals("")) {
                  prevTag.setText(prevTag.getText() + " ×");
                  prevTag.setBackgroundResource(R.drawable.tag_selected);
                  prevTag.setTextColor(Color.parseColor("#ffffff"));
                  tagViewState.set(lastIndex, true);
                }
              }
              break;
          }
 
        }
        return false;
      }
 
    });
 
    //監(jiān)聽編輯標(biāo)簽的輸入事件
    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) {
        //輸入文字時取消已經(jīng)選中的標(biāo)簽
        for (int i = 0; i < tagViewState.size(); i++) {
          if (tagViewState.get(i)) {
            TextView tmp = tagView.get(i);
            tmp.setText(tmp.getText().toString().replace(" ×", ""));
            tagViewState.set(i, false);
            tmp.setBackgroundResource(R.drawable.tag_normal);
            tmp.setTextColor(Color.parseColor("#66CDAA"));
          }
        }
      }
 
      @Override
      public void afterTextChanged(Editable s) {
 
      }
    });
 
  }
 
  /**
   * 創(chuàng)建一個正常狀態(tài)的標(biāo)簽
   * @param tag
   * @return
   */
  private TextView getTag(String tag){
    TextView textView=new TextView(getApplicationContext());
    textView.setTextSize(12);
    textView.setBackgroundResource(R.drawable.tag_normal);
    textView.setTextColor(Color.parseColor("#66CDAA"));
    textView.setText(tag);
    textView.setLayoutParams(params);
    return textView;
  }
 
}


效果挺好。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解析Android框架之Volley源碼

    解析Android框架之Volley源碼

    我們知道Volley是在2013年Google I/O大會上推出了一個新的網(wǎng)絡(luò)通信框架,他的設(shè)計目的就是為了那些請求數(shù)據(jù)量不是特別大,但是又是特別頻繁的網(wǎng)絡(luò)操作非常適合。但是對于數(shù)據(jù)量較大的請求,比如說下載一個較大的文件,Volley可能相比于其他的框架,就有點不足了。
    2021-06-06
  • Android開發(fā)中日期工具類DateUtil完整實例

    Android開發(fā)中日期工具類DateUtil完整實例

    這篇文章主要介紹了Android開發(fā)中日期工具類DateUtil,結(jié)合完整實例形式分析了Android針對日期與時間的計算、轉(zhuǎn)換、格式化、獲取等相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android自定義上下左右間隔線

    Android自定義上下左右間隔線

    這篇文章主要為大家詳細(xì)介紹了Android自定義上下左右間隔線,自定義SpaceItemDecoration分割線,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • android實現(xiàn)人臉識別技術(shù)的示例代碼

    android實現(xiàn)人臉識別技術(shù)的示例代碼

    本篇文章主要介紹了android人臉識別技術(shù)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android實現(xiàn)短信驗證碼自動填寫

    Android實現(xiàn)短信驗證碼自動填寫

    這篇文章主要為大家詳細(xì)介紹了Android短信驗證碼自動填寫功能的實現(xiàn)過程,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 【Android 基礎(chǔ)】詳解Animation 動畫介紹和實現(xiàn)

    【Android 基礎(chǔ)】詳解Animation 動畫介紹和實現(xiàn)

    這篇文章主要介紹了【Android 基礎(chǔ)】詳解Animation 動畫介紹和實現(xiàn) ,對于想要學(xué)習(xí)android開發(fā)的同學(xué)具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • Flutter UI實現(xiàn)側(cè)拉抽屜菜單

    Flutter UI實現(xiàn)側(cè)拉抽屜菜單

    這篇文章主要為大家詳細(xì)介紹了Flutter UI實現(xiàn)側(cè)拉抽屜菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • react native android6+拍照閃退或重啟的解決方案

    react native android6+拍照閃退或重啟的解決方案

    android 6+權(quán)限使用的時候需要動態(tài)申請,那么在使用rn的時候要怎么處理拍照權(quán)限問題呢?本文提供的是一攬子rn操作相冊、拍照的解決方案,需要的朋友可以參考下
    2017-11-11
  • Android Parcelable與Serializable詳解及區(qū)別

    Android Parcelable與Serializable詳解及區(qū)別

    這篇文章主要介紹了Android Parcelable與Serializable詳解及區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android編程重寫ViewGroup實現(xiàn)卡片布局的方法

    Android編程重寫ViewGroup實現(xiàn)卡片布局的方法

    這篇文章主要介紹了Android編程重寫ViewGroup實現(xiàn)卡片布局的方法,實例分析新建FlowLayout繼承ViewGroup類及設(shè)置布局文件實現(xiàn)卡片布局效果的相關(guān)技巧,需要的朋友可以參考下
    2016-02-02

最新評論

青铜峡市| 区。| 张掖市| 孝昌县| 宜章县| 满洲里市| 金坛市| 丽水市| 交城县| 绥化市| 武宁县| 正阳县| 上饶县| 凉山| 蛟河市| 镇原县| 绩溪县| 周口市| 丰台区| 赞皇县| 凌海市| 苏尼特右旗| 枝江市| 凤阳县| 百色市| 永吉县| 通山县| 图木舒克市| 南投市| 拉孜县| 江北区| 封丘县| 上杭县| 苏州市| 汾阳市| 遂平县| 华阴市| 离岛区| 万全县| 定南县| 天台县|