PIN解鎖項(xiàng)目

一、EditText(輸入框)

EditText和TextView非常類似,都是用于顯示文本控件,最大的區(qū)別就是EditText可以接收用戶的輸入。下面就來(lái)看看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="請(qǐng)輸入密碼"
    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文件里面添加一個(gè)EditText控件,和其他控件一樣可以設(shè)置輸入框的大小和位置還可以設(shè)置背景顏色或者添加背景圖片;其中:

 android:hint="請(qǐng)輸入密碼"
android:textColorHint="#999999"

是默認(rèn)提示文本的兩個(gè)控制屬性,前者設(shè)置默認(rèn)提示文本的內(nèi)容,后者設(shè)置提示文本的顏色。
2,有時(shí)候我們需要對(duì)輸入的數(shù)據(jù)進(jìn)行限制:

    android:maxLines="1"
    android:inputType="textPassword"
    android:maxLength="6"
   android:letterSpacing="0.6"

第一行代碼設(shè)置輸入的數(shù)據(jù)只能是一行顯示,不能換行;第二行代碼設(shè)置我們輸入的數(shù)據(jù)是一串密碼,這樣輸入字符后,字符會(huì)自動(dòng)變成原點(diǎn);第三行是限制只能輸入六個(gè)字符;最后一行代碼設(shè)置輸入的字符之間的距離。下面是一些常用的設(shè)置輸入框的屬性:

margin:設(shè)置和父容器的內(nèi)間距
padding:設(shè)置控件的內(nèi)間距
paddingLeft 設(shè)置光標(biāo)位置
hint:設(shè)置默認(rèn)提示文本
maxLines:設(shè)置輸入的文本不換行
inputType:設(shè)置輸入內(nèi)容 可以調(diào)用對(duì)應(yīng)的鍵盤
cursorVisible:設(shè)置光標(biāo)是否可見
minLines:設(shè)置最小行數(shù)
maxLines:設(shè)置最大行數(shù)
PS:當(dāng)輸入的內(nèi)容超過(guò)maxLine,文字會(huì)自動(dòng)向上滾動(dòng)。

如果想要進(jìn)入界面時(shí),鍵盤一直顯示,則可以在 ActivityManifest.xml配置文件里添加一句:

    android:windowSoftInputMode="stateAlwaysVisible"

這樣鍵盤就會(huì)在進(jìn)入界面時(shí)一直顯示了。
補(bǔ)充知識(shí):

dp: device independent pixels(設(shè)備獨(dú)立像素). 不同設(shè)備有不同的顯示效果,這個(gè)和設(shè)備硬件有關(guān),一般我們?yōu)榱酥С諻VGA、HVGA和QVGA 推薦使用這個(gè),不依賴像素。
px:pixels(像素). 不同設(shè)備顯示效果相同,一般我們HVGA代表320x480像素,這個(gè)用的比較多。
pt:point,是一個(gè)標(biāo)準(zhǔn)的長(zhǎng)度單位,1pt=1/72英寸,用于印刷業(yè),非常簡(jiǎn)單易用;
sp: scaled pixels(放大像素). 主要用于字體顯示best for textsize。

二、PIN解鎖項(xiàng)目

一、要求:

實(shí)現(xiàn)手機(jī)PIN解鎖功能,包括設(shè)置密碼六位密碼,成功解鎖等功能。

二、技術(shù)難點(diǎn):

監(jiān)聽鍵盤key按下的事件:
1,創(chuàng)建匿名類對(duì)象:匿名類的匿名對(duì)象
2,讓當(dāng)前這個(gè)類Activity來(lái)監(jiān)聽事件
3,專門寫一個(gè)類來(lái)監(jiān)聽這個(gè)事件 創(chuàng)建一個(gè)對(duì)象;創(chuàng)建一個(gè)匿名對(duì)象

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
       @Override
       public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        System.out.println("點(diǎn)擊了");
           return false;
       }
   });

4,Lambda表達(dá)式

    editText.setOnAction.listener((textView,actionId,event) -> {
    System.out.println("點(diǎn)擊了");
    return true;
    });
