您好,登錄后才能下訂單哦!
package com.example.view; import com.iflytek.voiceads.AdError; import com.iflytek.voiceads.AdKeys; import com.iflytek.voiceads.IFLYAdListener; import com.iflytek.voiceads.IFLYAdSize; import com.iflytek.voiceads.IFLYFullScreenAd; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.KeyEvent; import android.widget.EditText; import android.widget.Toast; public class HPEditText extends EditText { private boolean isRun = false; private String d = ""; private Context mContext; public HPEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.mContext=context; setBankCardTypeOn(); } public HPEditText(Context context, AttributeSet attrs) { super(context, attrs); setBankCardTypeOn(); } public HPEditText(Context context) { super(context); setBankCardTypeOn(); } public void setBankCardTypeOn() { HPEditText.this.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (isRun) { isRun = false; return; } isRun = true; d = ""; String newStr = s.toString(); newStr = newStr.replace(" ", ""); int index = 0; while ((index + 4) < newStr.length()) { d += (newStr.substring(index, index + 4) + " "); index += 4; } d += (newStr.substring(index, newStr.length())); int i = getSelectionStart(); HPEditText.this.setText(d); try { if (i % 5 == 0 && before == 0) { if (i + 1 <= d.length()) { HPEditText.this.setSelection(i + 1); } else { HPEditText.this.setSelection(d.length()); } } else if (before == 1 && i < d.length()) { HPEditText.this.setSelection(i); } else if (before == 0 && i < d.length()) { HPEditText.this.setSelection(i); } else HPEditText.this.setSelection(d.length()); } catch (Exception e) { } } private void insertText(EditText editText, String mText) { editText.getText().insert(getSelectionStart(), mText); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { // Toast.makeText(mContext, "最中輸入"+s, Toast.LENGTH_LONG).show(); } }); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。