一叹谁、EditText(輸入框)
EditText和TextView非常類似鞭衩,都是用于顯示文本控件敲茄,最大的區(qū)別就是EditText可以接收用戶的輸入充包。下面就來看看EditText的使用和它的一些屬性副签。
<EditText
android:layout_width="450dp"
android:layout_height="70dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="150dp"
android:background="@drawable/unclockpin"
android:hint="請輸入密碼"
android:textColorHint="#999999"
android:paddingLeft="70dp"
android:textColor="#ffffff"
android:textSize="20dp"
android:maxLines="1"
android:inputType="textPassword"
android:maxLength="6"
android:letterSpacing="0.6"
/>
代碼解釋:
1,代碼首先在activity_main.xml
文件里面添加一個EditText控件基矮,和其他控件一樣可以設置輸入框的大小和位置還可以設置背景顏色或者添加背景圖片淆储;其中:
android:hint="請輸入密碼"
android:textColorHint="#999999"
是默認提示文本的兩個控制屬性,前者設置默認提示文本的內容家浇,后者設置提示文本的顏色本砰。
2,有時候我們需要對輸入的數據進行限制:
android:maxLines="1"
android:inputType="textPassword"
android:maxLength="6"
android:letterSpacing="0.6"
第一行代碼設置輸入的數據只能是一行顯示钢悲,不能換行点额;第二行代碼設置我們輸入的數據是一串密碼,這樣輸入字符后莺琳,字符會自動變成原點还棱;第三行是限制只能輸入六個字符;最后一行代碼設置輸入的字符之間的距離惭等。下面是一些常用的設置輸入框的屬性:
margin:設置和父容器的內間距
padding:設置控件的內間距
paddingLeft 設置光標位置
hint:設置默認提示文本
maxLines:設置輸入的文本不換行
inputType:設置輸入內容 可以調用對應的鍵盤
cursorVisible:設置光標是否可見
minLines:設置最小行數
maxLines:設置最大行數
PS:當輸入的內容超過maxLine珍手,文字會自動向上滾動。
如果想要進入界面時,鍵盤一直顯示琳要,則可以在 ActivityManifest.xml
配置文件里添加一句:
android:windowSoftInputMode="stateAlwaysVisible"
這樣鍵盤就會在進入界面時一直顯示了寡具。
補充知識:
dp: device independent pixels(設備獨立像素). 不同設備有不同的顯示效果,這個和設備硬件有關,一般我們?yōu)榱酥С諻VGA焙蹭、HVGA和QVGA 推薦使用這個晒杈,不依賴像素。
px:pixels(像素). 不同設備顯示效果相同孔厉,一般我們HVGA代表320x480像素拯钻,這個用的比較多。
pt:point撰豺,是一個標準的長度單位粪般,1pt=1/72英寸,用于印刷業(yè)污桦,非常簡單易用亩歹;
sp: scaled pixels(放大像素). 主要用于字體顯示best for textsize。
二凡橱、PIN解鎖項目
一小作、要求:
實現手機PIN解鎖功能,包括設置密碼六位密碼稼钩,成功解鎖等功能顾稀。
二、技術難點:
監(jiān)聽鍵盤key按下的事件:
1坝撑,創(chuàng)建匿名類對象:匿名類的匿名對象
2静秆,讓當前這個類Activity來監(jiān)聽事件
3,專門寫一個類來監(jiān)聽這個事件 創(chuàng)建一個對象巡李;創(chuàng)建一個匿名對象
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
System.out.println("點擊了");
return false;
}
});
4抚笔,Lambda表達式
editText.setOnAction.listener((textView,actionId,event) -> {
System.out.println("點擊了");
return true;
});
三、代碼實現
1侨拦,activtiy_main.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<!--添加背景圖片-->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:visibility="visible"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg"
android:visibility="visible"/>
<!--輸入框殊橙,用于接收用戶的輸入-->
<EditText
android:id="@+id/et_password"
android:layout_width="450dp"
android:layout_height="70dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="150dp"
android:background="@drawable/unclockpin"
android:hint="請輸入密碼"
android:paddingLeft="70dp"
android:textColor="#ffffff"
android:textColorHint="#999999"
android:textSize="20dp"
android:maxLines="1"
android:inputType="textPassword"
android:maxLength="6"
android:letterSpacing="0.6"
/>
<!--文本提示框-->
<TextView
android:id="@+id/tv_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:text="請設置密碼"
android:textAlignment="center"
android:textColor="@color/colorGray"
android:textSize="@dimen/dimen_alert"
android:visibility="visible"/>
</RelativeLayout>
2,MainActivity.java
文件
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
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 textView;
private EditText editText;
private String password;
private String firstInput;
//1.xml配置文件
//2.使用代碼創(chuàng)建
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通過id獲取xml文件里對應的控件
editText = findViewById(R.id.et_password);
textView = findViewById(R.id.tv_alert);
//獲取保存的密碼
//獲取管理資源對象
Resources resources = getResources();
//通過這個對象獲取string.xml里面對應的字符串
String fileName = resources.getString(R.string.password_file_name);
//獲取共享的SharedPreferences對象:1阳谍,文件存在就打開蛀柴,不存在就創(chuàng)建
final SharedPreferences sharedPreferences = getSharedPreferences(fileName,MODE_PRIVATE);
//通過key獲取對應的value
password = sharedPreferences.getString("pwd",null);
//顯示提示文本
if(password == null){
textView.setText("請設置密碼");
}else{
textView.setText("請輸入密碼");
}
//監(jiān)聽文本內容改變的事件
editText.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) {
//獲取目前輸入的個數
int length = s.toString().length();
if(length > 6){
//獲取前六個
editText.setText(s.subSequence(0,6));
//將光標定位到最后
editText.setSelection(6);
}
}
@Override
public void afterTextChanged(Editable s) {
//獲取文本內容
String inputPassword = s.toString();
//判斷是不是6個
if(inputPassword.length() == 6){
//判斷是不是設置密碼
if(password == null){
//設置密碼
if(firstInput == null){
//設置輸入密碼的第一次
firstInput = inputPassword;
//提示確認密碼
textView.setText("請確認密碼");
//清空輸入內容
editText.setText("");
}else {
if(firstInput.equals(inputPassword)){
//兩次密碼一致
textView.setText("設置密碼成功");
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("pwd",firstInput);
editor.commit();
}else{
//密碼不正確
textView.setText("兩次密碼不一致,請重新設置");
firstInput = null;
editText.setText("");
}
}
}else{
//密碼設置過了
if(inputPassword.equals(password)){
//密碼正確
textView.setText("密碼正確");
}else{
//密碼不正確
textView.setText("密碼錯誤矫夯,請重新輸入");
//清空
editText.setText("");
}
}
}
}
});
}
}
PS:
1鸽疾,在values文件夾下面新建一個dimens.xml
文件,在里面配置輸入字符串的大小
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dimen_alert">20sp</dimen>
<dimen name="dimen_textView">20sp</dimen>
</resources>
2训貌,其中TextWatcher()用法:
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//文本改變前
}
其中有4個參數:
CharSequence s:文本改變之前的內容
int start : 被替換文本區(qū)域起點位置
int count:將被替換的文本區(qū)域字符數目
-
int after:替換后的文本字符數目
文本 s 中的 start 位置之后的 count 個字符將被替換為 after 個新的字符
注:s為替換前的文本@Override public void onTextChanged(CharSequence s, int start, int before, int count) { //文本改變時 }
其中有4個參數:
CharSequence s:文本改變之后的內容
int start : 被替換文本區(qū)域起點位置,setText時是替換所有內容,此時數值為0
int before:被替換之前的文本區(qū)域字符數目
-
int count:替換后的文本字符數目
文本 s 中的 start 位置之后的 before 個字符已經被替換為 count 個新的字符
注:s為替換后的新文本@Override
public void afterTextChanged(Editable s) {
// 文本改變后
}
其中1個參數: Editable s:文本改變之后的內容
三制肮、學習感悟
學習這些小控件的使用是為了以后的開發(fā)做準備冒窍,然后寫的這個PIN解鎖項目,即練習了新學的輸入框豺鼻,又能夠復習前面學過的SharedPreferences保存用戶的密碼综液,雖然之前已經學過了用戶偏好的方式保存密碼,但再一次寫的時候還是有點陌生儒飒,還是要多寫多練才行谬莹。