android EditText in preference screen

南楼画角 提交于 2019-12-07 11:25:28

What you could do is just make a standard Preference which will show a dialog containing an EditText, and add an ontextchangelistener:

searchET.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub              
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub              
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });

and as far as showing the search results, I don't know. I've never tried any of this specifically before, but it seems like it would work for you, and you'd have to figure out whatever code is needed to perform the search itself, but this would add the initial functionality.

EDIT: By standard Preference, I mean in the XML:

<Preference
    android:title="Time Zone"
    android:summary="Choose your time zone"
    android:key="timeZone"/>

And in your PreferenceActivity add the following:

Preference timeZone = (Preference)findPreference("timeZone");
     timeZone.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference arg0) {
                showDialog(1);
                return false;
            }
        });



@Override
    protected Dialog onCreateDialog(int id) {

    switch (id) {       

    case 1: 
        final EditText searchET = new EditText(this);
//do the searchET.addTextChangedListener here
                 return new AlertDialog.Builder(this)
.setTitle("Choose Time Zone")
.setView(showAppString)
.setPositiveButton(new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
    //save the time zone to sharedPreferences
SharedPreferences pref = PreferenceManager.getDefaultSharedPreference(this);
SharedPreferences.Editor editor = pref.edit();
editor.putString("TIME_ZONE", TZ.toString());

}//closes the onClick
})//closes the onclicklistener
.show();
}//closes the switch

}//closes the oncreatedialog

Though, I think you could actually just use an EditTextPreference and then addTextChangedListner to the EditTextPreference, since I THINK it inherits everything from EditTexts. I'm not postitive, though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!