Android仿微信标签功能

微信中有对联系人添加标签的功能,如下图所示。

这里有三种状态的标签,分别的未选择,选中,编辑中,由于前两种标签不需要提供输入,所以用TextView实现即可,编辑中的标签用EditText来实现。而标签的形状就用Shape来实现。

在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" />
  <!-- 这里实现虚线边框-->
 <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用以存放标签(如果要实现多行标签自适应添加,用自定义的FlowLayout,代码网上很多。)

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>

根据对微信标签的分析,这里可以这样实现,创建一个EditText,对其软键盘的Enter和Delete按键进行监听,当输入完成后按下Enter则生成一个标签,添加到LinearLayout中。然后如果当标签内文字为空时,按下删除键,就将它前一个标签的状态修改为选中状态。同样地,当点击未选择的标签也可以选中该标签进行删除。

详细实现如下

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);

    //存放标签和标签选择状态
    final List<TextView> tagView=new ArrayList<>();
    final List<Boolean> tagViewState=new ArrayList<>();

    //创建编辑中的标签
    final EditText editText=new EditText(getApplicationContext());
    editText.setHint("添加标签");
    //设置固定宽度
    editText.setMinEms(4);
    editText.setTextSize(12);
    //设置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键监听
    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;
              //判断标签是否重复添加
              for(TextView tag:tagView){
                String tempStr=tag.getText().toString();
                if(tempStr.equals(editTextContent)) {
                  Log.e("tag","重复添加");
                  editText.setText("");
                  editText.requestFocus();
                  return true;
                }
              }
              //添加标签
              final TextView temp = getTag(editText.getText().toString());
              tagView.add(temp);
              tagViewState.add(false);
              //添加点击事件,点击变成选中状态,选中状态下被点击则删除
              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"));
                    //修改选中状态
                    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;
              //没有添加标签则不继续执行
              if (lastIndex < 0)
                return false;
              //获取前一个标签
              TextView prevTag = tagView.get(lastIndex);
              //第一次按下Del键则变成选中状态,选中状态下按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;
      }

    });

    //监听编辑标签的输入事件
    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) {
        //输入文字时取消已经选中的标签
        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) {

      }
    });

  }

  /**
   * 创建一个正常状态的标签
   * @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;
  }

}

效果挺好。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android如何取得本地通讯录的头像的原图的实现代码

    本文介绍了android如何取得本地通讯录的头像的原图的实现代码,分享给大家,也给自己留个笔记 如果想通讯录进入详情页,那么最重要的参数就是contactId,这个是联系人的唯一标识 getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position

  • Android带刷新时间显示的PullToRefresh上下拉刷新

    用过很多上下拉刷新,找到一个让自己满意的确实不容易,有些好的刷新控件,也并不是公司所需要的,在这里我给大家推荐一下我所喜欢的上下拉控件,实现也挺简单,需要的不妨来用一下,效果一看便知 加载就是一个圆形进度条,一个正在加载Textview,我就不上图了 这个是刷新的头布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.an

  • Android实现动态添加标签及其点击事件

    在做Android开发的时候,会遇到动态添加标签让用户选择的功能,所以自己写了个例子,运行效果图如下. 标签可以左右滑动进行选择,点击的时候,会弹出toast提示选择或者取消选择了哪个标签.通过动态添加TextView作为标签,并给TextView设置背景,通过selector选择器改变其背景颜色,来确定是否处于选中状态. 代码如下所示: 1.标签的布局文件,我在标签里只设置了一个TextView <?xml version="1.0" encoding="utf-8&

  • Android中LayoutInflater.inflater()的正确打开方式

    前言 LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源码的流程. 首先来看from()的源码: /** * Obtains the LayoutInflater from the given context. */ public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater

  • Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

    一.自定义Dialog 在沉浸式效果下,当界面弹出对话框时,对话框将获取到焦点,这将导致界面退出沉浸式效果,那么是不是能通过屏蔽对话框获取焦点来达到不退出沉浸式的目的呢.说干就干,我们先来看一下改善后的效果图. 普通对话框弹出效果 LoadingDialog弹出效果 自定义LoadingDialog public class LoadingDialog extends Dialog { public LoadingDialog(Context context) { super(context);

  • Android实现外部唤起应用跳转指定页面的方法

    前言 通常有这么一个场景,就是分享内容到微信朋友圈等,然后点击内容中的某个按钮就可以唤起自家应用. 这里要讲的也是使用 scheme 的方式去实现跳转,先捋一捋思路,首先如果要外部能唤醒 App ,那么 App 肯定要先注册一个全局的事件监听吧.然后,应该有一个页面来处理接受事件然后解析出具体的参数然后跳转具体的页面.就是这么简单. 思路捋好来,那么就来一一实现吧. 注册事件监听 这里需要使用到 Android Activity中的 <intent-filter> ,现在可以创建一个解析跳转的

  • Android中buildToolVersion与CompileSdkVersion的区别

    SDK中主要的目录: [build-tools]里面是不同版本(例如21.1.1)的build工具,这些工具包括了aapt打包工具.dx.bat.aidl.exe等等 [platform]是存放不同API-level版本SDK目录的地方 [platform-tools]是一些android平台相关的工具,adb.fastboot等 [tools]是指的安卓开发相关的工具,例如android.bat.ddms.bat(Dalvik debug Monitor Service).draw9patch

  • Android定时器实现定时执行、重复执行、定时重复执行、定次数执行的多种方式

    作用: 1.定时执行某种功能 2.重复执行.定时重复执行.定次数执行某种功能 类别: 1. Thread(new Runnable) 2.Thread() 3.Timer 4.Handler ····· 代码如下: 1.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro

  • android的ListView点击item使item展开的做法的实现代码

    本文介绍了android的ListView点击item使item展开的做法的实现代码,分享给大家,具体如下: 效果图: 原理是点击item的时候,重新measure list的各个item的高度 list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { My

  • Android单一实例全局可调用网络加载弹窗

    最近因为项目需求,需要完成一个全局的网络加载弹窗需求,真正完成这个需求之后,感觉最重要的不是结果,而是思维. 我刚开始接到这个需求的时候,第一种想到的方案是 基类加单例.但是实际做起来之后发现,因为单例的原因,你的弹窗只能在第一次创建这个单例的activity中显示出来. 那么发现这个问题之后在这个的基础上改进一下,如果我不用activity的上下文,而是采用类似于Application的一种全局上下文呢?当然,个人能力有限,这种想法就给毙掉了,后来由导师指点,利用service的上下文,dia

随机推荐