在编辑时以编程方式填充EditTextPreference

时间:2018-10-31 03:47:29

标签: android edittextpreference

下面是我的代码的一部分,该代码可以自动设置电话号码。

  @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Preference connectionPref = findPreference(key);
        if (connectionPref != null) {
            connectionPref.setSummary(sharedPreferences.getString(key, ""));
        }

        //get phone number automatically
        if (key.equals("pref_phone_number")) {
            if (ContextCompat.checkSelfPermission(MainActivity.getContext(), android.Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

            }

            TelephonyManager tMgr = (TelephonyManager) MainActivity.getContext().getSystemService(Context.TELEPHONY_SERVICE);
            String mPhoneNumber = tMgr.getLine1Number();
            connectionPref.setSummary(mPhoneNumber);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(key, mPhoneNumber);
            editor.commit();
        }

    }

在单击EditTextPreference并打开编辑窗口时,是否可以通过编程方式用mPhoneNumber填充else?我想让用户能够编辑他们的号码,而现在他们不能。

1 个答案:

答案 0 :(得分:0)

你可以这样尝试吗,

EditTextPreference pref = (EditTextPreference)PreferenceManager.findPreference("edit");
EditText prefEditText = pref.getEditText();
prefEditText.setInputType(InputType.TYPE_CLASS_TEXT);
prefEditText.setSingleLine(true);
prefEditText.setText("yourText");