EditText的常用属性
android:text 设置显示在EditText中的内容
android:maxLength 用来限制EditText中可以输入的字符个数
android:hint 设置显示在EditText上的提示信息
android:numeric 设置编辑框中只能输入数字
android:password 设置EditText以输入密码模式来显示
android:lines 设置EditText中可以输入的字符行数
Android:digits 设置只能输入的内容
给EditText添加监听:
mEditText.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) {
}
@Override
//当文本改变后被调用
public void afterTextChanged(Editable s) {
}
EditText实例1:
在EditText中最多只能输入30个字符,当到第30个字符时提示"文字不能超过30"
首先创建布局文件。
代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zhiyuan3g.edittextlx.Main2Activity">
<EditText
android:maxLength="30"
android:layout_width="wrap_content"
android:layout_height="100sp"
android:id="@+id/editText2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"/>
<TextView
android:textSize="35sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0/30"
android:id="@+id/textView"
android:layout_below="@+id/editText2"
android:layout_alignParentStart="true"/>
<Button
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发表"
android:id="@+id/button"
android:layout_below="@+id/editText2"
android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"/>
</RelativeLayout>
在java文件中:
public class Main2Activity extends AppCompatActivity {
private Button btn;
private EditText mEditText;
private TextView mTextView,mTextView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
btn = (Button) findViewById(R.id.button);
mEditText = (EditText) findViewById(R.id.editText2);
mTextView = (TextView) findViewById(R.id.textView);
mTextView2 = (TextView) findViewById(R.id.textView2);
mEditText.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) {
}
@Override
public void afterTextChanged(Editable s) {
mTextView.setText(s.toString().length()+"/30");
if(s.toString().length()==30){
mTextView2.setText("文字不能超过30");
}else{
mTextView2.setText("");
}
}
});
}
}
EditText实例2:
EditText中可以输入多个字符,但是当超过30时,发表按钮不能点击
布局中EditText的代码:
<EditText
android:layout_width="wrap_content"
android:layout_height="100sp"
android:id="@+id/editText2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"/>
在java文件中的代码:
public class Main2Activity extends AppCompatActivity {
private Button btn;
private EditText mEditText;
private TextView mTextView,mTextView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
btn = (Button) findViewById(R.id.button);
mEditText = (EditText) findViewById(R.id.editText2);
mTextView = (TextView) findViewById(R.id.textView);
mTextView2 = (TextView) findViewById(R.id.textView2);
mEditText.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) {
}
@Override
public void afterTextChanged(Editable s) {
mTextView.setText(s.toString().length()+"/30");
if(s.toString().length()>30){
//按钮处于不可点击状态
btn.setEnabled(false);
mTextView2.setText("文字不能超过30");
}else{
//按钮处于可点击状态
btn.setEnabled(true);
mTextView2.setText("");
}
}
});
}
}