1.EditText
文本編輯框:用戶輸入文本信息
可以輸入的文本類型如下:?
常用屬性:
?系統(tǒng)默認(rèn)的EditText:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".EdittextActivity"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="please enter user name"
android:inputType="text"
android:maxLength="20"
android:textColorHint="@color/purple_200"
></EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="please enter password"
android:inputType="numberPassword"
android:maxLength="16"
android:textColorHint="@color/teal_200"
android:paddingTop="20dp"
></EditText>
</LinearLayout>
效果圖:
當(dāng)然也可以自定義EditText的背景,比如用selector設(shè)定獲取焦點(diǎn)時(shí)EditText背景的變化。
selector:
其中focus和nofocus都是自定義的shape,分別表示聚焦和沒有聚焦時(shí)的背景,如下圖
focus shape:?
nofocus shape:?
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".EdittextActivity"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="default edit text"
android:inputType="text"
android:maxLength="20"
></EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" edit text with no background"
android:inputType="text"
android:maxLength="20"
android:background="@null"
></EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="use selector"
android:inputType="text"
android:maxLength="20"
android:background="@drawable/edittext"
></EditText>
</LinearLayout>
效果圖:
2.焦點(diǎn)變更監(jiān)聽器
使用EditText時(shí),可以在focus變更時(shí)觸發(fā)事件,常用于檢查EditText的內(nèi)容或者長(zhǎng)度。
注意這里是焦點(diǎn)變更,而不是點(diǎn)擊事件,因?yàn)镋ditText點(diǎn)擊一次觸發(fā)的是焦點(diǎn)變更,第二次點(diǎn)擊才會(huì)觸發(fā)點(diǎn)擊事件。
?xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".FocusActivity"
android:orientation="vertical"
>
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter 11-digit phone number "
android:background="@drawable/edittext"
android:maxLength="11"
android:inputType="number"
android:layout_marginTop="50dp"
android:layout_marginBottom="30dp"
android:textSize="20dp"
></EditText>
<EditText
android:id="@+id/etPw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter 8-digit password "
android:background="@drawable/edittext"
android:maxLength="8"
android:inputType="numberPassword"
android:layout_marginBottom="80dp"
android:textSize="20dp"
></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="30dp"
android:layout_gravity="center"
></Button>
</LinearLayout>
java:
package com.example.ch3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class FocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {
private EditText etPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_focus);
etPhone = findViewById(R.id.etPhone);
findViewById(R.id.etPw).setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View view, boolean b) {
if(b){
String phoneNum = etPhone.getText().toString();
if(phoneNum.length()<11){
etPhone.requestFocus();
Toast.makeText(this, "Please enter 11-digit phone number", Toast.LENGTH_LONG).show();
}
}
}
}
?效果圖:
當(dāng)焦點(diǎn)移向密碼欄時(shí),檢查手機(jī)號(hào)碼的長(zhǎng)度,若小于11位,將焦點(diǎn)返回手機(jī)號(hào)碼欄,彈出提示。
3.文本變化監(jiān)聽器
監(jiān)聽EditText的內(nèi)容變化,觸發(fā)響應(yīng)的動(dòng)作
?舉例:當(dāng)EditText的內(nèi)容長(zhǎng)度滿足要求后,隱藏輸入法窗口。
java代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-531884.html
package com.example.ch3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import com.example.ch3.until.UtilFunc;
public class TextChangeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_change);
EditText et_Phone = findViewById(R.id.etPhone);
EditText et_Pw = findViewById(R.id.etPw);
et_Phone.addTextChangedListener(new HideTextWatcher(et_Phone, 11));
et_Pw.addTextChangedListener(new HideTextWatcher(et_Pw, 8));
}
private class HideTextWatcher implements TextWatcher {
private EditText et;
private int maxL;
public HideTextWatcher(EditText et, int maxL) {
this.et = et;
this.maxL = maxL;
}
@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 s = editable.toString();
if(s.length()==maxL){
UtilFunc.hideInput(TextChangeActivity.this, et);
}
}
}
}
隱藏輸入法的工具函數(shù)實(shí)現(xiàn):文章來源地址http://www.zghlxwxcb.cn/news/detail-531884.html
package com.example.ch3.until;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class UtilFunc {
public static void hideInput(Activity act, View v){
InputMethodManager imm = (InputMethodManager)
act.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
}
}
到了這里,關(guān)于Android開發(fā) 文本輸入 EditText 監(jiān)聽器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!