一、常用到的属性:
android:hint:文本提示信息
android:maxLength:控制EditText字符数
android:inputType:限制输入的类型
二、文本监听事件
获取到的EditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d("before", "输入之前字符串为:" + s.toString() + ",从下标为:[" + start + "]开始操作,操作的数量为:" + after); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("on", "输入后的字符串为: " + s.toString() + " ,从下标为: [ " + start + " ]开始操作,操作的数量为: " + count); } @Override public void afterTextChanged(Editable s) { Log.d("after", "输入结束后的字符串为 :" + s.toString() + "\n--------------------------------------------"); } });
三、问题解决:
在app目录下的build.gradle文件里面添加两行代码:
aaptOptions.cruncherEnabled =false aaptOptions.useNewCruncher =false
四、练习
(1)布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="15dp"> <EditText android:id="@+id/txt01" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/bg_txt" android:hint="请输入用户名" android:paddingLeft="10dp" android:maxLines="1" android:maxLength="30" android:singleLine="true" android:textColor="#ed1941"/> <EditText android:singleLine="true" android:id="@+id/txt02" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@+id/txt01" android:textColor="#ed1941" android:inputType="textPassword" android:background="@drawable/bg_txt" android:hint="请输入密码" android:paddingLeft="10dp" android:maxLines="1" android:layout_marginTop="10dp"/> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_below="@+id/txt02" android:layout_height="50dp" android:layout_marginTop="10dp" android:text="登录" android:textSize="20sp" android:background="@drawable/bg_btn" android:textColor="#ffffff"/> </RelativeLayout>
(2)Activity文件:
package com.example.lqh.firstproctect; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText mTxt01; private EditText mTxt02; private Button mBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTxt01 = (EditText) findViewById(R.id.txt01); mTxt02 = (EditText) findViewById(R.id.txt02); Drawable drawable = getResources().getDrawable(R.drawable.user); drawable.setBounds(0, 0, 50, 50); mTxt01.setCompoundDrawables(drawable, null, null, null); drawable = getResources().getDrawable(R.drawable.pass); drawable.setBounds(0, 0, 50, 50); mTxt02.setCompoundDrawables(drawable, null, null, null); mBtn = (Button) findViewById(R.id.btn); mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "登录成功!", Toast.LENGTH_SHORT).show(); } }); mTxt01.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d("before", "输入之前字符串为:" + s.toString() + ",从下标为:[" + start + "]开始操作,操作的数量为:" + after); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("on", "输入后的字符串为: " + s.toString() + " ,从下标为: [ " + start + " ]开始操作,操作的数量为: " + count); } @Override public void afterTextChanged(Editable s) { Log.d("after", "输入结束后的字符串为 :" + s.toString() + "\n--------------------------------------------"); } }); } }
(3)新建drawable-xxhdpi文件夹,图片放里面:
(4)样式文件:
bg_btn.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <shape> <solid android:color="#007d65" /> <corners android:radius="15dp" /> </shape> </item> <item android:state_pressed="true"> <shape> <solid android:color="#225a1f" /> <corners android:radius="15dp" /> </shape> </item> </selector>
bg_txt.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="2dp" android:color="#007d65"/> <corners android:radius="10dp"/> </shape>
(5)效果图: