简体   繁体   中英

Getting value from edittext preference in preference screen

I'm new to android development. I just heard about preference screen in android. So I just made one. This is just to enter name in a dialog.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
   <EditTextPreference 
   android:title="Name"
   android:summary="To enter name click on Name"
   android:dialogTitle="Name Dialog"
   android:dialogMessage="Enter Your Name"
   android:key="name"/>

This is my created preference screen. I just need to know how can I access the entered name 'tony' in my mainactivity. I don't know the way to access the name from the dialog. Do I need to add any seperate activity for accessing the name in the name dialog. Can someone please explain it to me.

在此输入图像描述在此输入图像描述

Try this:

PreferenceManager.getDefaultSharedPreferences(this).getString("name", null);

Where this is a Context like an Activity . name is the key defined in your xml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM