一、EditText介紹
①EditText是一個輸入框,在Android開發(fā)中是常用的控件。也是獲取用戶數據的一種方式。
②EditText是TextView的子類,它繼承了[TextView]的所有屬性。
二、常用屬性
| 屬性 | 介紹 |
|---|---|
android:background="@drawable/bg" |
設置背景(如果背景透明,則將@drawable/bg改為@null) |
android:layout_marginRight="50dp" |
設置外間距 |
android:paddingLeft="80dp" |
設置內間距 |
android:textColor = "#ff8c00" |
設置字體顏色 |
android:textSize="30sp" |
設置字體大小 |
android:hint="請輸入密碼" |
設置顯示在空間上的提示信息 |
android:textColorHint="#999999" |
設置提示文本的顏色(默認為灰色) |
android:lines=“1” |
設置文本的行數,設置兩行就顯示兩行,即使第二行沒有數據 |
android:maxLines="1" |
設置文本的最大顯示行數,與width或者layout_width結合使用,超出部分自動換行,超出行數將不顯示 |
android:minLines=“1” |
設置文本的最小行數,與lines類似 |
android:inputType="textPassword" |
設置文本的類型,用于幫助輸入法顯示合適的鍵盤類型(此處鍵盤輸出類型為“.....”) |
android:maxLength="6" |
限制顯示的文本長度,超出部分不顯示 |
android:imeOptions="actionGo" |
附加功能,設置右下角IME動作與編輯框相關的動作,如actionDone右下角將顯示一個“完成”,而不設置默認是一個回車符號 |
android:textAlign="center" |
EditText沒有這個屬性,但TextView有,居中 |
android:singleLine="true" |
設置單行輸入,一旦設置為true,則文字不會自動換行 |
android:password="true" |
設置只能輸入密碼 |
android:editable="true" |
設置是否可編輯,true:可編輯 false:不可編輯 |
android:cursorVisible=false |
設置光標顯示(true)/隱藏(false) |
三、常用方法
1.文本監(jiān)聽事件
editText.addTextChangedListener(new TextWatcher() {
2 @Override
3 public void onTextChanged(CharSequence text, int start, int before, int count) {
4 //text 輸入框中改變后的字符串信息
5 //start 輸入框中改變后的字符串的起始位置
6 //before 輸入框中改變前的字符串的位置 默認為0
7 //count 輸入框中改變后的一共輸入字符串的數量
8
9 }
10
11 @Override
12 public void beforeTextChanged(CharSequence text, int start, int count,int after) {
13 //text 輸入框中改變前的字符串信息
14 //start 輸入框中改變前的字符串的起始位置
15 //count 輸入框中改變前后的字符串改變數量一般為0
16 //after 輸入框中改變后的字符串與起始位置的偏移量
17
18 }
19
20 @Override
21 public void afterTextChanged(Editable edit) {
22 //edit 輸入結束呈現在輸入框中的信息
23
24 }
25 });
使用:實現文本輸入個數只有6個
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//獲取目前輸入的個數
int len=charSequence.toString().length();
if(len>6){
//將最后一個刪除掉
//只要前面6個
et.setText(charSequence.subSequence(0,6));
//讓光標定到最后
et.setSelection(6);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
image.png
2.監(jiān)聽鍵盤事件的集中方式
-
1.創(chuàng)建匿名類對象
1.1創(chuàng)建一個類管理事件
PXDListener pl = new PXDListener();
et.setOnEditorActionListener(pl);
//或
et.setOnEditorActionListener(new PXDListener());
//創(chuàng)建一個類 管理事件的回調
class PXDListener implements TextView.OnEditorActionListener{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
System.out.println("點擊了");
return false;
}
}
1.2 匿名類對象
et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
et.getText();
return false;
}
});
1.3 Lambda表達式
et.setOnEditorActionListener((textView, actionId, event) -> {
System.out.println("點擊了");
return true;
});
-
2.當前這個Activity來監(jiān)聽事件
public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et.setOnEditorActionListener(this);
}
//鍵盤被按下的回調事件
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
System.out.println("點擊了");
return false;
}
}
四、練習-PIN解鎖Demo
完整demo
Xml搭建界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--添加背景圖片 -->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
/>
<!--文本提示框-->
<TextView
android:id="@+id/tv_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請設置密碼"
android:textColor="@color/colorGray"
android:textSize="@dimen/dimen_textView"
android:textAlignment="center"
android:layout_marginTop="80dp"/>
<!--添加文本輸入框-->
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/unlock"
android:layout_centerHorizontal="true"
android:focusedByDefault="@id/tv_alert"
android:layout_marginTop="30dp"
android:paddingLeft="50dp"
android:textColor="@color/colorGray"
android:textSize="@dimen/dimen_textView"
android:inputType="textPassword"
android:maxLength="6"
android:maxLines="1"
android:cursorVisible="false"
android:letterSpacing="0.5"
/>
</RelativeLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private EditText mEditText;
private String password;
private String firstInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取xml的控件
mTextView=findViewById(R.id.tv_alert);
mEditText=findViewById(R.id.et_password);
Resources res=getResources();
//通過這個對象獲取string.xml里面對應的字符串
String fileName=res.getString(R.string.password_file_name);
//獲取共享的sp對象:1.文件不存在就創(chuàng)建 2.文件存在就打開
final SharedPreferences sp=getSharedPreferences(fileName,MODE_PRIVATE);
//通過key獲取對應的value
password=sp.getString("pwd",null);
//顯示提示文本
if(password==null){
mTextView.setText("請設置密碼");
}else{
mTextView.setText("請輸入密碼");
}
//監(jiān)聽內容改變的事件
//觀察者
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//獲取文本的內容
String inputPassword=editable.toString();
//判斷是不是6個
if(inputPassword.length()==6)
{
if(password==null){
//設置密碼
if(firstInput==null){
//設置密碼的第一次
firstInput=inputPassword;
//提示請確認密碼
mTextView.setText("請確認密碼");
//清空密碼
mEditText.setText("");
}else{
//確認密碼
if(firstInput.equals(password)){
//密碼正確
mTextView.setText("設置密碼成功");
//保存密碼
SharedPreferences.Editor edit=sp.edit();
edit.putString("pwd",firstInput);
edit.commit();
goToNext();
}else{
//密碼不正確
mTextView.setText("兩次密碼不一致 請重新設置");
firstInput = null;
mEditText.setText("");
}
}
}else{
//密碼設置過了
if(inputPassword.equals(password)){
//密碼正確
mTextView.setText("密碼正確!!");
}else{
//不正確
mTextView.setText("密碼錯誤請重新輸入");
//清空
mEditText.setText(" ");
}
}
}
}
});
}
運行效果

20190908_194523.gif
步驟解析
-
導入圖片素材
image.png -
colors.xml管理字體顏?
image.png dimens.xml管理尺寸數據-(在value里面新建一個dimens.xml管理尺寸數據)
新建dimens.xml
values->new->Values resource file->設置filename為dimens
image.png
image.png
- 設置鍵盤?直顯示-(PIN解鎖界面中,當輸入密碼時,會有鍵盤彈出,但是要在點擊一下界面后鍵盤才會出現,現要求鍵盤一直顯示,不需要點擊后再顯示)
在
manifests的AndroidManifest.xml里面添加android:windowSoftInputMode="stateAlwaysVisible"
image.png
思考:如何在密碼輸入成功(即登錄成功)后跳轉到另一個界面,并從另一個界面跳轉回去。^^
- 1.創(chuàng)建下一個界面
創(chuàng)建一個新的界面
在java的第一個包里面鼠標右擊新建->Activity->Empty Activity->設置一個Activity Name
image.png
- 2.設置第二個界面的背景
image.png
- 3.在MainActivity里面實現界面跳轉
- 4.點擊屏幕實現返回
image.png
運行效果:
20190908_202133.gif
心得體會
今天上午沒有拿眼鏡,然后作為一個近視,很痛苦的上了一上午的課,然后上課就跟不上進度,難受。事實證明,要愛護好自己的視力











