0

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

  @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我想让用户能够编辑他们的号码,而现在他们不能。

4

1 回答 1

0

可以这样试试吗

EditTextPreference pref = (EditTextPreference)PreferenceManager.findPreference("edit");
EditText prefEditText = pref.getEditText();
prefEditText.setInputType(InputType.TYPE_CLASS_TEXT);
prefEditText.setSingleLine(true);
prefEditText.setText("yourText");
于 2018-10-31T04:03:58.860 回答