三、代碼實(shí)現(xiàn)

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="請(qǐng)輸入密碼"
    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="請(qǐng)?jiān)O(shè)置密碼"
    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);
    //通過(guò)id獲取xml文件里對(duì)應(yīng)的控件
    editText = findViewById(R.id.et_password);
    textView = findViewById(R.id.tv_alert);
    //獲取保存的密碼
    //獲取管理資源對(duì)象
    Resources resources = getResources();
    //通過(guò)這個(gè)對(duì)象獲取string.xml里面對(duì)應(yīng)的字符串
    String fileName = resources.getString(R.string.password_file_name);
    //獲取共享的SharedPreferences對(duì)象:1,文件存在就打開,不存在就創(chuàng)建
    final SharedPreferences sharedPreferences = getSharedPreferences(fileName,MODE_PRIVATE);
    //通過(guò)key獲取對(duì)應(yīng)的value
    password = sharedPreferences.getString("pwd",null);
    //顯示提示文本
    if(password == null){
        textView.setText("請(qǐng)?jiān)O(shè)置密碼");
    }else{
        textView.setText("請(qǐng)輸入密碼");
    }
  //監(jiān)聽文本內(nèi)容改變的事件
 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) {

         //獲取目前輸入的個(gè)數(shù)
         int length = s.toString().length();

         if(length > 6){
             //獲取前六個(gè)
            editText.setText(s.subSequence(0,6));
            //將光標(biāo)定位到最后
             editText.setSelection(6);
         }
     }

     @Override
     public void afterTextChanged(Editable s) {
         //獲取文本內(nèi)容
         String inputPassword = s.toString();
         //判斷是不是6個(gè)
         if(inputPassword.length() == 6){
            //判斷是不是設(shè)置密碼
             if(password == null){
                 //設(shè)置密碼
                 if(firstInput == null){
                     //設(shè)置輸入密碼的第一次
                     firstInput = inputPassword;
                     //提示確認(rèn)密碼
                     textView.setText("請(qǐng)確認(rèn)密碼");
                     //清空輸入內(nèi)容
                     editText.setText("");
                 }else {
                     if(firstInput.equals(inputPassword)){
                         //兩次密碼一致
                         textView.setText("設(shè)置密碼成功");
                         SharedPreferences.Editor editor = sharedPreferences.edit();
                         editor.putString("pwd",firstInput);
                         editor.commit();
                     }else{
                         //密碼不正確
                         textView.setText("兩次密碼不一致,請(qǐng)重新設(shè)置");
                         firstInput = null;
                         editText.setText("");
                     }
                 }
             }else{
                 //密碼設(shè)置過(guò)了
                 if(inputPassword.equals(password)){
                     //密碼正確
                     textView.setText("密碼正確");

                 }else{
                     //密碼不正確
                     textView.setText("密碼錯(cuò)誤,請(qǐng)重新輸入");
                     //清空
                     editText.setText("");
                 }
             }
         }
     }
   });
  }
}

PS:
1,在values文件夾下面新建一個(gè)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個(gè)參數(shù):

  • CharSequence s:文本改變之前的內(nèi)容

  • int start : 被替換文本區(qū)域起點(diǎn)位置

  • int count:將被替換的文本區(qū)域字符數(shù)目

  • int after:替換后的文本字符數(shù)目
    文本 s 中的 start 位置之后的 count 個(gè)字符將被替換為 after 個(gè)新的字符
    注:s為替換前的文本

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    //文本改變時(shí)
    }
    

其中有4個(gè)參數(shù):

  • CharSequence s:文本改變之后的內(nèi)容

  • int start : 被替換文本區(qū)域起點(diǎn)位置,setText時(shí)是替換所有內(nèi)容,此時(shí)數(shù)值為0

  • int before:被替換之前的文本區(qū)域字符數(shù)目

  • int count:替換后的文本字符數(shù)目
    文本 s 中的 start 位置之后的 before 個(gè)字符已經(jīng)被替換為 count 個(gè)新的字符
    注:s為替換后的新文本

    @Override
    public void afterTextChanged(Editable s) {
    // 文本改變后
    }
    其中1個(gè)參數(shù):

  • Editable s:文本改變之后的內(nèi)容

三、學(xué)習(xí)感悟

學(xué)習(xí)這些小控件的使用是為了以后的開發(fā)做準(zhǔn)備,然后寫的這個(gè)PIN解鎖項(xiàng)目,即練習(xí)了新學(xué)的輸入框,又能夠復(fù)習(xí)前面學(xué)過(guò)的SharedPreferences保存用戶的密碼,雖然之前已經(jīng)學(xué)過(guò)了用戶偏好的方式保存密碼,但再一次寫的時(shí)候還是有點(diǎn)陌生,還是要多寫多練才行。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容