在玩android的时候,有时候需要这样的功能,在editText里输入字符,要实时的监控输入的内容,比如 我要获取手机的IMEI号码。最后要输入*#06#就可以获取到了。那这是如何做到呢?或者说Phone是如何实现这个功能的呢?
这主要用到了TextWatcher接口。
public class DialpadFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, View.OnKeyListener, AdapterView.OnItemClickListener, TextWatcher, PopupMenu.OnMenuItemClickListener, DialpadImageButton.OnPressedListener
这个类是拨号界面的类。可以看到它就实现了TextWatcher
public void beforeTextChanged(CharSequence s, int start, int count, int after) { mWasEmptyBeforeTextChange = TextUtils.isEmpty(s); } @Override public void onTextChanged(CharSequence input, int start, int before, int changeCount) { if (mWasEmptyBeforeTextChange != TextUtils.isEmpty(input)) { final Activity activity = getActivity(); if (activity != null) { activity.invalidateOptionsMenu(); } } // DTMF Tones do not need to be played here any longer - // the DTMF dialer handles that functionality now. } @Override public void afterTextChanged(Editable input) { // When DTMF dialpad buttons are being pressed, we delay SpecialCharSequencMgr sequence, // since some of SpecialCharSequenceMgr‘s behavior is too abrupt for the "touch-down" // behavior. if (!mDigitsFilledByIntent && SpecialCharSequenceMgr.handleChars(getActivity(), input.toString(), mDigits)) { // A special sequence was entered, clear the digits mDigits.getText().clear(); } if (isDigitsEmpty()) { mDigitsFilledByIntent = false; mDigits.setCursorVisible(false); } updateDialAndDeleteButtonEnabledState(); }
判断输入的内容就是这句SpecialCharSequenceMgr.handleChars(getActivity(), input.toString(), mDigits))
进入了这里
static boolean handleIMEIDisplay(Context context, String input, boolean useSystemWindow) { if (input.equals(MMI_IMEI_DISPLAY)) { int subscription = MSimTelephonyManager.getDefault().getPreferredVoiceSubscription(); int phoneType; if (MSimTelephonyManager.getDefault().isMultiSimEnabled()) { phoneType = ((MSimTelephonyManager)context.getSystemService( Context.MSIM_TELEPHONY_SERVICE)).getCurrentPhoneType(subscription); } else { phoneType = ((TelephonyManager)context.getSystemService( Context.TELEPHONY_SERVICE)).getCurrentPhoneType(); } if (phoneType == TelephonyManager.PHONE_TYPE_GSM) { showIMEIPanel(context, useSystemWindow); return true; } else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) { showMEIDPanel(context, useSystemWindow); return true; } } return false; }
最后通过
AlertDialog alert = new AlertDialog.Builder(context) .setTitle(R.string.imei) .setMessage(imeiStr) .setPositiveButton(android.R.string.ok, null) .setCancelable(false) .show();
显示出对话框了。
android 监控EditText的变化
时间: 2024-11-08 20:17:24