EditText文本輸入框
基本屬性
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/bg" // 背景
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp" // 外間距
android:paddingLeft="50dp" // 內(nèi)間距
android:textColor="#ffffff" // 字體顏色
android:textSize="30sp"
android:hint="請(qǐng)輸入密碼" // 默認(rèn)提示文本
android:textColorHint="#999999"
android:maxLines="1"
android:inputType="textPassword" // 鍵盤(pán)內(nèi)向和固定行數(shù)
android:cursorVisible="false" // 光標(biāo)是否可見(jiàn)
android:imeOptions="actionGo" // 按鍵鍵盤(pán)的類(lèi)型
/>
監(jiān)聽(tīng)鍵盤(pán)事件的集中方式
// 1. 當(dāng)前這個(gè)Activity來(lái)監(jiān)聽(tīng)事件
et.setOnEditorActionListener(this);
// 實(shí)現(xiàn)接口
public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener {
// 鍵盤(pán)被按下的回調(diào)事件
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
System.out.println("點(diǎn)擊了");
return false;
}
}
// 2.創(chuàng)建一個(gè)類(lèi) 管理事件的回調(diào)
class aaListener implements TextView.OnEditorActionListener{
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
System.out.println("點(diǎn)擊了");
return false;
}
}
// 創(chuàng)建對(duì)象
aaListener pl = new aaListener();
et.setOnEditorActionListener(pl);
// 匿名對(duì)象
et.setOnEditorActionListener(new aaListener());
// 3.創(chuàng)建匿名類(lèi)對(duì)象
et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
et.getText();
return false;
}
});
// 4.Lambda表達(dá)式
et.setOnEditorActionListener((TextView textView, int i, KeyEvent keyEvent) ->{
System.out.println("點(diǎn)擊了");
return true;
}
);
// 監(jiān)聽(tīng)EditText文本內(nèi)容改變的事件
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) {
// 獲取目前輸入的個(gè)數(shù)
int len = charSequence.toString().length();
if (len > 6){
// 將最后一個(gè)刪除掉
// 只要前面6個(gè)
et.setText(charSequence.subSequence(0,6));
// 讓光標(biāo)定位到最后
et.setSelection(6);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
PIN解鎖demo
- xml搭建界面
<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/main"/>
<TextView
android:id="@+id/tv_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請(qǐng)?jiān)O(shè)置密碼"
android:textColor="@color/colorGray"
android:textSize="@dimen/dimen_alert"
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/normal"
android:layout_centerHorizontal="true"
android:layout_below="@+id/tv_alert"
android:layout_marginTop="30dp"
android:paddingLeft="50dp"
android:textColor="@color/colorGray"
android:textSize="@dimen/dimen_textView"
android:textColorHint="#999999"
android:maxLines="1"
android:inputType="textPassword"
android:maxLength="6"
android:cursorVisible="false"
android:letterSpacing="0.6"
/>
</RelativeLayout>
-
設(shè)置鍵盤(pán)一直顯示
密碼解鎖代碼
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.view.MotionEvent;
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);
//獲取保存的密碼
//獲取管理資源對(duì)象Resources
Resources res = getResources();
//通過(guò)這個(gè)對(duì)象獲取string.xml里面對(duì)應(yīng)的字符串
String fileName = res.getString(R.string.password_file_name);
//獲取共享的sp對(duì)象:1.文件不存在就創(chuàng)建 2.文件存在就打開(kāi)
final SharedPreferences sp = getSharedPreferences(fileName, MODE_PRIVATE);
//通過(guò)key獲取對(duì)應(yīng)的value
password = sp.getString("yx",null);
//顯示提示文本
if (password == null){
mTextView.setText("請(qǐng)?jiān)O(shè)置密碼");
}else{
mTextView.setText("請(qǐng)輸入密碼");
}
//監(jiān)聽(tīng)內(nèi)容改變的事件
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) {
//獲取文本內(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)密碼
mTextView.setText("請(qǐng)確認(rèn)密碼");
//清空內(nèi)容
mEditText.setText("");
}else{
//確認(rèn)密碼
if (firstInput.equals(inputPassword)){
//兩次密碼一致
mTextView.setText("設(shè)置密碼成功中姜!");
goToNext();
//保存密碼
SharedPreferences.Editor edit = sp.edit();
edit.putString("yx",firstInput);
edit.commit();
}else{
//密碼不正確
mTextView.setText("兩次密碼不一致 請(qǐng)重新設(shè)置");
firstInput = null;
mEditText.setText("");
}
}
} else{
//密碼設(shè)置過(guò)了
if(inputPassword.equals(password)){
//密碼正確
mTextView.setText("密碼正確!");
goToNext();
}else{
//不正確
mTextView.setText("密碼錯(cuò)誤請(qǐng)重新輸入");
//清空
mEditText.setText("");
}
}
}
}
});
}
-
實(shí)現(xiàn)界面跳轉(zhuǎn)
1.先創(chuàng)建一個(gè)界面
2.代碼實(shí)現(xiàn)跳轉(zhuǎn)
3.點(diǎn)擊屏幕實(shí)現(xiàn)返回