代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
编辑联系人 联系人块,点按删除,键盘删除,键盘识别
作者:
/ 发布于
2013/3/18
/
636
编辑联系人 联系人块,点按删除,键盘删除,键盘识别等
package com.example.animationtest; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.example.animationtest.view.FlowLayout; public class EditContactActivity extends Activity { /** @category 上下文对象 */ Context context = this; /** @category 编辑框 */ EditText edit; /** @category 模拟增加按钮 */ Button add; /** @category 流布局 */ FlowLayout fl; TextView tips; RelativeLayout editPanel; LinearLayout tipsPanel; /** @category 数据列表 */ List<EditBean> data; int count = 0; /** @category 用来判断当前View的删除反选状态 */ boolean delMode = false; /** @category 用来截取空格键还是回车键 */ String tempEdit = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_editcontact); init(); data = new ArrayList<EditContactActivity.EditBean>(); tips.setText("请输入号码或选择联系人"); editPanel.setVisibility(View.GONE); tipsPanel.setVisibility(View.VISIBLE); tipsPanel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub editPanel.setVisibility(View.VISIBLE); tipsPanel.setVisibility(View.GONE); edit.requestFocus(); } }); edit.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if (!hasFocus) { String content = edit.getText().toString(); if (!"".equals(content)) { add(content); edit.setText(""); } editPanel.setVisibility(View.GONE); tipsPanel.setVisibility(View.VISIBLE); loadTips(); } } }); edit.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub // 有新字符输入时清除删除状态 delMode = false; // 新字符输入时将View的反色状态清除 int flSize = fl.getChildCount(); if (flSize > 1) { View v = fl.getChildAt(flSize - 2); v.setBackgroundColor(Color.TRANSPARENT); } // 缓存上一次字符供afrerTextChanged使用 tempEdit = s.toString(); } @Override public void afterTextChanged(android.text.Editable s) { // TODO Auto-generated method stub // 当前输入框字符 String source = s.toString(); int tempLen = tempEdit.length(); if (tempLen < source.length()) { String nowStr = source.substring(tempLen); if (" ".equals(nowStr)) {// 截获到空格键 System.out.println("space"); if (source.length() > 2) { add(source.substring(0, source.length() - 1)); } edit.setText(""); } else if ("\r".equals(nowStr) || "\n".equals(nowStr)) {// 截获到回车键 System.out.println("enter"); if (source.length() > 2) { add(source.substring(0, source.length() - 1)); } edit.setText(""); } else if (",".equals(nowStr) || ",".equals(nowStr)) {// 截获到逗号 System.out.println(","); if (source.length() > 2) { add(source.substring(0, source.length() - 1)); } edit.setText(""); } } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO 截获按键 switch (keyCode) { case KeyEvent.KEYCODE_DEL: delete(); break; } return super.onKeyDown(keyCode, event); } private void init() { editPanel = (RelativeLayout) findViewById(R.id.editPanel); tipsPanel = (LinearLayout) findViewById(R.id.editTipsPanel); tips = (TextView) findViewById(R.id.editTips); add = (Button) findViewById(R.id.btn_add); edit = (EditText) findViewById(R.id.contact); int width = context.getResources().getDisplayMetrics().widthPixels; edit.setWidth(width); final float scale = context.getResources().getDisplayMetrics().density; int height = (int) (48 * scale + 0.5f); edit.setHeight(height); fl = (FlowLayout) findViewById(R.id.area); add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { add("Element:" + count); loadTips(); } }); } /** @category 加载人员提示 */ public void loadTips() { String tipsContent = ""; for (EditBean b : data) { tipsContent += b.getName() + ","; } if (tipsContent.length() > 1) { tipsContent = tipsContent.substring(0, tipsContent.length() - 1); tips.setText(tipsContent + "(共" + count + "人)"); } else { tips.setText("请输入号码或选择联系人"); } } /** @category 增加一个元素 */ public void add(String title) { int n = fl.getChildCount(); System.out.println("now have element:" + n); Editable ea = new Editable(context); ea.title.setText(title); ea.v.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { delete(v); } }); data.add(new EditBean(title, "000", ea.v)); fl.addView(ea.v, n - 1); count++; } /** @category 删除一个元素 */ public void delete(View v) { EditBean removeItem = null; for (EditBean b : data) { if (v.equals(b.getView())) { removeItem = b; } } // TODO List在遍历时不能删除Item,只有拿到for之外删除了 if (removeItem != null) { data.remove(removeItem); } System.out.println("del element:" + v.toString()); count--; fl.removeView(v); } /** @category 删除最后一个元素 */ public void delete() { System.out.println("Now Press Key is Delete."); String content = edit.getText().toString(); System.out.println("Now content is:" + content); System.out.println("Now content lenght is:" + content.length()); int flSize = fl.getChildCount(); if (flSize > 1 && "".equals(content)) { if (delMode) { delMode = false; System.out.println("Now delete the last item."); fl.removeViewAt(flSize - 2); count--; if (data.size() > 0) data.remove(data.size() - 1); } else { View v = fl.getChildAt(flSize - 2); v.setBackgroundColor(Color.parseColor("#ff0000")); delMode = true; } } } /** @category 元素界面容器 */ class Editable { Context context; public View v; public TextView title, delete; public Editable(Context context) { this.context = context; init(); } private void init() { v = LayoutInflater.from(context).inflate(R.layout.item_editable, null); title = (TextView) v.findViewById(R.id.title); delete = (TextView) v.findViewById(R.id.delete); } } /** @category 元素数据Bean */ class EditBean { public EditBean(String name, String cell, View view) { setName(name); setCell(cell); setView(view); } private String name; private View view; public View getView() { return view; } public void setView(View view) { this.view = view; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCell() { return cell; } public void setCell(String cell) { this.cell = cell; } private String cell; } }
试试其它关键字
联系人
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3