Android开发-EditTextDemo-AndroidStudio
2016-10-24 15:10
423 查看
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下面是一个EditText,EditText是TextView的一个子类,TextView的所有属性EditText都适用" />
<EditText
android:inputType="textPassword"
android:hint="请输入密码"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="0.3dp"
android:background="#D6D6D6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下面是一个EditText,用java控制输入规则,只允许字母、数字、汉字" />
<EditText
android:id="@+id/editText_1"
android:hint="请输入字母、数字、汉字"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="0.3dp"
android:background="#D6D6D6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下面是一个EditText,用java控制输入规则,只允许字母、数字、汉字、-"
android:id="@+id/textView_2" />
<EditText
android:id="@+id/editText_2"
android:hint="请输入字母、数字、汉字、-"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="0.3dp"
android:background="#D6D6D6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下面是一个EditText,每新建一行则自动生成一条下划线"
android:id="@+id/textView_3" />
<com.iwanghang.edittextdemo.UnderLineEditText
android:id="@+id/editText_3"
android:background="@null"
android:hint="请输入"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java:
package com.iwanghang.edittextdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 只允许字母、数字和汉字
*/
final EditText editText_1 = (EditText) findViewById(R.id.editText_1);
editText_1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG,"beforeTextChanged--------");
Log.d(TAG,"beforeTextChanged:" + s);
Log.d(TAG,"beforeTextChanged:" + start);
Log.d(TAG,"beforeTextChanged:" + count);
Log.d(TAG,"beforeTextChanged:" + after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG,"onTextChanged--------");
Log.d(TAG,"onTextChanged:" + s);
Log.d(TAG,"onTextChanged:" + start);
Log.d(TAG,"onTextChanged:" + before);
Log.d(TAG,"onTextChanged:" + count);
String editable = editText_1.getText().toString();
String str = stringFilter(editable.toString());
if(!editable.equals(str)){
editText_1.setText(str);
//设置新的光标所在位置
editText_1.setSelection(str.length());
}
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG,"afterTextChanged--------");
Log.d(TAG,"afterTextChanged:" + s);
}
});
/**
* 只允许字母、数字和汉字-
*/
final EditText editText_2 = (EditText) findViewById(R.id.editText_2);
editText_2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG,"beforeTextChanged--------");
Log.d(TAG,"beforeTextChanged:" + s);
Log.d(TAG,"beforeTextChanged:" + start);
Log.d(TAG,"beforeTextChanged:" + count);
Log.d(TAG,"beforeTextChanged:" + after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG,"onTextChanged--------");
Log.d(TAG,"onTextChanged:" + s);
Log.d(TAG,"onTextChanged:" + start);
Log.d(TAG,"onTextChanged:" + before);
Log.d(TAG,"onTextChanged:" + count);
String editable = editText_2.getText().toString();
String str = stringFilter2(editable.toString());
if(!editable.equals(str)){
editText_2.setText(str);
//设置新的光标所在位置
editText_2.setSelection(str.length());
}
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG,"afterTextChanged--------");
Log.d(TAG,"afterTextChanged:" + s);
}
});
}
/**
* 匹配正则表达式
* 只允许字母、数字和汉字
*/
public static String stringFilter(String str)throws PatternSyntaxException {
// 只允许字母、数字和汉字
String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
/**
* 匹配正则表达式
* 只允许字母、数字和汉字-
*/
public static String stringFilter2(String str)throws PatternSyntaxException {
// 只允许字母、数字和汉字-
String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5\\-]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
}
UnderLineEditText.java:
package com.iwanghang.edittextdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* 方法来自:重写EditText,使其具有下划线:http://blog.csdn.net/harvic880925/article/details/24416131
*/
public class UnderLineEditText extends EditText {
private Paint linePaint;
private int paperColor;
public UnderLineEditText(Context context,AttributeSet paramAttributeSet) {
super(context,paramAttributeSet);
// TODO Auto-generated constructor stub
this.linePaint = new Paint();
linePaint.setColor(Color.GRAY);//设置下划线颜色
}
protected void onDraw(Canvas paramCanvas) {
paramCanvas.drawColor(this.paperColor); //设置背景色
int i = getLineCount();
int j = getHeight();
int k = getLineHeight();
int m = 1 + j / k;
if (i < m) i = m;
int n = getCompoundPaddingTop();
for (int i2 = 0;; i2++) {
if (i2 >= i) {
super.onDraw(paramCanvas);
paramCanvas.restore();
return;
}
n += k;
paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);
paramCanvas.save();
}
}
}
相关文章推荐
- Android开发-EditTextDemo-AndroidStudio
- Android开发-DesignDemo-AndroidStudio(十二)TextInputLayout
- Android开发-TextViewDemo-AndroidStudio
- Android开发-TextViewDemo-AndroidStudio
- Android开发-DesignDemo-AndroidStudio(十二)TextInputLayout
- android开发之EditText组件
- Android开发技巧:editText 像QQ一样输入表情图像
- 在Android开发中如何移除EditText上的输入焦点
- android开发之EditText的属性说明
- Android软件开发之EditText 详解(八)
- Android开发开发技巧之EditText 属性大全
- android开发之EditText的属性说明
- Android软件开发之EditText 详解(八)
- Android软件开发之EditText 详解(八)
- Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法
- Android获取通讯录联系人显示在EditText上Demo
- Android手机开发:EditText+DatePicker带日期选择器的编辑框
- Android软件开发之EditText 详解(五)
- Android开发学习笔记:如何移除EditText上的输入焦点
- android开发之设置Edittext密码的方法