一、前言:這篇文章是關(guān)于當(dāng)我們輸入賬號(hào)密碼時(shí),達(dá)到11位(自定義)時(shí),自動(dòng)隱藏鍵盤。
二、上代碼:
界面布局:
<?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"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rg_login"
android:layout_width="match_parent"
android:layout_height="@dimen/item_layout_height"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_password"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:checked="true"
android:text="@string/login_by_password"
android:textSize="@dimen/common_font_size" />
<RadioButton
android:id="@+id/rb_verifycode"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/login_by_verifycode"
android:textSize="@dimen/common_font_size" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/item_layout_height"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/phone_number"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<EditText
android:id="@+id/et_phone"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:inputType="number"
android:hint="@string/input_phone_number"
android:maxLength="11"
android:textColorHint="@color/gray"
android:textSize="@dimen/common_font_size" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/item_layout_height"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/login_password"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:hint="@string/input_password"
android:inputType="numberPassword"
android:maxLength="11"
android:textColorHint="@color/gray"
android:textSize="@dimen/common_font_size" />
<Button
android:id="@+id/btn_forget"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:text="@string/foget_password"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
</RelativeLayout>
</LinearLayout>
<CheckBox
android:id="@+id/ck_remember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/remember_password"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/login"
android:textSize="@dimen/buttom_font_size" />
</LinearLayout>
對(duì)應(yīng)的Activity:LoginMainActivity文章來源:http://www.zghlxwxcb.cn/news/detail-529456.html
public class LoginMainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private TextView tv_password;
private EditText et_password;
private Button btn_forget;
private CheckBox ck_remember;
private EditText et_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_main);
RadioGroup rg_login = findViewById(R.id.rg_login);
tv_password = findViewById(R.id.tv_password);
et_phone = findViewById(R.id.et_phone);
et_password = findViewById(R.id.et_password);
btn_forget = findViewById(R.id.btn_forget);
ck_remember = findViewById(R.id.ck_remember);
//給rg_login設(shè)置單選監(jiān)聽器
rg_login.setOnCheckedChangeListener(this);
//給et_phone添加文本變更器
et_phone.addTextChangedListener(new HideTextWatcher(et_phone,11));
//給et_password添加文本變更器
et_password.addTextChangedListener(new HideTextWatcher(et_password,6));
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
//選擇密碼登錄
case R.id.rb_password:
tv_password.setText(getString(R.string.login_password));
et_password.setHint(getString(R.string.input_password));
btn_forget.setText(getString(R.string.foget_password));
ck_remember.setVisibility(View.VISIBLE);
break;
//選擇驗(yàn)證碼登錄
case R.id.rb_verifycode:
tv_password.setText(getString(R.string.verifycode));
et_password.setHint(getString(R.string.input_verifycode));
btn_forget.setText(getString(R.string.get_verifycode));
ck_remember.setVisibility(View.GONE);
break;
}
}
//定義一個(gè)編輯框監(jiān)聽器,在輸入文本達(dá)到指定長(zhǎng)度時(shí)自動(dòng)隱藏輸入法
private class HideTextWatcher implements TextWatcher {
private EditText mView;
private int mMaxLength;
public HideTextWatcher(EditText v, int maxLength) {
this.mView = v;
this.mMaxLength = maxLength;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
//在編輯框的輸入文本變化后觸發(fā)
@Override
public void afterTextChanged(Editable s) {
//獲得已輸入的文本字符串
String str = s.toString();
if (str.length() == mMaxLength){
//隱藏輸入法軟鍵盤
ViewUtil.hideOneInputMethod(LoginMainActivity.this,mView);
}
}
}
}
新建一個(gè)工具類:ViewUtil文章來源地址http://www.zghlxwxcb.cn/news/detail-529456.html
public class ViewUtil {
public static void hideOneInputMethod(Activity act, View v){
//從系統(tǒng)服務(wù)中獲取輸入法管理器
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
//關(guān)閉屏幕上的輸入法軟鍵盤
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
}
}
到了這里,關(guān)于android:登錄界面,輸入框輸入數(shù)量達(dá)到了之后自動(dòng)隱藏鍵盤。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!