首页 > 学院 > 开发设计 > 正文

EditText的常用属性和实例

2019-11-09 14:57:47
字体:
来源:转载
供稿:网友
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"首先创建布局文件。代码:<RelativeLayoutxmlns: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"><EditTextandroid: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"/><TextViewandroid: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"/><Buttonandroid: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"/><TextViewandroid: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;@Overrideprotected 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() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic 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的代码:<EditTextandroid: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;@Overrideprotected 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() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic 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("");}}});}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表