溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

自定義view之HPEditText

發(fā)布時間:2020-07-08 22:45:52 來源:網(wǎng)絡(luò) 閱讀:424 作者:樂無莜 欄目:移動開發(fā)
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();
			}
		});
	}

}  	


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI