android - 从首选项屏幕中的 edittext 首选项获取值(value)

标签 android dialog android-activity preferencescreen edittextpreference

我是 Android 开发的新手。我刚刚听说 android 中的首选项屏幕。所以我只做了一个。这只是在对话框中输入名称。

<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"/>

这是我创建的首选项屏幕。我只需要知道如何在我的主要 Activity 中访问输入的名称“tony”。我不知道如何从对话框中访问名称。我是否需要添加任何单独的 Activity 来访问名称对话框中的名称。谁能给我解释一下。

enter image description here enter image description here

最佳答案

试试这个:

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

this 是一个类似于 ActivityContextname 是在您的 xml 文件中定义的 key

关于android - 从首选项屏幕中的 edittext 首选项获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21820031/

相关文章:

android - 为 x86 编译的 APK 可以上传到 Amazon Appstore 吗?

android - 如何使用 static{System.loadLibrary()} 在 android 中加载 native 库

具有自定义布局的 Android 对话框 : returning data

android - 子 Activity 继承的屏幕方向

android - 在 RxJava 中使用多个 .subscribe() 语句

JavaFx - 不想出现结束对话框

android - 警报消息未显示在警报对话框中?

android - 无法恢复 RecyclerView 的位置/状态

android - 如果 Activity 堆栈为空,则启动主 Activity

Android ScrollView /背景图片/Xamarin