一、Edittext输入限制
添加TextWatcher能够实现监听Edittext中的内容输入。而在实际中,常需要区分汉字和英文字符。一个英文字符占一个长度,一个汉字占两个长度。为实现这个需求,定制监听类工具。
EditTextUtil.java
public class EditTextUtil {
public interface EditTextWatcher {
public void afterChanged();
}
/**
* 中英文混合时的长度控制,可实时显示剩余字符数,中文占两个字符。剩余字符数是按中文数展示的
*/
public void setTextLimit(final Context context, final EditText edit, final String alertStr, final int maxLength, final TextView textLeftCount, final EditTextWatcher watcher) {
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null) {
String value = s.toString();
int nowLength = length(value);
if (textLeftCount != null) {
Log.i("", "leftCount=" + (maxLength - nowLength) / 2);
textLeftCount.setTextColor(((maxLength - nowLength) / 2 <= 0) ? Color.RED : 0xFF999999);
textLeftCount.setText("" + (maxLength - nowLength) / 2);
}
if (nowLength > maxLength) {
String str = limitString(value, maxLength);
Log.v("", "new value=" + str);
edit.setText(str);
edit.setSelection(str.length());
if (alertStr != null) {
Toast.makeText(context, alertStr, Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (watcher != null) {
watcher.afterChanged();
}
}
});
}
/**
* 仅有英文时的长度控制,可实时显示剩余字符数
*/
public void setTextLimitOnlyCharEn(final Context context, final EditText edit, final String alertStr, final int maxLength, final TextView textLeftCount, final EditTextWatcher watcher) {
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null) {
String value = s.toString();
int nowLength = length(value);
if (textLeftCount != null) {
Log.e("", "leftCount=" + (maxLength - nowLength));
textLeftCount.setTextColor(((maxLength - nowLength) <= 0) ? Color.RED : 0xFF999999);
textLeftCount.setText("" + (maxLength - nowLength));
}
if (nowLength > maxLength) {
String str = limitString(value, maxLength);
Log.v("", "new value=" + str);
edit.setText(str);
edit.setSelection(str.length());
Toast.makeText(context, alertStr, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (watcher != null) {
watcher.afterChanged();
}
}
});
}
/**
* 获得字节数
*/
private static int length(String value) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
/* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
for (int i = 0; i < value.length(); i++) {
/* 获取一个字符 */
String temp = value.substring(i, i + 1);
/* 判断是否为中文字符 */
if (temp.matches(chinese)) {
/* 中文字符长度为2 */
valueLength += 2;
} else {
/* 其他字符长度为1 */
valueLength += 1;
}
}
return valueLength;
}
/**
* 取前20个字节的字符串
*/
private static String limitString(String value, int maxLenght) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
/* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
for (int i = 0; i < value.length(); i++) {
/* 获取一个字符 */
String temp = value.substring(i, i + 1);
/* 判断是否为中文字符 */
if (temp.matches(chinese)) {
/* 中文字符长度为2 */
valueLength += 2;
} else {
/* 其他字符长度为1 */
valueLength += 1;
}
if (valueLength > maxLenght) {
return value.substring(0, i);
}
}
return "";
}
}
使用布局文件:
使用代码:
titleDescET = (EditText) findViewById(R.id.title_desc_tv);
titleDescLimitTV = (TextView) findViewById(R.id.title_desc_tv_limit);
new EditTextUtil().setTextLimit(MainActivity.this, titleDescET,
getString(R.string.title_limit_desc), 20, null, new EditTextUtil.EditTextWatcher() {
@Override
public void afterChanged() {
int length = titleDescET.getText().toString().trim().length();
titleDescLimitTV.setText(length + "");
}
});
二、获取控件大小
drawTestIV = (ImageView) findViewById(R.id.draw_test_iv);
第一种方式:
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
drawTestIV.measure(w, h);
wrigintHeight = drawTestIV.getMeasuredHeight();
wrigintWidth = drawTestIV.getMeasuredWidth();
ViewTreeObserver vto = drawTestIV.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
wrigintHeight = drawTestIV.getMeasuredHeight();
wrigintWidth = drawTestIV.getMeasuredWidth();
return true;
}
});
第三种方式:
ViewTreeObserver vto2 = drawTestIV.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
drawTestIV.getViewTreeObserver().removeGlobalOnLayoutListener(this);
wrigintHeight = drawTestIV.getMeasuredHeight();
wrigintWidth = drawTestIV.getMeasuredWidth();
}
});
三、绘制背景图
依据以上控件及控件大小,为控件绘制“会”+“米”结构背景图。原始资料有对应“回”、“米”背景图。当然,整个背景图可直接使用切图方式实现。
try {
if (wrigintHeight <= 0 || wrigintWidth <= 0) {
wrigintHeight = drawTestIV.getHeight();
wrigintWidth = drawTestIV.getWidth();
}
Bitmap mBitmapCanvasGrid = Bitmap.createBitmap(wrigintWidth, wrigintHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(mBitmapCanvasGrid);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
/**
* 绘制 “回”字型
*/
Bitmap lines = BitmapUtil.getBitmapBySize(R.mipmap.writing_demopath_bg_grid, wrigintWidth, wrigintHeight);
c.drawBitmap(lines, 0, 0, paint);
lines.recycle();
/**
* 绘制“米”字型
*/
lines = BitmapUtil.getBitmapBySize(R.mipmap.writing_demopath_bg, wrigintWidth, wrigintHeight);
c.drawBitmap(lines, 0, 0, paint);
lines.recycle();
c.save();
c.restore();
drawTestIV.setBackgroundDrawable(new BitmapDrawable(mBitmapCanvasGrid));
} catch (Exception e) {
e.printStackTrace();
}
四、EditText键盘关联
存在一些需求,进入某一个页面,则需要实现,Edittext获取焦点,并自动弹出键盘。
1,设置Edittext属性:
testET.setFocusable(true);
testET.setFocusableInTouchMode(true);
testET.requestFocus();
InputMethodManager inputManager = (InputMethodManager) testET.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(testET, 2);
testET.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
InputMethodManager manager = (InputMethodManager) testET.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(testET, 0);
}
});
4,3的方法能够让键盘正常弹出。
由于EditText的焦点并没有修改,当点击其他位置时,会出现键盘隐藏又马上显示出来。作如下修改:
testET.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) testET.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(testET, 0);
}
}, 200);
/**
* 输入模式管理器--控制键盘自动收缩
*/
private InputMethodManager manager = null;
manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (getCurrentFocus() != null
&& getCurrentFocus().getWindowToken() != null) {
manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return super.onTouchEvent(event);
}
startActivity(new Intent(A.this, B.class));
overridePendingTransition(R.anim.left_out, R.anim.right_in);
关闭时,直接在finish()后使用以上动画即可。
点击返回按钮时,需要将动画添加到:onPause()方法中。
在Activity执行动画中,可能存在黑屏的可能。需要修改Activity的theme主题:
其中,AppTheme作如下变化:
以下部分为添加部分:
- true
则能实现平缓的动画效果!
源码
“真正的勤奋,不是被迫的机械重复劳动,也不是自我感动式的摧残健康,更不是因为拖延症导致的最后一刻效率爆发。它来自于一个人内心深处,对与那些无法获得即刻回报的事情,依然能够保持十年如一的热情与专注。”